Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make cell output in Jupyter notebook scroll horizontally?

I have a long Sympy expression that I'd like to get printed with a horizontal scrollbar beneath it. Is it possible to do so in Jupyter? I'm able to toggle vertical scrolling but I want it to be horizontally scrollable instead. The problem with vertical scrolling is that the output of sympy.pretty_print() gets badly distorted in my case. The output also looks ugly and the user has to scroll through the whole output unnecessarily.enter image description here

like image 298
Safwan Ahmad Avatar asked Jan 20 '18 14:01

Safwan Ahmad


People also ask

How do you make a scrollable output in a Jupyter notebook?

You can try Cell -> Current Outputs -> Toggle Scrolling in the Jupyter UI to enable the scrolling for the output of one cell.

How do I make my element horizontally scrollable?

For horizontal scrollable bar use the x and y-axis. Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line.

How do I scroll horizontally in Python idle?

I'm using IDLE 2.7. 3, Windows 7, and I can scroll horizontally by holding down the center mouse button/scroll wheel, and "dragging" around the cursor like that.


2 Answers

Something similar to the np.set_printoptions(linewidth=some_large_number) and/or np.set_printoptions(threshold=some_large_number) approach can be useful but doesn't fix the problem if Jupyter's output window is itself too narrow.

The quickest solution I ended up with is inserting this line somewhere at the top of your notebook:

from IPython.core.display import HTML
display(HTML("<style>pre { white-space: pre !important; }</style>"))

If you want to change this setting for all of your notebooks, you'll need to mess around with the custom.css config file for Jupyter as discussed here.

I wasted too much time figuring this out. Hopefully I can help some of you figure it out quicker!

like image 168
Eric Le Fort Avatar answered Sep 27 '22 00:09

Eric Le Fort


Iterating on Eric's response slightly, here's a self-contained version that only creates horizontally-scrolling outputs when you want it to, rather than enabling it notebook-wide:

from IPython.display import display, HTML
from pprint import pformat
def boxprint(*args):
    for arg in args:
        display(HTML('<pre style="white-space: pre !important;">{}</pre>'.format(pformat(arg))))
like image 42
Max Avatar answered Sep 24 '22 00:09

Max