Skip to content

feature request: redirect magic's result to pager #9891

Open

Description

@taoluo

sometimes we just want to temporarily check some info and don't like to make our console dirty. like %whos mentioned in this stackoverflow question. another case can be someone types %hist to remind himself. So I suggest add an option to those magics such that users can display the output in pager.

Activity

  1. added this to the wishlist milestone on Aug 21, 2016
  2. freddiev4 commented on Sep 2, 2016

    @freddiev4

    Hey so I'd be interested in making an attempt at this, but need a bit of guidance, as I'm not sure where to look as far as accessing something like pager via Python, and where the IPython magics-related code lives

    Thinking something like subprocess.call(['less', args...]) for opening pager?

  3. takluyver commented on Sep 2, 2016

    @takluyver
    Member

    The magics code all lives in IPython/core/magics - e.g. %whos is in namespace.py.

    You shouldn't need to call subprocess directly for the pager, there's already machinery to do that in IPython.core.page. For most things, you can just pass the data to IPython.core.page.page(). That handles various details, like checking the $PAGER environment variable, and doing something sensible on Windows.

  4. freddiev4 commented on Sep 3, 2016

    @freddiev4

    @takluyver OK. That makes sense. Should it be put into an existing file or in it's own file? And is there some example you could direct me to that shows where IPython input is accepted (i.e. when someone types in 2+2 or %whos into the REPL)

  5. takluyver commented on Sep 3, 2016

    @takluyver
    Member

    I'd do it as new options for the existing magics, so in the same functions already defining them.

    Code enters terminal IPython here and the IPython kernel here. But if you explain a bit more what you're interested in, we can probably point you to something more useful.

  6. freddiev4 commented on Sep 4, 2016

    @freddiev4

    Well basically what I was thinking was when someone opens up IPython via the command line, like so

    image

    and starts to enter their own input, and it's recognized as a magics command, then I could use IPython.core.page.page() to take the output of magics and show it in pager (i.e. less), sorta like what you mentioned above, and then maybe modify it so all magics output is put into pager regardless of length (or maybe not?)

    First time looking at the IPython source code aside from custom extensions, so I'm pretty excited about doing something like the above :)

  7. takluyver commented on Sep 4, 2016

    @takluyver
    Member

    I think it's easier to do it in the magic functions themselves; where they print output, just make them send it to the pager instead.

  8. freddiev4 commented on Sep 6, 2016

    @freddiev4

    So would making something like a custom decorator, or adding to the existing @line_magic decorator probably be a good idea? And then decorating each magics class? Or modifying the @magics_class decorator?

    First thing that popped in to my head ^^

  9. takluyver commented on Sep 12, 2016

    @takluyver
    Member

    I would probably do it inside specific magics where it's most likely to make sense, rather than trying to do something general with decorators.

  10. freddiev4 commented on Sep 12, 2016

    @freddiev4

    Hmm all right. I'll make a list of magics that would most likely need to output to pager then

  11. takluyver commented on Sep 17, 2016

    @takluyver
    Member

    IIRC, the page() function checks the length of text passed to it, and only actually invokes the pager if it's longer than some threshold. So small outputs are printed as normal, and long ones are paged.

  12. takluyver commented on Sep 18, 2016

    @takluyver
    Member

    It's deciding whether to use the pager based on the estimated number of lines, not the string length:

    if numlines <= screen_lines :

    Try putting newlines in so it sees it as 50 lines:

    s = "hello,world\n"
    page(50 * s)
  13. reopened this on Jan 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions