2

I want to know if Jupyter Notebook has a command to stop subsequent cells from running once the command has been given.

I found this question and tried sys.exit() but some of the cells below the command are still being executed.

The structure of my script looks as follows:

# tons of cells above
if df["target"].mean() == 1:
    sys.exit("Condition met. Do not execute the cells below.")
# tons of cells below

Ideally, none of the cells represented by tons of cells below should be executed if df["target"].mean() is 1. Otherwise, I do want to the subsequent cells executed.

I suppose one solution would be to write the code of all the subsequent cells in an else statement inside the same cell where sys.exit() is written. However, I do not want to do this.

Arturo Sbr
  • 4,419
  • 2
  • 23
  • 51

1 Answers1

5

try this instead of sys.exit():

raise SystemExit("Stop right there!")
jtlz2
  • 6,105
  • 7
  • 54
  • 98
Yatish Kadam
  • 434
  • 2
  • 10