Enabling notifications in your Jupyter notebooks for cell completion
Get notified when your long-running cell finishes execution.
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.
The installation can be done via pip
pip install jupyternotify
or through source as:
git clone email@example.com:ShopRunner/jupyter-notify.git
pip install -r requirements.txt
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:
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.
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 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.
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
We’ll now use the same code as above to check if the notifications are enabled or not.
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"
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.
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