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?
In this article, I’ll show you how to enable notifications in Jupyter Notebook and Jupyter Lab using Notify.
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.
You can install the package via pip . . .
pip install jupyternotify
or through source as:
git clone [email protected]:ShopRunner/jupyter-notify.git
pip install -r requirements.txt
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:
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.
On executing the above cell, you’ll get the following notification in your browser:
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 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.
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
We’ll now use the same code as above to check if the notifications are enabled or not.
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"
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.