How to Enable Jupyter Notebook Notifications

Get notified when your long-running cell finishes execution, and get on with your life.

Written by Parul Pandey
Published on Jan. 25, 2022
Brand Studio Logo

If you’re a regular Jupyter Notebook user, you’ve likely encountered a situation in which a particular cell takes a long time to finish its execution. This occurrence is particularly common during model training in machine learning, hyperparameter optimization or even just when running lengthy computations. Wouldn’t it be nice if a browser notification would inform you once the process is finished?  You could 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; it’s aptly named Notify.

What Is Notify for Jupyter Notebook?

Notify is a Jupyter Notebook extension that notifies the user once a long-running cell has finished executing through a browser notification. Notify is supported by Chrome and Firefox.

In this article, I’ll show you how to enable notifications in Jupyter Notebook and Jupyter Lab using Notify.

More From Parul Pandey10 Python Image Manipulation Tools You Can Try Today

 

Notify Browser Notifications

Notify is a Jupyter Notebook extension that notifies the user once a long-running cell has finished executing through a browser notification. Notify is  supported by Chrome and Firefox. However, if you face any issues regarding the installation, you can follow these detailed instructions to get things up and  running.

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

 

Installation

You can install the package via pip . . .

pip install jupyternotify

or through source as:

git clone [email protected]:ShopRunner/jupyter-notify.git
cd jupyter-notify/
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
jupyter notebook

Everything You Need to Practice Python4 Python Tools to Simplify Your Life

 

Enabling Notifications in Jupyter Notebook

First, we’ll 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 run this cell for the first time, your browser will ask you to allow notifications in your notebook; hit “Yes.”

Now we’re all set. 

Here I’ll show you a small code snippet using the sleep() function. This function suspends (waits) the execution of the current thread for a given number of seconds.

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

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

jupyter-notification-when-done


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

Push Notifications in Jupyter Notebook

 

Enabling Notifications in Jupyter Lab

While there is currently 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 yet. However, the solution works seamlessly.

upyter-notification-when-done
Source

Paste the following in a Jupyter Lab cell followed by the code 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)

Python Tutorials on Built In5 Ways to Write More Pythonic Code

 

Customize the Notification Message

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

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

Many other options are available. For instance, you can fire a notification in the middle of a cell or automatically trigger the notification after a certain time. I highly encourage you to check out the package’s GitHub repository for detailed information.

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’s 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 add-ons like Jupyter Notify can really help to increase your productivity and smooth out your workflow.

Explore Job Matches.