An example Jupyter Notebook

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

Markdown

Configuration

The MyST-NB parser derives from [the base MyST-Parser](myst:intro/get-started>, 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_admonition_enable = True
myst_amsmath_enable = True
myst_html_img_enable = True
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](../_static/logo.png)

myst-nb logo

With the myst_html_img_enable=True (see here), you can even add HTML img tags with attributes:

<img src="../_static/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 set myst_amsmath_enable = True 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([[22.73346286, 81.77998561, 22.95639968, 30.87236781, 85.72754876,
        31.37649989, 84.89344232, 52.80725328,  6.03064868, 43.55871349],
       [99.4237149 , 87.5282205 , 11.48091138, 88.78564781,  9.6204348 ,
        97.24640609, 55.43608268, 26.23661011, 30.1832214 , 32.38475711],
       [41.36065511, 90.51948223,  4.48482969, 27.72822225, 28.35742287,
        21.6269233 , 61.38803215, 74.12574042, 27.22183411, 88.87823616]])

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 22.733463 99.423715 41.360655
1 81.779986 87.528220 90.519482
2 22.956400 11.480911 4.484830
3 30.872368 88.785648 27.728222
4 85.727549 9.620435 28.357423

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)
<ipython-input-4-8f5b3a77886c> in <module>
      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 0x7f604f217208>
../_images/basic_9_1.png

1

This notebook can be downloaded as basic.ipynb and basic.md