An example Jupyter Notebook#

This notebook is a demonstration of directly-parsing Jupyter Notebooks into Sphinx using the MyST parser.[^download]

Markdown#

Configuration#

https://myst-parser.readthedocs.io/en/latest/using/intro.html#getting-started

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

myst_enable_extensions = [
    "amsmath",
    "colon_fence",
    "deflist",
    "dollarmath",
    "html_image",
]
myst_url_schemes = ("http", "https", "mailto")

Syntax#

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](../img/unitary_fund_logo.png)

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="../img/unitary_fund_logo.png" alt="logo" width="200px" class="shadow mb-2">
logo

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:

Note

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

:::{note}
**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:

\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*}
(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([[39.1685791 , 71.43317005, 19.82374029, 98.37974446, 86.73797395,
        82.13810141, 91.6519104 ,  4.1328172 , 78.46473519, 75.55439985],
       [ 5.5851028 , 51.11848789, 46.93521012, 24.89225982, 69.29491496,
        20.39219943, 60.88912282, 63.80896751, 44.9439873 , 74.46182648],
       [89.60308303, 52.3838682 , 41.7794128 , 69.4348838 , 78.81698223,
        89.12967504,  2.52611362, 88.34343443, 61.21796044,  7.38461059]])

This will also work with HTML outputs

import pandas as pd
df = pd.DataFrame(data.T, columns=['a', 'b', 'c'])
df.head()
a b c
0 39.168579 5.585103 89.603083
1 71.433170 51.118488 52.383868
2 19.823740 46.935210 41.779413
3 98.379744 24.892260 69.434884
4 86.737974 69.294915 78.816982

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...")
print(thiswont)
This will be properly printed...
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 2
      1 print("This will be properly printed...")
----> 2 print(thiswont)

NameError: name 'thiswont' is not defined

Images#

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 0x7f8866593d90>
../_images/2014689b3dbc84007dba465b430b9cb818976634ce4f424c50e1ea90f0662f4e.png

Thumbnail for the Notebook#

To add a thumbnail for an example notebook, first add the thumbnail image file to docs/source/_thumbnails. Next, modify the docs/source/conf.py to include the example and thumbnail in the nbsphinx_thumbnails dictionary at the end of the file. The sample below contains both a generic template and an actual example.

nbsphinx_thumbnails = {
    "examples/{EXAMPLE_FILENAME_WITHOUT_.md}": "_static/{THUMBNAIL_FILENAME_WITH_EXTENSION}",
    "examples/hamiltonians": "_static/vqe-cirq-pauli-sum-mitigation-plot.png"
    }