Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter magic to handle notebook exceptions

I have a few long-running experiments in my Jupyter Notebooks. Because I don't know when they will finish, I add an email function to the last cell of the notebook, so I automatically get an email, when the notebook is done.

But when there is a random exception in one of the cells, the whole notebook stops executing and I never get any email. So I'm wondering if there is some magic function that could execute a function in case of an exception / kernel stop.

Like

def handle_exception(stacktrace):     send_mail_to_myself(stacktrace)   %%in_case_of_notebook_exception handle_exception # <--- this is what I'm looking for 

The other option would be to encapsulate every cell in try-catch, right? But that's soooo tedious.

Thanks in advance for any suggestions.

like image 485
Florian Golemo Avatar asked Oct 18 '16 14:10

Florian Golemo


People also ask

What does %% do in Jupyter Notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.

What is %% Writefile in Jupyter Notebook?

%%writefile lets you output code developed in a Notebook to a Python module. The sys library connects a Python program to the system it is running on.

What are magic functions in Jupyter Notebook?

What are the magic commands? Magic commands are special commands that can help you with running and analyzing data in your notebook. They add a special functionality that is not straight forward to achieve with python code or jupyter notebook interface. Magic commands are easy to spot within the code.

What is the use of %Xmode magic function?

Using the %xmode magic function (short for Exception mode), we can change what information is printed. This extra information can help narrow-in on why the exception is being raised.


1 Answers

Such a magic command does not exist, but you can write it yourself.

from IPython.core.magic import register_cell_magic  @register_cell_magic('handle') def handle(line, cell):     try:         exec(cell)     except Exception as e:         send_mail_to_myself(e)         raise # if you want the full trace-back in the notebook 

It is not possible to load the magic command for the entire notebook automatically, you have to add it at each cell where you need this feature.

%%handle  some_code() raise ValueError('this exception will be caught by the magic command') 
like image 168
show0k Avatar answered Sep 23 '22 01:09

show0k