Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Wrapping in Collaboratory Google results

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.

enter image description here

Does anyone know how to activate text wrapping to see all text without using scrollbar?

Thanks in advance.

Regards,

like image 303
DCA Avatar asked Nov 16 '19 11:11

DCA


People also ask

What is the use of wrapping line?

In text display, line wrap is continuing on a new line when a line is full, so that each line fits into the viewable window, allowing text to be read from top to bottom without any horizontal scrolling.

How do you comment multiple lines in Google Colab?

You can select the lines of code and press ( Ctrl + / ) to comment or uncomment your selected lines of code. You can also use triple single quotes ( ''' ) at the start and end of the code block you are interested to comment out. Save this answer.

How do you go to the next line in a Google text Colab?

There are two options here either you can just press the “Enter” key and leave a line of space in the between or you can use the br tag also called a line break.


1 Answers

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.

Instead, put this in the first cell of your notebook.

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)

like image 106
Bon Ryu Avatar answered Sep 20 '22 18:09

Bon Ryu