Hiding cell contents

You can use Jupyter Notebook cell tags to control some of the behavior of the rendered notebook. This uses the sphinx-togglebutton package to add a little button that toggles the visibility of content.1

Hiding code cells

You can use cell tags to control the content hidden with code cells. Add the following tags to a cell’s metadata to control what to hide in code cells:

  • hide-input tag to hide the cell inputs

  • hide-output to hide the cell outputs

  • hide-cell to hide the entire cell

For example, we’ll show cells with each below.

Here is a cell with a hide-input tag. Click the “toggle” button to the right to show it.

# This cell has a hide-input tag
fig, ax = plt.subplots()
ax.scatter(*data, c=data[0], s=data[0])
<matplotlib.collections.PathCollection at 0x7f58d9e44748>
../_images/hiding_3_1.png

Here’s a cell with a hide-output tag:

# This cell has a hide-output tag
fig, ax = plt.subplots()
ax.scatter(*data, c=data[0], s=data[0])
<matplotlib.collections.PathCollection at 0x7f58d9d46b38>
../_images/hiding_5_1.png

And the following cell has a hide-cell tag:

# This cell has a hide-cell tag
fig, ax = plt.subplots()
ax.scatter(*data, c=data[0], s=data[0])
<matplotlib.collections.PathCollection at 0x7f58d9cb1b00>
../_images/hiding_7_1.png

Hiding markdown cells

There are two ways to hide markdown cells. First, you can add the hide-input cell metadata. This triggers the same hiding behavior described above for code cells.

Note

This cell was hidden by adding a hide-input tag to it!

You may also use a Sphinx directive to hide specific markdown content. This is possible by adding the .toggle class to any block-level directive that will allow for classes. For example, to the container, note, or admonition directives.

For example, the hidden block below

This cell was hidden with the toggle class

Wow, a hidden block! ✨✨

Is generated with the following code:

```{admonition} This cell was hidden with the toggle class
:class: toggle
Wow, a hidden block! ✨✨
```

Don’t add headings to toggle-able sections

Note that containers for markdown (like notes, or this container directive) cannot have their own headings (ie, lines that start with #. If you’d like to use headings, do one of the following:

  • Use bolded text if you want to highlight sections of a toggle-able section.

  • Use an admonition directive to control the title of the message box (that’s what this message box uses). Like so:

    ```{admonition} my admonition title
    My admonition content
    ```
    

Removing parts of cells

Sometimes, you want to entirely remove parts of a cell so that it doesn’t make it into the output at all. To do this, you can use the same tag pattern described above, but with the word remove_ instead of hide_. Use the following tags:

  • remove-input tag to remove the cell inputs

  • remove-output to remove the cell outputs

  • remove-cell to remove the entire cell

Here is a cell with a remove-input tag. The inputs will not make it into the page at all.

<matplotlib.collections.PathCollection at 0x7f58d9c185f8>
../_images/hiding_13_1.png

Here’s a cell with a remove-output tag:

# This cell has a remove-output tag
fig, ax = plt.subplots()
points = ax.scatter(*data, c=data[0], s=data[0])

And the following cell has a remove-cell tag (there should be nothing below, since the cell will be gone).


1

This notebook can be downloaded as hiding.ipynb and hiding.md