By default longer lines of text in the output cells of a Jupyter notebook will be wrapped. How to stop this behaviour?
-
do you want a horizontal scroll bar or the output cell width's to adapt to its content? – Alexis Nov 26 '18 at 23:04
-
both options would be acceptable. a horizontal scroll bar would be better. – SomJura Nov 29 '18 at 10:53
4 Answers
If you don't want to mess around with a config file, you can modify the behaviour of a notebook ad hoc by calling the IPython.core.display function. Then add the CSS suggested by @atevm:
from IPython.core.display import display, HTML
display(HTML("<style>div.output_area pre {white-space: pre;}</style>"))
for line in range(5):
for num in range(70):
print(f" {num}", end="")
print()
- 209
- 2
- 5
-
This worked to stop the line wrap, but the cell displaying the data doesn't extend (can't scroll it side to see to actually SEE the unwrapped data :D ) – RightmireM Aug 03 '21 at 11:59
-
@RightmireM I'm not seeing that issue. Side scroll works fine on my Mac in Chrome, at least. – Steve Bond Aug 05 '21 at 13:43
-
1Wish I could up arrow this response twice! Add that import and display line at the top of your jupyter notebook, and from there out you sparkdf.show(5,False) and it won't wrap! – Tony Fraser Aug 30 '21 at 17:49
You can use an html magic command. Check the CSS selector is correct, by inspecting the output cell, then edit the below accordingly.
%%html
<style>
div.output_area pre {
white-space: pre;
}
</style>
- 2,206
- 1
- 11
- 7
I was able to solve this problem by adding a simple CSS rule to the custom/custom.css file in my Jupyter user configuration:
/*Disable code output line wrapping*/
div.output_area pre {
white-space: pre;
}
The result:
The div.output_area pre selects the pre preformated text areas of the code output areas for the rule (set of css properties). The white-space property states how the browser should display white spaces in the selected HTML elements with the pre value the browser only breaks at new line characters \n and <br> elements.
This CSS renders well (with a fine horizontal scrollbar) for my Firefox v70.0 and Chorme v78.0.3904.97, according to Can I Use the white-space: pre property and value should work on all modern desktop browsers.
You can find out where your configuration resides by running the following shell command:
jupyter --config
If you want make further style modifications just play around with the inspector of your favorite browser on Jupyter Notebook tab. where you can modify the CSS without permanent effects.
- 701
- 11
- 26
-
-
This still doesn't work for long numpy arrays. How could I solve that? – Jordan Kohn Apr 21 '21 at 02:59
-
@JordanKohn Strange, for me numpy arrays are displayed already wrapped even with the default CSS and config. Can you share a screenshot? – atevm Apr 21 '21 at 08:27
-
https://stackoverflow.com/a/65341534/10733210 this actually fixed my problem – Jordan Kohn Apr 21 '21 at 16:23
-
This was so painful but I was having problems that something override this. apparently it's because of `log = logging.getLogger("rich")`, can't be sure though, just a note for future dev. In the end I output what I have and opened a new notebook to read whatever I needed. – Viet Than Apr 06 '22 at 20:19
I can't comment so I have to answer: maybe there's something different with the last versions of Jupyter. If the accepted answer doesn't work, you can try with "jp-OutputArea-output" instead of "div.output_area"; for example
from IPython.core.display import display, HTML
display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
And if you have a dark-mode browser and you don't like the resulting lighter scrollbars, you can try to set the dark mode in Jupyter adding
display(HTML("<style>:root {color-scheme: dark;}</style>"))
See: How do I switch to Chromes dark scrollbar like GitHub does?
- 21
- 6