Hiding or removing code blocks or entire cells

It's possible to control which cells show up in your final book pages. For example,you may want to display a complex visualization to illustrate an idea, but don'twant the page to be cluttered with a large code cell that generated the viz. In othercases, you may want to remove a code cell entirely.

In this case, you have two options:

  • Hiding the inputs of a code cell will hide the cell's contents and providea button that lets readers reveal the content.
  • Removing the inputs (or the entire cell) will prevent the contents frommaking it into your book's HTML. It will be entirely gone (though still present inthe .ipynb file) In both cases, Jupyter Books uses notebook cell tags to determine which code cells to hide.To make this process easier to manage, we recommend theJupyterLab Cell Tags extension

Hiding inputs and displaying a button to show them

If you add the tag hide_input to a cell, then Jupyter Book will hide the cell butdisplay the outputs.

Here's an example of cell metadata that would trigger the "hide code" behavior:

  1. {
  2. "tags": [
  3. "hide_input",
  4. ]
  5. }

Jupyter Book will display a small button to the right of the location that used to hold the hidden contents. If a user clicks the button,the contents will be displayed. For example, see the cell below contains the hide_inputtag:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. plt.ion()
  4.  
  5. data = np.random.randn(2, 100)
  6. fig, ax = plt.subplots()
  7. ax.scatter(*data, c=data[1], s=100*np.abs(data[0]));

Hiding code blocks or entire cells - 图1

Note how we only see the output by default. Now try clicking the buttonto the right of the empty spot above!

Note that this button only shows up for cells where you've hidden the code:

  1. # However, this code cell does not have the tag, and will show up!
  2. print("This cell will show up!")
  1. This cell will show up!

Removing inputs from the HTML

In the above examples, we are only hiding the inputs, with the optionthat readers can reveal them if they wish. However, if you'd like to completely removethe inputs, so that their contents do not make it into the book's HTML, you mayuse the following tag:

To remove the inputs of a cell:

  1. {
  2. "tags": [
  3. "remove_input",
  4. ]
  5. }

The following cell demonstrates removing inputs. Note that inthis case, there is no button available to show the input contents,the entire input cell is gone!

Removing inputs

The following cell has its inputs removed

Hiding code blocks or entire cells - 图2

Removing outputs from the HTML

Similar to hiding inputs, it is also possible to hide the outputsof a cell.

To remove the outputs of a cell:

  1. {
  2. "tags": [
  3. "remove_output",
  4. ]
  5. }

Removing an entire cell

You can also remove both the inputs and outputs of a cell, in which case itwon't show up in your book at all. These cells remain in the notebook file itself,so they'll show up if readers click on a JupyterHub or Binder link from a page.

To remove both the inputs and outputs of a cell, add the tag remove_cell to the tagsof the cell. Here's an example of cell metadata that would trigger the "remove cell" behavior:

  1. {
  2. "tags": [
  3. "remove_cell",
  4. ]
  5. }

These cells will be entirely removed from each book page - remember that if you'd like tooptionally display the inputs of a cell instead, you should use the hide_input tag.

For example, there's a cell below this one that won't make it into the final book,because it has been removed!

Removing markdown cells

Sometimes, you have extra Markdown in your documents that isn't meant for thereader. For example, if you want to organize your notebook based on developer-relevantinformation (like "# Import packages") but you don't want the reader to see this.

In this case, you can use the remove_cell pattern described above as well.

Here's an example of markdown cell metadata that would trigger the "hide text" behavior:

  1. {
  2. "tags": [
  3. "remove_cell",
  4. ]
  5. }

Removing empty cells

You don't need to do anything to remove empty cells from your pages.Jupyter Book will remove these automatically. Any cell with _only_whitespace will be removed.

For example, in the notebook for this page there are two cells above this one. Onecell with whitespace, and another cell with a few line-breaks. Both are gone inthe final output.

This page was created by The Jupyter Book Community