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.
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.
%%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 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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With