41

Is there a way to hide a single code cell's output in Google Colab?

No one needs to see the sea of pip logs when we install things:

enter image description here

If you look at the screenshot you'll see I tried to apply one of the solutions from this similar Stack Overflow question: https://stackoverflow.com/a/48084050/1762493

I tried applying TagRemovePreprocessor.remove_single_output_tags as a #comment, a @@Magic, and a !command but those don't work with this line.

I checked Colab's "Welcome" and "Resource" notebooks but didn't notice anything there for deeper notebook settings: https://colab.research.google.com/notebooks/welcome.ipynb

Is this even possible?

korakot
  • 37,818
  • 16
  • 123
  • 144
Mikeumus
  • 3,570
  • 9
  • 40
  • 65

5 Answers5

60

In this case you can just use

!pip install -q gwpy

In general, you can start the cell with %%capture

%%capture
# the rest of your code
korakot
  • 37,818
  • 16
  • 123
  • 144
  • 4
    This `%%capture` is what answers the title of the question - the tittle is general, although the example in the content of the question is specific to `pip`. – Martin Apr 24 '21 at 12:10
29

I found this answer and applied it successfully: https://serverfault.com/a/41968/328943

Simply adding &> /dev/null to the tail of any command will silence its output outside of any errors that may arise.

Like so:

!pip install gwpy &> /dev/null

Mikeumus
  • 3,570
  • 9
  • 40
  • 65
14

This can be done by adding --quiet at the end. Thus for your case

!pip install gwpy --quiet
DaCard
  • 511
  • 4
  • 15
10

Just select the cell and press Ctrl+ M O. This will hide the output of the cell. This option is also available under the view tab.

Vikash Kumar
  • 296
  • 3
  • 10
0

Two ways:

  • Manage colab notebook by splitting it into sections, which can hide group of cells together with output. For instance, at the beginning I have "INIT" section, which loads pip's and necessary data from disk. One click, and initialized notebook is ready for work.
  • Stable code I hide with snippet at the beginning of each cell.

#@title 'What does it do'

If needed, output can be cleared too.

Other useful snippets can be found here

Nikolai Zaitsev
  • 303
  • 5
  • 9