4.1.3. Notebooks with MyST Markdown#

Jupyter Book also lets you write text-based notebooks using MyST Markdown. See the Notebooks with MyST Markdown documentation for more detailed instructions. This page shows off a notebook written in MyST Markdown.

An example cell#

With MyST Markdown, you can define code cells with a directive like so:

print(2 + 2)
4

When your book is built, the contents of any {code-cell} blocks will be executed with your default Jupyter kernel, and their outputs will be displayed in-line with the rest of your content.

You can also hide cell elements of a page. For example, notice the cell below contains the hide-cell tag:

Hide code cell content
import numpy as np
import matplotlib.pyplot as plt
plt.ion()

data = np.random.randn(2, 100)
fig, ax = plt.subplots()
ax.scatter(*data, c=data[1], s=100*np.abs(data[0]));
../_images/6cc167995b3769792716aa465856e7e23cbab54475851076089efc7b73c486cf.png

Similarly, you can hide just the input code or the output with the hide-input and hide-output tags respectively.

To add these tags to your code cells, you can use the following syntax:

```{code-cell}
:tags: [hide-input]

print("This code's input will be hidden!")
```

```{code-cell}
:tags: [hide-output]

print("This code's output will be hidden!")
```

```{code-cell}
:tags: [hide-cell]

print("Both input and output will be hidden!")
```

The cell tags control the visibility of different parts of your code cells, and you can use them to customize how your notebook content is displayed.

See also

Jupyter Book uses Jupytext to convert text-based files to notebooks, and can support many other text-based notebook files.

Create a notebook with MyST Markdown#

MyST Markdown notebooks are defined by two things:

  1. YAML metadata that is needed to understand if / how it should convert text files to notebooks (including information about the kernel needed). See the YAML at the top of this page for example.

  2. The presence of {code-cell} directives, which will be executed with your book.

That’s all that is needed to get started!

Quickly add YAML metadata for MyST Notebooks#

If you have a markdown file and you’d like to quickly add YAML metadata to it, so that Jupyter Book will treat it as a MyST Markdown Notebook, run the following command:

jupyter-book myst init path/to/markdownfile.md