jbex-example: 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)

from matplotlib import rcParams, cycler
import matplotlib.pyplot as plt
import numpy as np
print(1)
plt.ion()
plt.plot((0,0),(1,1))
plt.show()
4
1
_images/jbex-markdown-notebooks_1_2.png

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.

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
def test():
  pass
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 200)
y = np.sin(x)
fig, ax = plt.subplots()
print(123455)
ax.plot(x, y, 'b-', linewidth=2)
123455
[<matplotlib.lines.Line2D at 0x7f6e66c421d0>]
_images/jbex-markdown-notebooks_4_2.png
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 200)
y = np.sin(x)
fig, ax = plt.subplots()
print(123455)
ax.plot(x, y, 'b-', linewidth=2)
123455
[<matplotlib.lines.Line2D at 0x7f6e66c3a6d0>]
_images/jbex-markdown-notebooks_5_2.png