You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feature request: redirect magic's result to pager #9891
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.
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?
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.
@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)
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.
Well basically what I was thinking was when someone opens up IPython via the command line, like so
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 :)
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?
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.
Activity
freddiev4 commentedon Sep 2, 2016
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?takluyver commentedon Sep 2, 2016
The magics code all lives in
IPython/core/magics- e.g.%whosis innamespace.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 toIPython.core.page.page(). That handles various details, like checking the$PAGERenvironment variable, and doing something sensible on Windows.freddiev4 commentedon Sep 3, 2016
@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+2or%whosinto the REPL)takluyver commentedon Sep 3, 2016
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.
freddiev4 commentedon Sep 4, 2016
Well basically what I was thinking was when someone opens up IPython via the command line, like so
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 :)
takluyver commentedon Sep 4, 2016
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.
freddiev4 commentedon Sep 6, 2016
So would making something like a custom decorator, or adding to the existing
@line_magicdecorator probably be a good idea? And then decorating each magics class? Or modifying the@magics_classdecorator?First thing that popped in to my head ^^
takluyver commentedon Sep 12, 2016
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.
freddiev4 commentedon Sep 12, 2016
Hmm all right. I'll make a list of magics that would most likely need to output to pager then
takluyver commentedon Sep 17, 2016
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.takluyver commentedon Sep 18, 2016
It's deciding whether to use the pager based on the estimated number of lines, not the string length:
ipython/IPython/core/page.py
Line 199 in 185e47e
Try putting newlines in so it sees it as 50 lines: