[英]Paging stdout output in IPython
Is it possible in an (interactive) IPython session to pass the stdout
output through a pager, like less
? 是否可以在(交互式)IPython会话中通过寻呼机传递
stdout
输出,如同less
? If so, how? 如果是这样,怎么样?
For example, in 例如,在
In [1]: from some_module import function_that_prints_a_lot
In [2]: function_that_prints_a_lot()
... everything scrolls away ...
I would like to page through the stdout
output of function_that_prints_a_lot
. 我想翻阅
function_that_prints_a_lot
的stdout
输出。
Another example: 另一个例子:
In [1]: %run script_that_prints_a_lot.py
I've looked through IPython magic commands but didn't find any solution. 我查看了IPython 魔术命令,但没有找到任何解决方案。
As discussed in chat there is no simple way of doing this. 正如聊天中所讨论的,没有简单的方法可以做到这一点。 Since the function prints the values, the only thing you can do is Capture output + Then page output.
由于该函数打印了值,因此您唯一能做的就是捕获输出+然后页面输出。 There are few issues on jupyter that you might be interested in
关于你可能感兴趣的jupyter的问题很少
https://github.com/jupyter/notebook/issues/2049 https://github.com/jupyter/notebook/issues/2049
https://github.com/ipython/ipython/issues/6516 https://github.com/ipython/ipython/issues/6516
Capturing output 捕获输出
Output capturing can be done multiple ways 输出捕获可以通过多种方式完成
import sys
data = ""
def myprint(value, *args, sep=' ', end='\n', file=sys.stdout, flush=False):
global data
current_text = value + " ".join(map(str, args)) + "\n"
data += current_text
original_print = print
print = myprint
def testing():
for i in range(1,1000):
print ("i =", i)
testing()
original_print("The output from testing function is", data)
from cStringIO import StringIO
import sys
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
Usage: 用法:
with Capturing() as output:
do_something(my_object)
import io
from contextlib import redirect_stdout
f = io.StringIO()
with redirect_stdout(f):
do_something(my_object)
out = f.getvalue()
Paging Output 分页输出
You can use magin %page
你可以使用magin
%page
%page -r <variablename>
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-page https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-page
Or you can use Ipython code 或者您可以使用Ipython代码
from IPython.core import page
page.page(variable)
For more details refer to below 有关详细信息,请参阅下文
PS: Some helpful threads PS:一些有用的线程
How to capture stdout output from a Python function call? 如何从Python函数调用中捕获stdout输出?
How can I redirect print output of a function in python 如何在python中重定向函数的打印输出
https://github.com/ipython/ipython/wiki/Cookbook:-Sending-built-in-help-to-the-pager https://github.com/ipython/ipython/wiki/Cookbook:-Sending-built-in-help-to-the-pager
Using bits and pieces from various sources, but essentially it's from IPython's cookbook and Defining custom magics from IPython official documentation 使用来自各种来源的点点滴滴,但基本上来自IPython的食谱和定义来自IPython官方文档的定制魔法
In [1]: from IPython.core.magic import register_line_magic
In [2]: @register_line_magic
...: def my_pager(line):
...: "my line magic"
...: import io
...: from IPython.core import page
...: from contextlib import redirect_stdout
...: f = io.StringIO()
...: with redirect_stdout(f):
...: eval(line)
...: page.pager_page(f.getvalue())
...: del my_pager # don't pollute my namespace
In [3]: def pippo(): print('\n'.join(str(i)for i in range(80)))
In [4]: %my_pager pippo()
This approach has a serious drawback: if the function call that is the argument to %my_pager
returns a value, said value is lost (no, %mypager result=print_and_return()
won't work...) 这种方法有一个严重的缺点:如果作为
%my_pager
参数的函数调用返回一个值,则表示该值丢失(不, %mypager result=print_and_return()
将无效...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.