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.

ReubenBeeler
  • 142
  • 1
  • 10
McBear Holden
  • 5,741
  • 7
  • 33
  • 55

4 Answers4

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
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 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 ? – McBear Holden Apr 21 '11 at 07:38
  • 1
    sorry i read it again and i dont get it. i tried "%page %whos" didnt work; i tried out = %whos, %page out didnt work either – McBear Holden Apr 21 '11 at 10:22
  • 1
    @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. – Sven Marnach Apr 21 '11 at 12:11
  • 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 x` – Dima Tisnek Jan 30 '14 at 10:11
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
Kijewski
  • 25,517
  • 12
  • 101
  • 143
bstpierre
  • 30,042
  • 15
  • 70
  • 103
  • 1
    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 ? – McBear Holden Apr 21 '11 at 12:07
  • 1
    @osager: [This message](http://mail.scipy.org/pipermail/ipython-user/2011-February/007384.html) 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](http://www.perlmonks.org/index.pl?node_id=542341), where Y is impossible, so the solutions you're getting here are attempts to solve X (your bigger picture problem). – bstpierre Apr 21 '11 at 12:23
  • @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. – bstpierre Apr 21 '11 at 12:26
  • 1
    Thank you for your reply it helps a lot! especially with that first link to the ipython mailing list – McBear Holden Apr 21 '11 at 14:35
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 {...}.

KFL
  • 17,162
  • 17
  • 65
  • 89
  • 1
    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. – Shabble Mar 02 '22 at 17:00
  • @Shabble yes you're correct. Fixed typo! Thank you! – KFL Mar 23 '22 at 20:33
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

das_weezul
  • 6,082
  • 2
  • 28
  • 33