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

后端 未结 7 2241
南笙
南笙 2020-12-04 15:41

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

相关标签:
7条回答
  • 2020-12-04 16:02

    Currently, there is no such feature included in the IPython notebook. Nevertheless, there are some possibilities to make your life easier, like:

    • use the %store or maybe better the %%cache magic (extension) to store the results of these intermittently cells, so they don't have to be recomputed (see https://github.com/rossant/ipycache)

    • add a if==0: before the cells you don't want to execute

    • convert these cells to raw cells (but you will loose the already stored output!)

    (see discussion at https://github.com/ipython/ipython/issues/2125)

    Jakob

    0 讨论(0)
  • 2020-12-04 16:07

    If no cached results are expected to be loaded, I find the Freeze nbextension quite useful to this end.

    Although unofficial, I strongly recommend to give these notebook extensions a try if you have never used them before.

    To install the extension machinery,

    $ pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install
    

    To enable the Freeze extension, launch jupyter notebook and open a new notebook, from the menu select Edit > nbextensions config, and then check Freeze.

    0 讨论(0)
  • 2020-12-04 16:15

    Here's a simple and universal solution without the need for workarounds: Simply type this as the top line of the cell to skip the cell:

    %%script echo skipping

    It's tested on Windows and Mac with recent Jupyter, and I think it should work on other Unix-like platforms as well because they also have an echo command. Some of the other proposed solutions are more platform-specific.

    Of course you can put what ever text you like instead of "skipping". When you execute the cell, it will merely print this text instead of executing the code in the cell.

    0 讨论(0)
  • 2020-12-04 16:16

    The %%script false solution stopped working some time in 2019.

    Here are some other available workarounds. These are based on programs ignoring their arguments when you tell them not to expect any. Here are some easy examples:

    Perl:

    %%perl -e0
    ​
    for i in range(10): print(i)
    

    Here you're running: perl -e '0' cellcontents

    A more memorable version:

    %%perl -eat
    ​
    for i in range(10): print(i)
    

    Here you're running: perl -e 'at' cellcontents

    Bash:

    %%bash -c :
    
    for i in range(10): print(i)
    

    ':' is a noop in bash, so you're running: bash -c : cellcontents

    I haven't looked at the external magic implementation code, but I'm pretty sure "cellcontents" are passed as arguments and won't be interpreted by shell by mistake, say if you were to include ';' in them and accidentally inject some bad code. But I can't guarantee you that.

    I'm sure you can come up with other creative solutions, by looking at the supported programs here: https://ipython.readthedocs.io/en/stable/interactive/magics.html#cell-magics

    0 讨论(0)
  • 2020-12-04 16:17

    This question is a bit older, but the most convenient answer seems to be missing. You can use the 'initialization cells' from the NBextensions. Once installed/activated, you can in any notebook mark cells as 'initialization cells' which can then be run with a specific button.

    • Install NBextensions:here
    • Activate 'initialization cells' when you launch the jupyter dashboard

    • In your notebook, in the 'view' menu choose 'cell toolbar' then 'initialization cell'

    • Next to each cell there is now a checkbox. Mark all the cells you want to run for initialization
    • When reopening a notebook, click the button that looks like a pocket calculator to run all the initialization cells.
    0 讨论(0)
  • 2020-12-04 16:21

    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
    
    0 讨论(0)
提交回复
热议问题