22

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

5 Answers 5

26

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.display import display, 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!

Sign up to request clarification or add additional context in comments.

Comments

2

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))))

Comments

2

As written by Eric, changing the white-space property to pre prevents automatic wrapping of output cells, and thus forces an horizontal scrollbar for all wide outputs of the current notebook.

Unfortunately, changing this property also prevents automatic wrapping when editing input cells, which is particularly annoying when editing markdown cells. Even worse: this behavior is not limited to the current notebook, but happens on all open tabs when using JupyterLab, for instance.

The workaround I propose, is to define a function to switch between the standard behavior white-space: pre-wrap and the scrollbar behavior white-space:pre. This simple function does the job:

def hscroll(activate=True):
  """activate/deactivate horizontal scrolling for wide output cells"""
  from IPython.display import display, HTML
  style = ('pre-wrap','pre')[activate] # select white-space style
  display(HTML("<style>pre {white-space: %s !important}</style>" % style))

3 Comments

This works great. Just note that sometimes if you deactivate scrolling with this function and then change the output again, the scrollbar will appear again. You need to run all of your cells every time you change the window if you don't want the scrollbars.
@liakoyras: I haven't tried with Jupyter Notebook, but when using Jupyter Lab, I have never observed this behavior. Do you have a downloadable notebook that presents such a bug?
Unfortunately not. It happened for a few times but then the behavior was normal. Don't have the time to test it more now.
0

On hovering right below

out[]:

in notebook you see "scroll output" . On clicking anywhere in that area you get your output scrollable both horizontally and vertically.

1 Comment

Clicking on that will toggle vertical scrolling, but I don't see a way for horizontal scrolling. Are we missing something?
0

If you are using a Mac, just scroll right/left with one touch, it will start scrolling automatically.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.