4

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
  • 5,567
  • 4
  • 38
  • 76

1 Answers1

7

try this instead of sys.exit():

raise SystemExit("Stop right there!")
jtlz2
  • 7,700
  • 9
  • 64
  • 114
Yatish Kadam
  • 454
  • 2
  • 11