Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

python - 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). After the function runs, I have some more code (also in input cell X); is there any way for that code to retrieve the current output (in output cell X)?

There may be other ways to do what I am trying to achieve; but I am curious if the above is possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

IPython's output caching system defines several global variables:

  • [_] (a single underscore): stores previous output, like Python’s default interpreter.
  • [__] (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. 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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...