Jupyter magic for exception handling for laptops

I have some lengthy experiments on Jupyter laptops. Since I don’t know when they will end, I add the email function to the last cell of the laptop, so I automatically receive an email when the laptop is done.

But when there is a random exception in one of the cells, the entire laptop stops working, and I never receive any letters. Therefore, I am wondering if there is any magic function that could perform a function if an exception / kernel stops.

how

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

Another option is to encapsulate each cell in a try-catch, right? But it is so tiring.

Thanks in advance for any suggestions.

+6
source share
3 answers

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

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

It is not possible to automatically load the magic command for the entire laptop; you must add it to every cell where you need this function.

 %%handle some_code() raise ValueError('this exception will be caught by the magic command') 
+8
source

@ show0k gave the correct answer to my question (regarding magic methods). Many thanks!:)

This answer inspired me to go a little deeper, and I came across an IPython method that allows you to define a special exception handler for the entire laptop .

I got it like this:

 from IPython.core.ultratb import AutoFormattedTB # initialize the formatter for making the tracebacks into strings itb = AutoFormattedTB(mode = 'Plain', tb_offset = 1) # this function will be called on exceptions in any cell def custom_exc(shell, etype, evalue, tb, tb_offset=None): # still show the error within the notebook, don't just swallow it shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset) # grab the traceback and make it into a list of strings stb = itb.structured_traceback(etype, evalue, tb) sstb = itb.stb2text(stb) print (sstb) # <--- this is the variable with the traceback string print ("sending mail") send_mail_to_myself(sstb) # this registers a custom exception handler for the whole current notebook get_ipython().set_custom_exc((Exception,), custom_exc) 

Thus, it can be placed in one cell at the top of any laptop, and as a result, it will do newsletters if something goes wrong.

Note on self / TODO: make this snippet in a small python module that can be imported into notepad and activated using line magic.

Be careful. The documentation contains a warning for this set_custom_exc method: "WARNING: adding your own exception handler to the main IPythons runtime will give you very good chances of unpleasant crashes. This tool should only be used if you really know what you are doing" .

+5
source

I don’t think there is a ready-made way to do this without using the try..except in your cells. AFAIK 4-year issue mentions this, but is still in an open state.

However, the runtools extension can do the trick.

+2
source

All Articles