Explore Books related to Python. Click Here

Adding Notifications to Long-Running Jupyter Notebook Cells

If you use Jupyter Notebook to run long-running processes, such as machine learning training, then you would probably like to know when the cell finishes executing. There is a neat browser plugin that you can use to help solve this issue called jupyter-notify. It will allow you to have your browser send a pop-up message when the cell finishes executing.

The notification will look something like this:

jupyter-notify sample image

Let's learn how you can add this notification to your Jupyter Notebook!


Installation

The first thing you need to do is install Jupyter Notebook, if you haven't done so already. Here's how you can do that using pip:

pip install jupyter

Once that is installed, you will need to install jupyter-notify:

pip install jupyternotify

Now that you have all the packages installed, let's try it out!


Using Jupyter-Notify

To use jupyter-notify, you will need to run Jupyter Notebook. In a terminal, run this command: jupyter notebook

Now enter the following text in the first cell of the Notebook:

%load_ext jupyternotify

This loads the jupyter-notify extension. Run that cell. You may see your browser ask you to allow notifications from your Notebook. You will want to allow that for the notifier to work properly.

Now you need to add the following code to the next cell in the notebook:

%%notify

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

Your Notebook should now look like this:

Example using jupyter-notify

Now run that second cell. It will call time.sleep() which makes the code pause execution for however many seconds you specify. In this case, you want to pause execution for 10 seconds, then print out a message that the cell is "Finished!". When the cell finishes execution, you should see a notification pop-up like this:

jupyter-notify sample image

If you would like to customize the message that jupyter-notify emits, then you can change the second cell to the following:

%%notify -m "The cell has finished running"

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

Note that the first line has changed to accept a flag and a message string. If you want, you can fire multiple messages off within the cell. Just place the %%notify -m "some message" as many times as necessary in your code.

For example, you could change the code above to this:

import time
time.sleep(10)
%notify -m "The cell finished sleeping"
print('Finished!')
%notify -m "All done!"

For this code to work, you need to make sure that %notify only has one percent (%) sign instead of two as this is a line-magic in Jupyter Notebook.


Wrapping Up

The jupyter-notify package is pretty neat, but it can be easy to miss the notification if you get interrupted by a coworker. One solution you could use it py-toolbox which has a Notify object that you can use to email yourself when a function or cell completes.

Either way, there are solutions available to you if you want your Jupyter Notebook to let you know when it is finished processing.

Personally, if I had a long running process I would probably put it into a Python script file rather than a Jupyter Notebook as that makes my code easier to test and debug. But if you like using Jupyter Notebook, these packages may be the way to go!

Copyright © 2022 Mouse Vs Python | Powered by Pythonlibrary