Enabling notifications in your Jupyter notebooks for cell completion

Get notified when your long-running cell finishes execution.

Photo by Manja Vitolic on Unsplash

If you are a Jupyter Notebook user, there must have been scenarios when a particular cell took a lot of time to finish the execution. This is particularly common during model training in machine learning, hyperparameter optimization, or even when running lengthy computations, etc. If yes, then a browser notification that would inform you once the process is finished could come in real handy. This way, you would be able to navigate to other tabs and only return to your machine learning experiment once you get that completion notification. Well, it turns out that there is a Jupyter extension that does exactly this and is very aptly named Notify. In this article, we’ll see how to enable notifications both in Jupyter Notebook as well as Jupyter Lab using notify.


Notify: A Jupyter Magic For Browser Notifications of Cell Completion

Notify is a Jupyter Notebook extension that notifies the user once a long-running cell has finished executing. It does so through browser notification. As of now, notify is supported by Chrome (Version: 58.0.3029) and Firefox (Version: 53.0.3).


Let’s now see how we can install the package and use it.

Installation

The installation can be done via pip

pip install jupyternotify

or through source as:

git clone git@github.com:ShopRunner/jupyter-notify.git
cd jupyter-notify/
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
jupyter notebook

Enabling notifications in Jupyter Notebook

Firstly, we shall look at how we can enable notifications in Jupyter Notebooks. Enter the following text in the first cell of the Notebook:

%load_ext jupyternotify

When you’ll run this cell for the first time, your browser will ask you to allow notifications in your notebook. You should press ‘Yes.’ Now we are all set. Here I’ll show you a small code snippet that using the sleep() function. This function suspends (waits) the execution of the current thread for a given number of seconds.

%%notify
import time
time.sleep(10)
print('Finished!')

On executing the above cell, you’ll get the following notification in your browser:

GIF by user

When you click on the body of the notification, it will bring you directly to the browser window and the tab containing your notebook.


Enabling notifications in Jupyter Lab

While currently, there is no official support for Jupyter Lab, I found a workaround. Apparently, a user has submitted a pull request with the solution, but it hasn’t been merged as yet. However, the solution does work seamlessly.

source: https://github.com/ShopRunner/jupyter-notify/pull/38

Paste the following in a Jupyter Lab cell followed by the code that you wish to run:

!pip uninstall jupyternotify -y
!pip install git+https://github.com/cphyc/jupyter-notify.git
%reload_ext jupyternotify

We’ll now use the same code as above to check if the notifications are enabled or not.

%%notify
import time
time.sleep(5)

GIF by user

Customizing the Notification message

If you wish to have a fancier notification message, the code can be easily tweaked to do so.

%%notify -m "Congrats.The process has finished"
import time
time.sleep(3)

There are many other options also available, for instance, firing a notification in the middle of a cell or automatically triggering the notification after a certain time. I’ll highly encourage you to check out the GitHub repository of the package for detailed information.


Conclusion

A feature as trivial as a notification can be beneficial, especially when you regularly work with processes that take a lot of time to execute. It is pretty handy to be able to navigate to another tab or even a desktop and still get notified when a running process finishes. Sometimes, small addons like jupyternotify can really help to increase the productivity of a user.


Originally published here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s