简体   繁体   English

以编程方式获取当前的 IPython 笔记本单元输出?

[英]Programmatically get current IPython notebook cell output?

I have an imported function that runs in an IPython notebook (input cell X) which produces an output (in output cell X).我有一个在 IPython 笔记本(输入单元 X)中运行的导入函数,它产生一个输出(在输出单元 X 中)。 After the function runs, I have some more code (also in input cell X);函数运行后,我还有一些代码(也在输入单元格 X 中); is there any way for that code to retrieve the current output (in output cell X)?该代码有什么方法可以检索当前输出(在输出单元格 X 中)?

There may be other ways to do what I am trying to achieve;可能还有其他方法可以完成我想要实现的目标; but I am curious if the above is possible.但我很好奇上述是否可行。

IPython's output caching system defines several global variables: IPython 的输出缓存系统定义了几个全局变量:

  • [ _ ] (a single underscore): stores previous output, like Python's default interpreter. [ _ ](单个下划线):存储以前的输出,就像 Python 的默认解释器。
  • [ __ ] (two underscores): next previous. [ __ ](两个下划线):下一个上一个。
  • [ ___ ] (three underscores): next-next previous. [ ___ ](三个下划线):下一个下一个上一个。

Additionally, after each output x is created, there is a variable _<x> created with the output as its value.此外,在创建每个输出x会创建一个变量_<x>并将输出作为其值。 For example:例如:

In [12]: lst = [i for i in range(11)]

In [13]: lst
Out[13]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [14]: _13
Out[14]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Also, if you're interested, _i<x> contains the contents of the input cell x :此外,如果您有兴趣, _i<x>包含输入单元格x的内容:

In [15]: _i12
Out[15]: 'lst = [i for i in range(11)]'

You can get the output of the cell X by using _ or Out[X] .您可以使用_Out[X]获取单元格 X 的Out[X] Such as:如:

In [1]: 2 + 35
Out[1]: 37
In [2]: _ + 3
Out[2]: 40 
In [3]: lst = [i for i in range(5)]
        lst
Out[3]: [0, 1, 2, 3, 4]
In [4]: Out[1] #Using the output of Cell 1
Out[4]: 37
In [5]: Out[3][1] #Using the output of Cell 3
Out[5]: 1

Here, If you want to get the output of the previous cell, then you can use _ .在这里,如果你想获得前一个单元格的输出,那么你可以使用_ You can use two ( __ ) or three underscores( ___ ) as well to refer to output of the next previous and next-next previous cells respectively.您也可以使用两个 ( __ ) 或三个下划线 ( ___ ) 来分别引用下一个和下一个前一个单元格的输出。

However, if you have many cells in the notebook and you want to refer some particular cell, then Out[X] will be helpful.但是,如果笔记本中有许多单元格并且您想引用某个特定的单元格,那么Out[X]会有所帮助。

The existing answers don't work for when a cell calls a function that generates its own stdout .当单元格调用生成其自己的stdout的函数时,现有答案不起作用。

I found a different solution that catches all of the output of the previous cell, no matter how it was produced.我找到了一个不同的解决方案,它可以捕获前一个单元格的所有输出,无论它是如何生成的。

# cell 1:
%%capture output
print("blah")
func_that_prints("Bha")
# -----------------
# cell 2:
prev_cell_output = str(output)
# do something with prev_cell_output

Note that %%capture line must the very first line of a cell for it to work.请注意, %%capture行必须是单元格的第一行才能使其工作。 output can be renamed to any other variable name. output可以重命名为任何其他变量名。 There will be no output for the first cell displayed (as it'll be captured).显示的第一个单元格将没有输出(因为它将被捕获)。

output becomes available only in the following cell. output仅在以下单元格中可用。 It is a utils.io.CapturedIO object, so you can stringify it, or even call .show() on it, which will display its contents.它是一个utils.io.CapturedIO对象,因此您可以对其进行字符串化,或者甚至对其调用.show() ,这将显示其内容。

For more information, eg capturing just stdout , or just stderr use the reference .有关更多信息,例如仅捕获stdout或仅捕获stderr使用参考

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

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