Jupyter magic to handle notebook exceptions

前端 未结 5 677
北恋
北恋 2020-12-16 11:22

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 au

5条回答
  •  天命终不由人
    2020-12-16 12:11

    @show0k gave the correct answer to my question (in regards to magic methods). Thanks a lot! :)

    That answer inspired me to dig a little deeper and I came across an IPython method that lets you define a custom exception handler for the whole notebook.

    I got it to work 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)
    

    So this can be put into a single cell at the top of any notebook and as a result it will do the mailing in case something goes wrong.

    Note to self / TODO: make this snippet into a little python module that can be imported into a notebook and activated via line magic.

    Be careful though. The documentation contains a warning for this set_custom_exc method: "WARNING: by putting in your own exception handler into IPython’s main execution loop, you run a very good chance of nasty crashes. This facility should only be used if you really know what you are doing."

提交回复
热议问题