IPython paging output

Is it possible in an (interactive) IPython session to pass stdout output through a pager, such as 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 go through the output of stdout function_that_prints_a_lot .

Another example:

 In [1]: %run script_that_prints_a_lot.py 

I looked through IPython magic commands , but did not find any solution.

+5
source share
2 answers

As discussed in the chat, there is no easy way to do this. Since the function prints the values, the only thing you can do is output Capture + Then output. There are several issues in jupyter that may interest you.

https://github.com/jupyter/notebook/issues/2049

https://github.com/ipython/ipython/issues/6516

Output capture

Data output can be performed in several ways.

1. Print overload method

 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) 

2. Capturing output using StringIO

 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 

Using:

 with Capturing() as output: do_something(my_object) 

3. Capturing output using redirect_stdout

 import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): do_something(my_object) out = f.getvalue() 

4. Capture with the capture command %%

Capture magic

Paging output

You can use magin %page

 %page -r <variablename> 

https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-page

Or you can use ipython code

 from IPython.core import page page.page(variable) 

See below for more details.

PS: Some useful topics

How to write stdout output from Python function call?

How to redirect function output in python

https://github.com/ipython/ipython/wiki/Cookbook:-Sending-built-in-help-to-the-pager

python overload

+4
source

Using bits and parts from different sources, but essentially it's from the IPython cookbook and Defining custom magicians from the official IPython documentation

 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 a function call, which is the argument of %my_pager , returns a value, the specified value is lost (no, %mypager result=print_and_return() does not work ...)

+1
source

Source: https://habr.com/ru/post/1272907/


All Articles