An example Jupyter Notebook

This notebook is a demonstration of directly-parsing Jupyter Notebooks into Sphinx using the MyST parser.1



The MyST-NB parser derives from the base MyST-Parser, and so all the same configuration options are available. See the MyST configuration options for the full set of options, and MyST syntax guide for all the syntax options.

To build documentation from this notebook, the following options are set:

myst_enable_extensions = [
myst_url_schemes = ("http", "https", "mailto")


As you can see, markdown is parsed as expected. Embedding images should work as expected. For example, here’s the MyST-NB logo:

![myst-nb logo](../_static/logo-wide.svg)

myst-nb logo

By adding "html_image" to the myst_enable_extensions list in the sphinx configuration (see here), you can even add HTML img tags with attributes:

<img src="../_static/logo-wide.svg" alt="logo" width="200px" class="shadow mb-2">

Because MyST-NB is using the MyST-markdown parser, you can include rich markdown with Sphinx in your notebook. For example, here’s a note admonition block:


Wow, a note! It was generated with this code (as explained here):

**Wow**, a note!

If you wish to use “bare” LaTeX equations, then you should add "amsmath" to the myst_enable_extensions list in the sphinx configuration. This is explained here, and works as such:

\frac {\partial u}{\partial x} + \frac{\partial v}{\partial y} = - \, \frac{\partial w}{\partial z}

2x - 5y &=  8 \\
3x + 9y &=  -12
(1)\[\begin{equation} \frac {\partial u}{\partial x} + \frac{\partial v}{\partial y} = - \, \frac{\partial w}{\partial z} \end{equation}\]
\[\begin{align*} 2x - 5y &= 8 \\ 3x + 9y &= -12 \end{align*}\]

Also you can use features like equation numbering and referencing in the notebooks:

$$e^{i\pi} + 1 = 0$$ (euler)
(2)\[e^{i\pi} + 1 = 0\]

Euler’s identity, equation (2), was elected one of the most beautiful mathematical formulas.

You can see the syntax used for this example here in the MyST documentation.

Code cells and outputs

You can run cells, and the cell outputs will be captured and inserted into the resulting Sphinx site.

__repr__ and HTML outputs

For example, here’s some simple Python:

import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(3, 100) * 100
data[:, :10]
array([[25.73730686, 11.99457912, 58.32601927, 40.48021776, 37.9268166 ,
        88.45584229, 99.91205417, 13.88448428, 47.91362829, 49.78595821],
       [19.36499526, 45.54537294, 39.21832227, 86.03092227, 48.07448462,
        56.16827795, 98.29240576, 40.29702216, 89.66167231, 23.35649292],
       [60.07072005, 46.52238832, 25.71657058, 33.31623131, 38.92002554,
        80.68950153,  6.46911335, 80.87941901,  0.98173058, 90.26460515]])

This will also work with HTML outputs

import pandas as pd
df = pd.DataFrame(data.T, columns=['a', 'b', 'c'])
a b c
0 25.737307 19.364995 60.070720
1 11.994579 45.545373 46.522388
2 58.326019 39.218322 25.716571
3 40.480218 86.030922 33.316231
4 37.926817 48.074485 38.920026

as well as math outputs

from IPython.display import Math
Math(r"\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}")
\[\displaystyle \sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}\]

This works for error messages as well:

print("This will be properly printed...")
This will be properly printed...
NameError                                 Traceback (most recent call last)
<ipython-input-4-8f5b3a77886c> in <module>
      1 print("This will be properly printed...")
----> 2 print(thiswont)

NameError: name 'thiswont' is not defined


Images that are generated from your code (e.g., with Matplotlib) will also be embedded.

fig, ax = plt.subplots()
ax.scatter(*data, c=data[2])
<matplotlib.collections.PathCollection at 0x7f93b80c4b10>


This notebook can be downloaded as basic.ipynb and