Reputation: 513
Working on Google Collaboratoy (colab) as Notebook, some cell results a long line text which is bigger than the screen resolution, so it is shown a scrollbar with no wrapping.
Does anyone know how to activate text wrapping to see all text without using scrollbar?
Thanks in advance.
Regards,
Upvotes: 38
Views: 27915
Reputation: 11
Just to add on, for thinking models like DeepSeek R1, I'd say use this because it makes it easier to read the output.
from IPython.core.display import display, HTML
display(HTML('''
<style>
pre {
white-space: pre-wrap !important; /* Wrap text */
word-break: break-word !important; /* Prevent long words from overflowing */
}
.output_result {
max-height: none !important; /* Remove scroll */
}
.output_area {
overflow-y: visible !important; /* Prevent vertical scrolling */
}
</style>
'''))
Upvotes: 0
Reputation: 193
To make text wrap in a colab notebook, you can use the fill
method from the textwrap
python standard library.
from textwrap import fill
print(fill("foo bar " * 12, width=60))
Output:
foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo
bar foo bar foo bar foo bar foo bar
Upvotes: 0
Reputation: 951
Extension of Bon Ryu's answer, which ensures cell can be executed multiple times, but same callback registered only once. Also fixes missing args exception.
from warnings import warn
from IPython import get_ipython
def enable_word_wrap(*args, **kwargs):
display(HTML('''
<style>
pre {
white-space: pre-wrap;
}
</style>
'''))
def register_ipython_callback_once(event_name, cb):
ev = get_ipython().events
cb_unregs = [cb_old for cb_old in ev.callbacks[event_name] if cb_old.__name__ == cb.__name__]
if len(cb_unregs) == 1 and cb.__code__ == cb_unregs[0].__code__:
return
for cb_old in cb_unregs:
warn(f'Removing unexpected callback {cb_old}.')
ev.unregister(event_name, cb_old)
ev.register(event_name, cb)
register_ipython_callback_once('pre_run_cell', enable_word_wrap)
Possibly delete and merge later this answer into original if useful. Not sure if this should be a separate QA, gist link, or future edit of Bon Ryu's answer..
Upvotes: 0
Reputation: 109
from IPython.display import HTML, display
def set_css():
display(HTML('''
<style>
pre {
white-space: pre-wrap;
}
</style>
'''))
get_ipython().events.register('pre_run_cell', set_css)
As Bon Ryu mentioned above, this should solve the issue. It will wrap your output properly
Upvotes: 8
Reputation: 895
I use the following snippet:
from IPython.display import HTML, display
def my_css():
display(HTML("""<style>table.dataframe td{white-space: nowrap;}</style>"""))
get_ipython().events.register('pre_run_cell', my_css)
Upvotes: 1
Reputation: 708
Normally on my own machine, I put this the following css snippit in the ~/.jupyter/custom/custom.css
file.
pre {
white-space: pre-wrap;
}
But, the above does not work for google colab: I tried creating a file /usr/local/share/jupyter/custom/custom.css
, but this didn't work.
from IPython.display import HTML, display
def set_css():
display(HTML('''
<style>
pre {
white-space: pre-wrap;
}
</style>
'''))
get_ipython().events.register('pre_run_cell', set_css)
Explanation: As described in Google Colab advanced output , get_ipython().events.register('pre_run_cell', <function name>)
...
defines an execution hook that loads it [our custom
set_css()
function in our case] automatically each time you execute a cell
My interpretation is that you need to specify 'pre_run_cell'
as the first argument in the events.register
, which tells the events.register
function that you wish to run your custom set_css()
function before the contents of the cell is executed.
This answer was inspired by How to import CSS file into Google Colab notebook (Python3)
Upvotes: 45
Reputation: 40818
I create a function to help with that. It works with List and String.
def set_wrap(N=100):
''' create a wrap function for list '''
def wrap(obj):
s = str(obj)
out = '<pre>'
while True:
if len(s) < N:
out += s
break
i = s.rfind(' ', 0, N)
if i==-1:
i = N
out += s[:i]+"\n"
s = s[i:]
out += "</pre>"
return out
''' register it '''
Formatter = get_ipython().display_formatter.formatters['text/html']
Formatter.for_type(list, wrap)
Formatter.for_type(str, wrap)
You can use it by just calling set_wrap(80)
.
Upvotes: 0