运行 IPython 笔记本时如何(间歇性地)跳过某些单元格? [英] How to (intermittently) skip certain cells when running IPython notebook?

查看:26
本文介绍了运行 IPython 笔记本时如何(间歇性地)跳过某些单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在重新打开笔记本时必须重新运行(大部分)笔记本,以便访问以前定义的变量并继续工作.

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".

编辑

%%cache (ipycache 扩展)在一定程度上解决了这个问题.

%%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.

作为变通方法,将 %%cache folder/unique_identifier 放在单元格的开头.代码只会执行一次,重新运行时不会加载任何变量,除非您删除 unique_identifier 文件.

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.

不幸的是,使用%%cache重新运行时所有输出结果都丢失了...

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

编辑 II(2013 年 10 月 14 日)

EDIT II (Oct 14, 2013)

ipython+ipycache 的主版本现在也处理(并重新显示)codecell 输出.

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

对于包括Latex、HTML(pandas DataFrame output)在内的丰富的显示输出,记得使用IPython的display()方法,例如,display(Latex(r'$alpha_1$'))>

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

推荐答案

目前,IPython notebook 中不包含此类功能.尽管如此,仍有一些可能性可以让您的生活更轻松,例如:

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)

在不想执行的单元格前添加一个if==0:

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!)

(参见 https://github.com/ipython/ipython/issues/2125 中的讨论)

这篇关于运行 IPython 笔记本时如何(间歇性地)跳过某些单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆