如何在Google Colaboratory中隐藏单元格内容?
Hey there! I totally get wanting to replicate those handy collapsible solution cells you see in Colab tutorials—they keep your notebook tidy and let you reveal answers only when you need them. Here are two straightforward ways to do it:
Method 1: Use Colab's Built-in Section Folding
This is great for grouping multiple cells under a collapsible header:
- Add a new text cell above the cells you want to hide, and format it as a heading (use
#,##,###etc.—the level doesn’t matter as long as it’s a heading). - Look for the small arrow icon that appears to the left of the heading once you’ve saved it. Click that arrow, and all cells below until the next heading of the same or higher level will collapse neatly under it. Click again to expand them.
Method 2: Create a Click-to-Reveal Collapsible Cell (Just Like Tutorials)
If you want a single, self-contained collapsible cell (the exact style you see in the Creating and Manipulating Tensors tutorial), use HTML with Colab’s %%html magic command:
- Add a new code cell where you want the hidden content to live.
- Paste this code into the cell, then run it:
%%html <details> <summary>Click to Show/Hide Solution</summary> <div style="padding: 10px; margin-top: 8px;"> <!-- Replace this part with your content --> <pre><code># Example hidden code import tensorflow as tf tensor = tf.constant([1, 2, 3]) print(tensor)</code></pre> <!-- Or add plain text if you prefer --> <p>Your written explanation or solution can go here too!</p> </div> </details>
- Once you run the cell, you’ll see a clickable summary line. Click it to expand and reveal your hidden solution, click again to tuck it away.
Pro tip: If you want to hide an existing code or text cell, you can wrap its content inside the <div> section of the HTML above—just make sure to format code with <pre><code> tags so it displays properly.
内容的提问来源于stack exchange,提问作者the_cat_lady

