Enabling notifications in your Jupyter notebooks for cell completion | by Parul Pandey | Towards Data Science

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 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.
The installation can be done via pip
pip install jupyternotify
or through source as:
git clone git@github.com:ShopRunner/jupyter-notify.gitcd jupyter-notify/virtualenv venvsource venv/bin/activatepip install -r requirements.txtjupyter 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.
%%notifyimport timetime.sleep(10)print('Finished!')
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.
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%reload_ext jupyternotify
We’ll now use the same code as above to check if the notifications are enabled or not.
%%notifyimport timetime.sleep(5)
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 timetime.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.
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.