4

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?

gogasca
  • 9,283
  • 6
  • 80
  • 125
mcansado
  • 2,026
  • 4
  • 25
  • 39

2 Answers2

2

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

fny
  • 31,255
  • 16
  • 96
  • 127
  • 1
    Thanks, I confirm that `sys.exit(0)` halts the execution of a notebook, by working as a silent "break" or as an "early return". – user345394 Feb 05 '23 at 00:25
1

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.

Pyrce
  • 8,296
  • 3
  • 31
  • 46