MyST-NB

1.4.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

To quickly get started, install MyST-NB and add 'myst_nb' to the `extensions` list in your Sphinx `conf.py`. You can then create MyST Markdown files (`.md`) or Jupyter Notebooks (`.ipynb`) containing code cells, which MyST-NB will parse and execute during the Sphinx build process. The `glue` function allows embedding variables directly into your narrative text.

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)

view raw JSON →