"Decorators" around Jupyter Notebook cells

Viewed 20

I'm curious about whether it's possible to configure Jupyter Notebook/Lab to run code "around" the cells, in the similar fashion as decorators do. A concrete example:

Say I have a cell with some code:

[some code]

What I'd like to do is to log if this code raises an exception. So I could achieve it with the following:

try:
    [some code]
except Exception as e:
    logger.exception()
    raise e

Ideally I'd like to do this globally for each cell, without the need to explicitly write the try-except clause each time.

This is useful, for example, if I run something that takes a long time to finish. This way I can close the notebook, and it will keep running in the background, but if it fails, I'll be able to debug it using the logged traceback.

0 Answers
Related