How to print stdout excerpt in IPython
Asked Answered
B

4

17

In a Linux terminal, when the output of one command is too long to read in one page, I can do this:

cat file | less

so that I can read and scroll up and down the output from the cat file.

How can I do this in IPython?

For example, I tried this and it didn't work:

whos | less

My original problem is that the output from whos is too much to be seen by doing Shift+Page Up and I don't want to change the scroll buffer.

Bin answered 21/4, 2011 at 7:18 Comment(0)
C
24

In IPython, you can use %page obj to show the object obj using your standard pager (usually less). Alternatively, you can increase the scroll buffer of your terminal, which might be convenient in any case.

%page obj -- display object similar to IPython default display (repr-like), using pager if output size requires

%page -r obj -- display object similar to print, using pager if size requires

%page can only take a plain name or attribute reference. It cannot evaluate an arbitrary expression, but you can use a temporary variable to work around this limitationL

tmp = ex * pr + ess - ion
%page tmp
Colicroot answered 21/4, 2011 at 7:21 Comment(4)
Hi thanks for the reply. i have heard of increasing the scroll buffer. but it's not what i'm looking for. i would like to use the command "less" within ipython to see the output from %whos. do you know anyway to do that ?Bin
sorry i read it again and i dont get it. i tried "%page %whos" didnt work; i tried out = %whos, %page out didnt work eitherBin
@osager: Sorry, I missed you want to paginate the output of the %whos magic command. That's a different story -- unfortunately, %page does not help here.Colicroot
best answer by far! by default displays repr of object. %page -r is useful for text, as it shows newlines and unicode same as print. page doesn't accept expression, so if you need something tricky you have to do tmp = 2**32 and then %page xTaal
T
3

Use of the pager should be automatic.

From the manual:

In order to configure less as your default pager, do the following:

  1. Set the environment PAGER variable to less.
  2. Set the environment LESS variable to -r (plus any other options you always want to pass to less by default). This tells less to properly interpret control sequences, which is how color information is given to your terminal.

For the bash shell, add to your ~/.bashrc file the lines:

export PAGER=less
export LESS=-r
Timetable answered 21/4, 2011 at 11:59 Comment(4)
hi first of all thanks for answering my question but maybe i didnt express myself correctly but people dont seem to understand my problem. my questions can be simply put as this: how can you do this in ipython: whos | less ?Bin
@osager: This message on the ipython list would seem to indicate that the feature you're asking for is not supported. It sounds like you have an XY problem, where Y is impossible, so the solutions you're getting here are attempts to solve X (your bigger picture problem).Timetable
@osager: If you really want the output of whos, you should do psource whos which dumps the source of that magic function and could be the starting point for you to write your own function that does exactly what you want.Timetable
Thank you for your reply it helps a lot! especially with that first link to the ipython mailing listBin
M
2

On my IPython (version 7.21) piping does work, after the ! prefix.

Basic usage:

!cat ~/.vimrc | less

Works even with python variable substitution:

# send `some_large_python_str` to pastebin
!echo "{some_large_python_str}" | pastebin

Note the use of quote " around the substitution {...}.

Misbecome answered 11/8, 2021 at 23:11 Comment(2)
I think you mean !echo "{some_str}" | pastebin, unless that string is actually the filename you want to feed into pastebin, since cat expects a filename.Shamblin
@Shamblin yes you're correct. Fixed typo! Thank you!Misbecome
S
0

System shell access

Any input line beginning with a ! character is passed verbatim (minus the !, of course) to the underlying operating system. For example, typing !ls will run ‘ls’ in the current directory.

Source: http://ipython.scipy.org/doc/rel-0.9.1/html/interactive/reference.html#id1

Smothers answered 21/4, 2011 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.