Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to halt execution of a Jupyter notebook using papermill?

I have a set-up whereby I have a few Jupyter notebooks that are parameterised so I can run them from another notebook.

I'm using the papermill module to do this which is quite convenient. The simplest way of running looks something like this

path = '/path/to/notebook.ipynb'
pm.execute_notebook(
   path,
   path,    
)

What I would now like is to have the ability of halting execution from within the notebook being run if certain conditions are not met.

Is there a way of doing this with papermill and, if not, is there another module that can do this?

like image 946
mcansado Avatar asked Oct 24 '25 03:10

mcansado


2 Answers

You can raise either SystemExit(0) or SystemExit('') directly or via sys.exit(0). Papermill is designed to ignore these errors: https://github.com/nteract/papermill/pull/449

like image 50
fny Avatar answered Oct 25 '25 17:10

fny


I would just use assert not failure_condition, "My condition failed" inside the notebook to force an exception to be raised. This will halt execution and raise an exception that papermill will wrap in a PapermillExecutionError in the parent process which called pm.execute_notebook.

like image 33
Pyrce Avatar answered Oct 25 '25 18:10

Pyrce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!