Colabtools: Disabling auto-scroll of outputs

Created on 6 May 2019  路  5Comments  路  Source: googlecolab/colabtools

Repeating a user question here:
Is it possible to disable auto-scrolling of large outputs to allow the entire output to be visible?

Answer:
Yes- the maximum height is 1000px but this can be overriden by executing the Javascript:

google.colab.output.setIframeHeight(0, true, {
  maxHeight: 5000,
})

An example in Python:

from IPython.display import Javascript
display(Javascript('''google.colab.output.setIframeHeight(0, true, {maxHeight: 5000})'''))

for i in range(200):
  print(i)

Most helpful comment

Inspired by numerous stackoverflow posts, I have come to a solution, which I have also shared on stackoverflow: https://stackoverflow.com/questions/58890109/line-wrapping-in-collaboratory-google-results/61401455#61401455

The solution I have come to is to put the following in the first cell of the notebook. Then every cell afterwards will execute the css snippet inside the set_css() function.

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)

All 5 comments

If I want to shrink the width of the output then? To keep it within the width of the window for example.

I've tried various variants of below code but without success.

from IPython.display import Javascript display(Javascript('''google.colab.output.setIframeWidth(0, true, {maxWidth: 500})'''))

Is there an alternative for this?

The iframe itself should always be limited to the width of the window, I'm guessing you're looking to wrap text within the iframe rather than horizontally scroll?

An example of that is:
https://colab.sandbox.google.com/gist/blois/a1e3918fe80487c8d539190eee220028/pre-wrapping.ipynb

Otherwise you should be able to use HTML styling within the outputframe to further constrain the rendering sizes.

You're correct. I apologise my rather ambiguous explanation. In either way, the approach in the link worked. Thank you very much @blois!

Is there a solution for text wrapping of output that can be applied once at top of the notebook (i.e. to avoid a horizontal scroll bar)? Or perhaps creating a 'custom.css' configuration file that gets read during start up of a colab notebook?

As far as I can tell, the solution in pre-wrapping.ipynb example must be used in every cell that prints horizontally long outputs.

One of the solutions here ( https://stackoverflow.com/questions/32156248/how-do-i-set-custom-css-for-my-ipython-ihaskell-jupyter-notebook ) suggests editing the local file ~/.jupyter/custom/custom.css.

I tried creating a file '/usr/local/share/jupyter/custom/custom.css' from within a colab notebook, but it doesn't get read prior to the notebook starting up, and is thus useless.

Thank you.

Inspired by numerous stackoverflow posts, I have come to a solution, which I have also shared on stackoverflow: https://stackoverflow.com/questions/58890109/line-wrapping-in-collaboratory-google-results/61401455#61401455

The solution I have come to is to put the following in the first cell of the notebook. Then every cell afterwards will execute the css snippet inside the set_css() function.

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)
Was this page helpful?
0 / 5 - 0 ratings