Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to (intermittently) skip certain cells when running IPython notebook?

I usually have to rerun (most parts of) a notebook when reopen it, in order to get access to previously defined variables and go on working.

However, sometimes I'd like to skip some of the cells, which have no influence to subsequent cells (e.g., they might comprise a branch of analysis that is finished) and could take very long time to run. These cells can be scattered throughout the notebook, so that something like "Run All Below" won't help much.

Is there a way to achieve this?

Ideally, those cells could be tagged with some special flags, so that they could be "Run" manually, but would be skipped when "Run All".

EDIT

%%cache (ipycache extension) as suggested by @Jakob solves the problem to some extent.

Actually, I don't even need to load any variables (which can be large but unnecessary for following cells) when re-run, only the stored output matters as analyzing results.

As a work-around, put %%cache folder/unique_identifier to the beginning of the cell. The code will be executed only once and no variables will be loaded when re-run unless you delete the unique_identifier file.

Unfortunately, all the output results are lost when re-run with %%cache...

EDIT II (Oct 14, 2013)

The master version of ipython+ipycache now pickles (and re-displays) the codecell output as well.

For rich display outputs including Latex, HTML(pandas DataFrame output), remember to use IPython's display() method, e.g., display(Latex(r'$\alpha_1$'))

like image 475
herrlich10 Avatar asked Oct 11 '13 02:10

herrlich10


People also ask

How do you skip cells in Jupyter notebook?

There is a surprisingly simple way to turn off specific cells in Jupyter Notebook without having to manually comment-out chunks of code. The simple solution is to place the cursor into the cell that you want to silence, then press Esc + r .

How do you stop a single cell from running in Jupyter notebook?

To interrupt a cell execution, you can click the ■ “stop” button in the ribbon above the notebook, or select “Interrupt Kernel” from the Kernel menue.


1 Answers

Though this isn't exactly what you seem to be looking for, if you wish to entirely omit the execution of a cell (where no cached results are loaded), you can add the following hack at the beginning of a cell (assuming you are using a Unix-based OS):

%%script false 

or a variant (working as of early 2020 -- see here for explanation):

%%script false --no-raise-error 
like image 197
Mark Avatar answered Sep 21 '22 15:09

Mark