MyST-NB
MyST-NB is a Sphinx extension that enables direct parsing and execution of Jupyter Notebooks (.ipynb) within Sphinx documentation. It builds upon the MyST Markdown parser, allowing users to write rich technical documentation that seamlessly integrates executable code, outputs, and MyST Markdown syntax. The library maintains an active development cycle, with new versions released regularly to enhance features and ensure compatibility with the latest Python and Sphinx versions.
Warnings
- breaking Breaking changes were introduced in v1.0.0, including significant changes to configuration option names. Many options that previously did not have a prefix now use `nb_` (e.g., `jupyter_execute_notebooks` became `nb_execution_mode`). `nb_render_priority` was removed and replaced by `nb_mime_priority_overrides` with a different format. The `dollarmath` MyST extension is no longer enabled by default.
- breaking The `ipywidgets` package was removed as a direct dependency in v1.0.0. If your documentation relies on interactive ipywidgets, they will no longer function unless `ipywidgets` is explicitly installed in your environment.
- breaking The internal AST structure and rendering plugin system were completely rewritten in v1.0.0 for compatibility with new Docutils functionality. Any custom renderers or direct manipulation of MyST-NB's internal Docutils AST nodes will likely break.
- gotcha MyST-NB automatically activates the `myst_parser` extension. Explicitly including `myst_parser` in your `extensions` list alongside `myst_nb` will cause conflicts or redundant processing.
- gotcha When MyST-NB processes Jupyter Notebooks (.ipynb), Sphinx error reporting for these files uses a special line number format: `<CELL_INDEX> * 10000 + LINE_NUMBER`. This can make pinpointing the exact line in a notebook cell challenging without understanding the format.
Install
-
pip install myst-nb
Imports
- myst_nb
extensions = ['myst_nb'] # in conf.py
- glue
from myst_nb import glue
Quickstart
import os
# --- conf.py (Sphinx configuration file) ---
# project = 'MyST-NB Quickstart'
# copyright = '2026, Your Name'
# extensions = [
# 'myst_nb',
# 'sphinx.ext.autodoc', # Example standard Sphinx extension
# ]
# # Optional: Configure notebook execution
# # nb_execution_mode = "auto" # 'auto', 'off', 'force', 'cache'
# # nb_execution_timeout = 60
# --- example.md (MyST Markdown file with code cells) ---
example_md_content = '''
# MyST-NB Example Document
This is a MyST Markdown file demonstrating executable code cells.
```{code-cell} python
import datetime
print("Hello from MyST-NB!")
print(f"Current time: {datetime.datetime.now()}")
```
You can also use the `glue` functionality to embed variables from executed cells.
```{code-cell} python
from myst_nb import glue
my_result = "This value was 'glued' from a Python cell."
glue("my_embedded_string", my_result)
```
The result embedded from the code cell is: {glue:text}`my_embedded_string`.
'''
# To run this, you would typically:
# 1. Create a Sphinx project (e.g., `sphinx-quickstart`)
# 2. Modify `conf.py` as commented above
# 3. Create `example.md` with the content above
# 4. Build with `sphinx-build -b html . _build`
# This snippet simulates saving the content to files for demonstration.
# In a real Sphinx project, these files would exist in your source directory.
# (Not actually running sphinx-build here, just showing file content)
print("--- Simulated conf.py content ---")
print("# See comments in code for full conf.py example")
print("extensions = ['myst_nb']")
print("\n--- Simulated example.md content ---")
print(example_md_content)