6

I would like to skip executing some code in cells of a jupyter notebook programmatically without wrapping everything in if-else blocks.

The closest solution I found so far from this SO question: https://stackoverflow.com/a/56953105/3124206 is this:

class StopExecution(Exception):
    def _render_traceback_(self):
        pass

raise StopExecution

However, it stops both current cell execution as well all of the subsequent ones while I only want an early exit from the current cell. Is there a way to continue execution of other cells?

Here is a code sample:

if skip_page:
    display(HTML('<!--SKIP-PAGE-->'))
    stop_cell()

render_some_output()

In case stop_cell() is called, I would like render_some_output() not to execute but the notebook execution to carry on overall.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3124206
  • 375
  • 1
  • 7
  • 16
  • I've only recently discovered jupyter notebook, so I'm probably not fully aware what it can do. Is there a specific reason you can't put cell contents in a function and call it? In that case you could `return` from the function as usual. – CGundlach Mar 05 '20 at 09:24
  • Something like this https://stackoverflow.com/questions/26494747/simple-way-to-choose-which-cells-to-run-in-ipython-notebook-during-run-all? – pgcudahy Mar 05 '20 at 09:27
  • 1
    @CGundlach you can but it's ugly with a lot of cells – user3124206 Mar 05 '20 at 11:10
  • @pgcudahy: thanks, this is closer. It unfortunately only works at the top of the cell, do you see a way to return from the middle of it (while preserving the current output)? – user3124206 Mar 05 '20 at 11:13
  • Can you provide an example of the code you are running and where you would like it to halt? – pgcudahy Mar 05 '20 at 12:16
  • @pgcudahy: just edited the question to include a sample – user3124206 Mar 05 '20 at 12:31
  • I don't think that is possible. The notebook stops once an error is detected, which helps to debug the notebook. If you want to skip several lines, you unfortunately have to use if-clauses or something to skip them. – Kim Tang Mar 05 '20 at 14:30
  • Use `%%script false --no-raise-error` as the first line of a cell to leave code in a cell but not have it be run, see [here](https://stackoverflow.com/a/60556423/8508004) for source. If you did this for a lot of cells you could convert the notebook to a python script using Jupytext command line, edit by find replace all to comment off, and then convert the notebook back using Jupytext. Or edit the json in the `.ipynb` directly. – Wayne Mar 06 '20 at 03:14
  • I should add I found an earlier Stackoverflow post of that trick [here](https://stackoverflow.com/a/37308200/8508004). – Wayne Mar 06 '20 at 03:22
  • Note that _render_traceback_ is meant to return list of strings (lines of traceback). So it should do `return []` instead of pass. Otherwise it can trigger problems with nbconvert. See code at https://github.com/ipython/ipython/blob/4f6e132d2fc56feabd9d87cac98906d3e5806d6a/IPython/core/interactiveshell.py#L1994 – Stan Oct 03 '22 at 12:22

3 Answers3

0

I know you don't want to use a lot of if else statements, but I think the simplest solution is to have one cell with global_run_flag = False and then before each optional code block, just preface it with if not global_run_flag: Then when you turn the global_run_flag to True, it will skip execution of all the wrapped code blocks.

pgcudahy
  • 1,542
  • 13
  • 36
0

I had the same question and found a very simple solution here:

  • in the command mode (toggle via the Esc key) press r in the keyboard, the cell won't be run.
  • to make it active again press Esc+y.
massisenergy
  • 1,764
  • 3
  • 14
  • 25
0

You can use raise SystemExit.

raise SystemExit("Stop here")

FYI, exit(0) will terminate the kernel.

Tian
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 04 '23 at 12:07