Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paging stdout output in IPython

Tags:

stdout

ipython

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

Another example:

In [1]: %run script_that_prints_a_lot.py

I've looked through IPython magic commands but didn't find any solution.

like image 722
Alexey Avatar asked Oct 26 '17 09:10

Alexey


People also ask

What is %% capture in Python?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

How do you use %% Timeit in Jupyter?

The “%timeit” is a line magic command in which the code consists of a single line or should be written in the same line for measuring the execution time. In the “%timeit” command, the particular code is specified after the “%timeit” is separated by a space.

What is pager in Python?

A pager is a terminal program that can be used to view the content of a file, or the output stream from another application. For instance, when we run man vim , the actual content is displayed in a pager, according to the $PAGER environment variable. Important for a pager is that the input can be streamed.

What is IPython command?

IPython bridges this gap, and gives you a syntax for executing shell commands directly from within the IPython terminal. The magic happens with the exclamation point: anything appearing after ! on a line will be executed not by the Python kernel, but by the system command-line.


1 Answers

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

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

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

Capturing output

Output capturing can be done multiple ways

1. Overload Print 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. Capture 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

Usage:

with Capturing() as output:
    do_something(my_object)

3. Capture 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 using %%capture magic 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)

For more details refer to below

PS: Some helpful threads

How to capture stdout output from a Python function call?

How can I redirect print output of a function in python

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

overload print python

like image 172
Tarun Lalwani Avatar answered Nov 15 '22 09:11

Tarun Lalwani