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

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

问题描述

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

但是,有时我想跳过一些对后续单元没有影响的单元(例如,它们可能包含已完成的分析分支),并且可能需要很长时间才能运行.这些单元格可以散布在整个笔记本电脑中,因此在下方运行所有内容"之类的功能不会有太大帮助.

有没有办法做到这一点?

理想情况下,可以为这些单元格添加一些特殊标志,以便可以手动运行"这些单元格,但在全部运行"时将其跳过.

编辑

@Jakob建议的

%%cache(ipycache扩展名)在某种程度上解决了该问题.

实际上,我什至在重新运行时甚至不需要加载任何变量(变量可能很大,但对于后续单元格来说则不必要),只有存储的输出才是分析结果.

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

很遗憾,当使用%%cache ...

重新运行时,所有输出结果都会丢失

EDIT II (2013年10月14日)

ipython + ipycache的主版本现在也可以腌制(并重新显示)代码单元输出.

对于丰富的显示输出,包括Latex,HTML(pandas DataFrame输出),请记住使用IPython的display()方法,例如display(Latex(r'$\alpha_1$'))

解决方案

当前,IPython笔记本中不包含此类功能. 尽管如此,仍有一些可能使您的生活更轻松,例如:

  • 使用%store或更好的%%cache魔术(扩展名)来存储这些间歇性单元格的结果,因此不必重新计算它们(请参阅https://github.com/rossant/ipycache )

  • 在您不想执行的单元格之前添加if==0:

  • 将这些单元格转换为原始单元格(但是您将丢失已经存储的输出!)

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

Jakob

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$'))

解决方案

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

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

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