Jupyter Sphinx Extensions
Jupyter Sphinx Extensions is a Sphinx extension that enables embedding Jupyter Notebook content directly into Sphinx documentation. It allows executing notebook cells, displaying outputs (text, images, interactive widgets), and including entire notebooks. The current version is 0.5.3, with releases typically occurring on an 'as needed' basis, often annually or bi-annually for minor updates.
Common errors
-
ERROR: Content is not a notebook: <jupyter_sphinx.parser.JupyterKernelParser object at 0x...>
cause This error typically occurs when the `jupyter-execute` directive is used with a file path argument, but it expects inline code content.fixUse `jupyter-execute` for inline code blocks only. For external notebook files (e.g., `mynotebook.ipynb`), use the `nbtosf-execute` directive: `.. nbtosf-execute:: path/to/mynotebook.ipynb`. -
No module named 'my_package_in_notebook'
cause The Python environment building your Sphinx documentation is missing a package required by the code inside your Jupyter Notebook.fixInstall the missing package into the Sphinx build environment: `pip install my_package_in_notebook`. Ensure your documentation's `requirements.txt` (or similar) includes all notebook dependencies. -
Kernel died, restarting...
cause The Jupyter kernel crashed during execution. This can be due to runtime errors, excessive memory/CPU usage, or unhandled exceptions within the notebook code.fixExamine the notebook code for errors that might cause a crash. Increase the `timeout` in `jupyter_sphinx_execute_options` if it's a long-running process, or provide more resources to your build environment if it's a memory/CPU issue. Check logs for more specific error messages. -
Output is missing from the document, even though code executed successfully.
cause The `jupyter-output` directive was not included after a `jupyter-execute` block, or `jupyter-sphinx` is not correctly configured to capture output.fixAlways place `.. jupyter-output::` immediately after your `.. jupyter-execute::` block to display its results. Ensure `jupyter_sphinx` is listed in `extensions` in `conf.py`.
Warnings
- breaking The `jupyter-include` directive was removed in version 0.4.0. It has been replaced by `jupyter-execute` (for inline code) and `nbtosf-execute` (for entire notebooks).
- gotcha Building documentation with `jupyter-sphinx` can be slow if notebook cells are re-executed on every build. This is particularly noticeable with many or long-running cells.
- gotcha The environment where Sphinx is built must have all Python packages installed that are required by the Jupyter Notebook code being executed. Missing packages will lead to `ModuleNotFoundError` or `Kernel died` errors.
Install
-
pip install jupyter-sphinx
Quickstart
# conf.py
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'jupyter_sphinx',
]
# Optional: Configure caching for faster builds
jupyter_sphinx_execute_options = {
'kernel': 'python3', # Specify kernel if not default
'timeout': 60, # Timeout for execution
'cache': '.jupyter_cache' # Enable caching
}
# index.rst or your_doc.md
# .. jupyter-execute::
#
# import pandas as pd
# df = pd.DataFrame({'A': [1,2], 'B': [3,4]})
# print(df)
#
# .. jupyter-output::
#
# For external notebooks:
# .. nbtosf-execute:: path/to/your_notebook.ipynb