简体   繁体   English

运行 IPython 笔记本时如何(间歇性地)跳过某些单元格?

[英]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.我通常必须在重新打开笔记本时重新运行(大部分)笔记本,以便访问以前定义的变量和 go 工作。

However, sometimes I'd like to skip some of the cells, which have no influence to subsequent cells (eg, 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.这些单元格可以分散在整个笔记本中,因此“Run All below”之类的内容不会有太大帮助。

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. @Jakob 建议的%%cache (ipycache 扩展)在一定程度上解决了这个问题。

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.实际上,我什至不需要在重新运行时加载任何变量(可能很大但对于后续单元格来说是不必要的),只有存储的 output 对分析结果很重要。

As a work-around, put %%cache folder/unique_identifier to the beginning of the cell.作为一种解决方法,将%%cache folder/unique_identifier放在单元格的开头。 The code will be executed only once and no variables will be loaded when re-run unless you delete the unique_identifier file.代码将只执行一次,重新运行时不会加载任何变量,除非您删除 unique_identifier 文件。

Unfortunately , all the output results are lost when re-run with %%cache ...不幸的是,使用%%cache重新运行时,所有 output 结果都将丢失...

EDIT II (Oct 14, 2013)编辑二(2013 年 10 月 14 日)

The master version of ipython+ipycache now pickles (and re-displays) the codecell output as well. ipython+ipycache 的主版本现在也腌制(并重新显示)codecell output。

For rich display outputs including Latex, HTML(pandas DataFrame output), remember to use IPython's display() method, eg, display(Latex(r'$\alpha_1$'))对于丰富的显示输出,包括 Latex、HTML(pandas DataFrame 输出),记得使用 IPython 的 display() 方法,例如display(Latex(r'$\alpha_1$'))

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):虽然这不是您似乎正在寻找的内容,但如果您希望完全省略单元格的执行(没有加载缓存的结果),您可以在单元格的开头添加以下 hack(假设您是使用基于 Unix 的操作系统):

%%script false 

or a variant (working as of early 2020 -- see here for explanation) :或变体(从 2020 年初开始工作 - 请参阅此处了解解释):

%%script false --no-raise-error

Currently, there is no such feature included in the IPython notebook.目前,IPython 笔记本中不包含此类功能。 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 )使用%store或者更好的%%cache魔法(扩展)来存储这些间歇性单元格的结果,因此不必重新计算它们(参见https://github.com/rossant/ipycache

  • add a if==0: before the cells you don't want to execute在您不想执行的单元格之前添加if==0:

  • convert these cells to raw cells (but you will loose the already stored output!)将这些单元格转换为原始单元格(但您将丢失已经存储的输出!)

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

Jakob雅各布

If no cached results are expected to be loaded, I find the Freeze nbextension quite useful to this end.如果不希望加载缓存结果,我发现Freeze nbextension 对此非常有用。

在此处输入图片说明

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 .要启用Freeze扩展,请启动jupyter notebook并打开一个新 notebook,从菜单中选择Edit > nbextensions config ,然后选中Freeze

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 %%script 回声跳过

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.它在 Windows 和 Mac 上使用最近的 Jupyter 进行了测试,我认为它也应该适用于其他类 Unix 平台,因为它们也有一个 echo 命令。 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.当您执行单元格时,它只会打印此文本而不是执行单元格中的代码。

This question is a bit older, but the most convenient answer seems to be missing.这个问题有点老了,但似乎缺少最方便的答案。 You can use the 'initialization cells' from the NBextensions.您可以使用 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安装 NBextensions: 这里
  • Activate 'initialization cells' when you launch the jupyter dashboard启动 jupyter 仪表板时激活“初始化单元”

  • 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.重新打开笔记本时,单击看起来像袖珍计算器的按钮以运行所有初始化单元。

跳过运行 jupyter notebook 单元格中的 python 代码的最简单方法,我暂时将这些单元格转换为 Markdown。

The %%script false solution stopped working some time in 2019. %%script false解决方案在 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在这里你正在运行: 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在这里你正在运行: 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 ':' 是 bash 中的一个 noop,所以你正在运行: 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 ';'我没有看过外部魔术实现代码,但我很确定“cellcontents”作为参数传递并且不会被shell错误地解释,比如你是否要包含';' 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我相信你可以想出其他创造性的解决方案,通过查看这里支持的程序: https : //ipython.readthedocs.io/en/stable/interactive/magics.html#cell-magics

For the cells you wish to skip when you press Run All you can use try / except blocks, where you try to display the already calculated information in the try block, and perform the calculation in the except block.对于您希望在按Run All时跳过的单元格,您可以使用try / except块,您可以尝试在try块中显示已计算的信息,并在except块中执行计算。

Take this cell for example:以这个单元格为例:

my_results = 2 # Assume this is a bigger calculation
print(my_results)
print('displayed my results.')

To skip the calculation for this cell in subsequent runs, change the contents of this cell to the following:要在后续运行中跳过此单元格的计算,请将此单元格的内容更改为以下内容:

try:
  print(my_results)
  print('displayed state value')
except:
  my_results = 2 # Assume this is a bigger calculation
  print(my_results)
  print('displayed newly calculated value')

The first time you run this cell, it will attempt to print the value of the state variable my_results .第一次运行此单元格时,它将尝试打印 state 变量my_results的值。 This throws an exception so it jumps to the except block and does the actual calculation of my_results (which in this case just makes it equate to 2 ).这会引发异常,因此它会跳转到except块并进行my_results的实际计算(在这种情况下,它等于2 )。 At the end of the first run the output for this cell would be:在第一次运行结束时,此单元的 output 将是:

2
displayed newly calculated value

When you run the cell a second time (whether that be manually or via Run All ), the try block will first execute but this time since the variable is available in the state, it does not throw an exception.当您第二次运行单元格时(无论是手动运行还是通过Run All ), try块将首先执行,但这次由于变量在 state 中可用,因此不会引发异常。 Instead it displays the result and the except block is never run.相反,它会显示结果,并且永远不会运行except块。 At the end of the second run the output of this cell would be:在第二次运行结束时,该单元的 output 将是:

2
displayed state value

This doesn't meet your explicit criteria that the cell should be completely skipped, but effectively the calculation is skipped.这不符合您应该完全跳过单元格的明确标准,但实际上跳过了计算。

If the displaying of the information is more complex than using a single print or display , it would probably be cleaner if you make the displaying routine into a function like so:如果信息的显示比使用单个printdisplay更复杂,如果将显示例程变成 function 可能会更干净,如下所示:

def print_my_results(my_result):
  print(my_result)
  print('displayed my result.')

try:
  print_my_results(my_results)
except:
  my_results = 2 # Assume this is a bigger calculation
  print_my_results(my_results)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM