The world’s leading publication for data science, AI, and ML professionals.

Personalized your Python Notification Sounds with Chime

Create an auditory notification system to notify your running code

Photo by Ryan Quintal on Unsplash
Photo by Ryan Quintal on Unsplash

I am pretty sure when you are working on your code; there are times when you do it parallel. There would be a time where many projects are going on, and you need to run a long script that you need to wait.

Previously, I have written on how to get notified via email when you have finished running your machine learning model or any scripts.

Automatic Notification to Email with Python

Although, there are times that you did not check email at all or simply just too busy on your laptop but really need the notification when the modeling or coding script is done.

In this case, I previously find an interesting open-source by Max Halford called Chime to get you notified using sounds when you have finished running all your code (or when you get an error).

Let’s try this out!


Chime

First thing first, let us install the required package. The chime package does not rely on any dependencies, so that it would ruin any of your existing packages.

pip install chime

When you have finished installing the package, we could start to get notified with a sound using the Chime package.

Let’s start by testing the basic function that chime has.

#importing chime package
import chime
#Successfully running the code sounds
chime.success()

When you run the code above, there should be a notification sounds going out. This is a specific sound that starts when you have success running the code or any process.

We could also test the error sounds by running the following code.

chime.error()

The sound should be different compared to the success sound.

For a note that the sounds are played in asynchronous processes and are thus non-blocking. Each sound should take around 2ms to start going out after the coding processing, regardless of the sound length.

Another function you could try testing is the warning function and information function, which also produces different sounds.

#Warning notification sound
chime.warning()
#Information notification sound
chime.info()

Chime Integration with Jupyter

Well, you could include the Chime function on your script to notified each process you feel need a notification. Still, if you need the notification in each Jupyter notebook cells, we need to do some preparation.

First, we need to load the extension by running the following code.

%load_ext chime

After that, we are ready to get a sound notification in each Jupyter cell we run. Let’s try a simple one by wrapping a single line of code in a Jupyter cell.

#Wrapping a single line in jupyter cell
%chime print("Sounds out")

If you want to wrap the whole Jupyter cell, you need to run it differently, just like the following code.

%%chime

print("Whole cell")

When you successfully run the cell, it would produce the sounds from chime.success() otherwise it would use the chime.error() sound.


Changing Sounds Theme

You could change the Chime notification sounds theme by setting up the available Chime theme. To check which theme available, you could run the following code.

chime.themes()

There are currently only three themes available; ‘chime’, ‘mario’, and ‘zelda’. To change the theme, just run the following code.

#Change the notification sound with Zelda theme
chime.theme('zelda')

This would change the theme sound to the Zelda notification theme sound. If you feel like having a different sound each time, you could change the theme to ‘random’.

If you want to have your own sounds notification, for a moment, you need to propose a new theme by opening a pull request that adds the necessary .wav files to the themes directory. For a theme, they are made up of four files: success.wav, warning.wav, error.wav, and info.wav.


Conclusion

Chime is an open-source package to notify you with a sound when you have successfully running your code (or getting an error).

When you want to add a new theme of your own sound, you need to pull the request first and propose your own sounds.

I hope it helps!


If you enjoy my content and want to get more in-depth knowledge regarding data or just daily life as a Data Scientist, please consider subscribing to my newsletter here.

If you are not subscribed as a Medium Member, please consider subscribing through my referral.


Related Articles