Skip to main content

IPython/Jupyter cell magic to execute code after a cell succeeds or fails

Project description

on-done

An IPython/Jupyter cell magic to execute code after a cell succeeds or fails so you can receive an alert when your code terminates.

Installation

$ pip install on-done

Example

Start by launching an IPython session:

$ ipython

Then, load the extension:

In [1]: %load_ext on_done

Next, define a function to alert you that your code has terminated.

In [2]: def alert():
   ...:     from datetime import datetime
   ...:     print(f"Cell terminated at {str(datetime.now())}\n")

You can change this function to send yourself an email, Slack alert, etc.

Putting %%on_done alert() at the top of a cell will cause it to alert:

In [3]: %%on_done alert()
   ...: import numpy as np
   ...: x = np.random.randn(10**8)
   ...: print(f"mean={x.mean()}")
mean=0.00021783589122321365
Cell terminated at 2022-11-21 00:35:40.678180

In particular, the alert will also occur if the cell failed:

In [4]: %%on_done alert()
   ...: import numpy as np
   ...: x = np.random.randn(10**8)
   ...: print(f"mean={x.mean(axis=1)}")
Cell terminated at 2022-11-21 00:35:50.654766

---------------------------------------------------------------------------
AxisError                                 Traceback (most recent call last)
...

Credit

on-done is inspired by this Stack Overflow answer by Colin Carroll

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

on-done-0.1.1.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

on_done-0.1.1-py3-none-any.whl (3.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page