the_cat_lady
the_cat_lady

Reputation: 951

How can I hide cell contents in Google Colaboratory?

I'm working through a tutorial in Google Colaboratory, and the author has handily hidden some of the solutions cells. When you click the hidden cell, it expands and becomes visible. How can I hide the cells?

An example is in this tutorial: Creating and Manipulating Tensors.

Hidden Cells example

Upvotes: 62

Views: 110504

Answers (19)

Harunduet
Harunduet

Reputation: 947

In the code area at Colab, add this line at the top

#@title don't show code till dblclick

your own code will be below:

print("hello world")

And After running the code area, you can see the title in Larger font at the top. Just double-click to hide/unhide enter image description here

Upvotes: 1

jarvis
jarvis

Reputation: 1183

None of the other answers worked for me. You can collapse cells by adding this to the top of the cell "#@title" and then double clicking on the white space that appears to the right of it.

#@title Your Title Here

Upvotes: 96

Muhammad Yasirroni
Muhammad Yasirroni

Reputation: 2167

Since no one mention metadata setups to hide a cell code in form environment, I will share my findings.

This use case is if you want to make a notebook file in GitHub that has a banner to be opened in Colab, and you want the notebook to hide some cell.


To hide a code cell you need to:

  1. Make the cell into form cell by adding #@title on the very top of the cell.

  2. add below metadata to the cell (you need to use plain text editor to do this):

       "metadata": {
        "cellView": "form",
        "colab": {}
       },
    

Upvotes: 1

damiendonnelly
damiendonnelly

Reputation: 382

Right click on the side bar with the play button

hide code

Upvotes: 1

Abhishek
Abhishek

Reputation: 1

just go on the top left corner (table of contents) then tap on +section. now on the left of the "new section" on your notebook there is a sign for the collapse of the cell and you are done.

Upvotes: 0

Pratik Singh
Pratik Singh

Reputation: 1

Just Make a new text cell and when writing the text include a # in the front it will make it a collapsable cell

without #

1

Use # infront of the title you want to enter and you are done

UPVOTE PLS

with #

2

Upvotes: -1

Toma
Toma

Reputation: 599

Here is the hot key for ya:

ctrl + "

After setting up the text part in Colab like what others have said, you can press control and quote key at the same time and collapse whichever cell/section you want.

Upvotes: -1

ctrl-alt-delor
ctrl-alt-delor

Reputation: 7745

To collapse/hide all cells without seeing them,

go to: menu → view → collapse sections, and click.

Upvotes: 4

Harsh Malra
Harsh Malra

Reputation: 71

To hide multiple cells.

  1. Create Text cell, by clicking +Text button.
  2. Give a title, like "# Setup".
  3. Then on the left, the triangle button will come, to hide all cells below that.

Upvotes: 1

Tomas Giro
Tomas Giro

Reputation: 4267

The black triangle, that makes it possible to fold and unfold sections, appears when you create a section (which is equivalent to creating a title).

You can create a section by creating a text cell that starts with # <your section title>.

This is how you create title in Markdown: # This is a title ## This is a smaller title ### This is even a smaller title...

You can fold and unfold sections by clicking on the triangle next to their title.

This image illustrates how to create a section:

This image illustrates how to create a part

This image is the result of the previous image (note the magic triangle on the left):

This image is the result of the previous image note the triangle on the left

Upvotes: 16

16180
16180

Reputation: 127

  1. Open a text cell
  2. Write text beginning with a # i.e #text
  3. Shift + Enter

The text cell will become a collapse/expand tool and the cells below this text cell can be collapsed/expanded now

Upvotes: 3

Shreyan
Shreyan

Reputation: 17

Just Press Ctrl + ] to collapse and Ctrl + [ to expand

Upvotes: 0

DrGG
DrGG

Reputation: 31

It took me a while to figure out...but it is actually quite easy.

Manually collapse the section you would like manually, then go to "file/save". It will preserve the layout (including which section is collapsed) next time you open the notebook.

Hope a bit helpful!

Upvotes: 3

Manish
Manish

Reputation: 31

The most easiest way is to define "section" between your code cells. Then next to the title there an arrow using which you can hide all cells inside the section

Hide using sections:

hide using sections


Select multiple cells using SHIFT and your mouse click.

then do right click to get the Hide Cell option like this

Show hide cell option:

show hide cell option

Upvotes: 3

Beth Lane
Beth Lane

Reputation: 81

I realise this question was posted some time ago but as a newbie to Google Colab myself I thought it would be helpful to share what I have found useful when 'collapsing' sections of code and generally organising code within my notebooks.

To add a collapsible section: proceed title with '#', all code below will fall within this title and can be collapsed. I.e. '#Import libraries' will produce:

Collapsible Colab title

To add collapsible sub-sections (within the main header) simply add another '#', and so on. So, '##Check tf version' would fall within '#Import libraries'

I hope this is helpful!

Upvotes: 8

henleo
henleo

Reputation: 11

If you are asking, how to replicate this:
What you can see in your screenshot is just what happens if you put text cells with a markup headings in between your code cells to create sections in your notebook. In this case that would simply be a text cell containing just # Solution above the two code cells.

You can hide and show the sections by clicking on the little triangle.

Upvotes: 1

guidoman
guidoman

Reputation: 1141

  1. You right-click on the area on the left of the cell (below the "Play" button) and choose "Add a form"
  2. You can enter a title for your cell after the #@title keyword (first line in your cell code)
  3. Right-click again in the same place and choose "Form > Hide code"

Upvotes: 22

If you need to hide code in your personal notebook, click at the gray vertical line (with 'run button') of the current cell and then check 'Hide code'

Upvotes: -1

korakot
korakot

Reputation: 40948

You can click at the “Triangle” in front of “Solution” heading. It will toggle between hide/show.

Upvotes: 2

Related Questions