sphinx-examples-as-code
A Sphinx extension that turns docstring/page "Examples" sections into downloadable,
runnable .py and/or .ipynb files, with a download link inserted into the section.
Pages or docstrings without an Examples section are left completely untouched. Adding
sphinx_examples_as_code to conf.py's extensions is the only on/off switch.
Installation
pip install sphinx-examples-as-code
Add it to your Sphinx conf.py:
extensions = [
...,
'sphinx_examples_as_code',
]
Configuration
Everything lives in one dict in conf.py, sphinx_examples_as_code_conf -- set only
the keys you want to change from their default:
sphinx_examples_as_code_conf = {
'link_position': 'top',
'formats': ['py', 'ipynb'],
'base_url': None,
'gallery_downloads': False,
'footer': (
'Generated by `sphinx-examples-as-code '
'<https://github.com/pyvista/sphinx-examples-as-code>`_'
),
}
link_position: where the download link(s) land within the Examples section.'top'(default) or'bottom'.formats: which downloads to generate. A list containing'py','ipynb', or both (default). Always offered in that order regardless of how the list is written.base_url: the site's published base URL (e.g.'https://docs.pyvista.org/'), used to turn cross-references into absolute links a downloaded, standalone file can actually use.None(default) means no links are generated anywhere. A missing trailing slash is added automatically; a value with no scheme or host raises a configuration error at build start.gallery_downloads: opt-in takeover of sphinx-gallery's own per-example downloads.False(default) leaves sphinx-gallery pages untouched. See Sphinx-Gallery integration below.footer: a string appended to the end of every generated file. Defaults to a one-line "generated file" credit linking back to this project; set toNoneto omit it entirely. Preceded by a blank line and a--only divider line, marking it as trailing boilerplate rather than more of the example's own commentary -- the divider also renders as a real horizontal rule in.ipynb, where the footer always gets its own dedicated cell, regardless of what content precedes it. Parsed as RST, the same as any other prose this extension handles: a hyperlink written as`text <url>`_becomes a real clickable Markdown link in.ipynb, and renders inline astext urlin.py. Plain text with no markup at all becomes one comment line per line of text, and blank-line-separated paragraphs stay separated -- a longer, multi-paragraph custom footer is laid out the same way any other prose in this extension is.
An unrecognized key (a typo, or a leftover from an older release) raises a configuration error at build start rather than silently doing nothing.
Overriding a single key from the command line works via a dotted -D flag, e.g.
-D sphinx_examples_as_code_conf.link_position=bottom -- this only touches that one
key, leaving the rest (conf.py's values, or the defaults) alone.
Conversion rules
What happens to the content of an Examples section:
- Doctest blocks (
>>> .../... ...) keep their input lines, prompts stripped, as real Python source. Doctest output lines are dropped — only the input code matters. .. code-block:: python(orpy) blocks are kept as-is; other languages become comments, set off with blank lines on both sides like any other directive -- so a preformatted block (e.g. an RST::-block illustrating a data format) still reads as a distinct block rather than melting into the surrounding prose.- Admonitions (
.. note::,.. warning::,.. seealso::, ...) become a# LABEL:comment followed by their content as comments. "See Also" is recognized in any of its three forms (.. seealso::, a bare.. rubric:: See Also, or a hand-writtenSee Alsoheading) and always renders the same way. - Cross-references and inline code (
:class:,:meth:,:func:,:attr:, double-backtick literals, ...) keep their display text, wrapped in backticks (e.g.:class:`pyvista.Plotter`->`pyvista.Plotter`). Ifbase_urlis set and the reference resolves:.ipynbturns it into a clickable link everywhere;.pyonly writes the link inside a "See Also" part (asname urlon its own line) — everywhere else in.pythe link is simply omitted. - Plain prose-style references (
:ref:,:doc:) are treated the same way, minus the backticks. - Everything else text-bearing (prose, captions, other non-Python code) becomes a plain
#comment. - Figures/images, raw HTML, sphinx-design dropdowns/tab-sets, and sphinx-tags'
.. tags::line are dropped entirely -- none of them are documentation content a downloaded, standalone file has any use for.
Generated .py files start with a # Examples from <qualified name> title header
(gallery mode uses the page's own title instead -- see below) and follow a few
whitespace conventions so the result reads like normal Python: prose
directly above a code block stays attached to it, a code block is always followed by a
blank line, and a directive (header, # NOTE:-style block) gets blank lines on both
sides.
Generated .ipynb notebooks use the same content, split into alternating code/markdown
cells instead.
A download link is only added if the resulting code contains at least one real executable statement.
Sphinx-Gallery integration
With sphinx_examples_as_code_conf['gallery_downloads'] = True, this extension takes
over the downloads on every page generated by
sphinx-gallery: the "Go to the end to download the
full example code" note and the .py/.ipynb/.zip download footer are removed from
the page, replaced with download link(s) built by this extension instead — same
conversion rules as above, applied to the whole page rather than one Examples section.
The generated file's header uses the page's own title (e.g. # Create Circular Arcs)
rather than the generic # Examples from <docname> — gallery mode converts the whole
page, not an Examples section carved out of a larger docstring, so there's no "from"
framing to make.
The "Total running time" line and the "Gallery generated by Sphinx-Gallery" credit line stay on the rendered page untouched, but never make it into the generated download itself: a "Total running time" sitting inside a file you just downloaded and ran yourself would read like it's timing your run, not the build's, and a "Generated by Sphinx-Gallery" credit is simply wrong inside a file this extension generated.
Detection is automatic and per-page — any page without a sphinx-gallery download footer is left completely untouched, so turning this on is safe even on a site that mixes gallery pages with ordinary docstring/prose pages using the Examples-section behavior above.
A # %% cell with its own RST heading gets the same title-plus-underline treatment as
the file's own header, rather than melting into whatever prose follows it -- and for
that reason renders as a real Markdown heading in .ipynb, not just plain text.
Two things worth knowing before turning this on:
- It's built against sphinx-gallery's own long-standing RST/HTML output (the same
sphx-glr-*CSS classes its own theming depends on), not a documented extension API. It's unlikely to change, but a future sphinx-gallery release could still shift that structure without warning. - sphinx-gallery registers its own
.py/.ipynb/.zipdownloads for copying into_downloads/before this extension gets a chance to remove the links pointing to them, so those files still end up in the build output even though nothing on the page links to them anymore.
Development
uv sync --group dev
uv run pytest
uv run pre-commit run --all-files
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sphinx_examples_as_code-0.2.0.tar.gz.
File metadata
- Download URL: sphinx_examples_as_code-0.2.0.tar.gz
- Upload date:
- Size: 51.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9be7253d3e297bfbed3a9711f3a6633206acc06606670fac7462cc24cabd211
|
|
| MD5 |
d4abbd3a7513b191047cf99eef565174
|
|
| BLAKE2b-256 |
367cfe875a38fff30c823468f51819ca35e2add437f899ab1ffab24c61c778c2
|
Provenance
The following attestation bundles were made for sphinx_examples_as_code-0.2.0.tar.gz:
Publisher:
ci.yml on pyvista/sphinx-examples-as-code
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sphinx_examples_as_code-0.2.0.tar.gz -
Subject digest:
c9be7253d3e297bfbed3a9711f3a6633206acc06606670fac7462cc24cabd211 - Sigstore transparency entry: 2349255852
- Sigstore integration time:
-
Permalink:
pyvista/sphinx-examples-as-code@5ead8d7ac522b8f558b03171296568d1bb3b8722 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/pyvista
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@5ead8d7ac522b8f558b03171296568d1bb3b8722 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sphinx_examples_as_code-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sphinx_examples_as_code-0.2.0-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2dd9357833eefef84eb8175e4e1475d49709bab53b8ae1ee5afa8335927a5e66
|
|
| MD5 |
39bd515ad9e7886343cc7b43ee893bc2
|
|
| BLAKE2b-256 |
4af379343032ac12b2b6f34eeebe0fda1dd27b17452bb21f41df1c54f8517580
|
Provenance
The following attestation bundles were made for sphinx_examples_as_code-0.2.0-py3-none-any.whl:
Publisher:
ci.yml on pyvista/sphinx-examples-as-code
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sphinx_examples_as_code-0.2.0-py3-none-any.whl -
Subject digest:
2dd9357833eefef84eb8175e4e1475d49709bab53b8ae1ee5afa8335927a5e66 - Sigstore transparency entry: 2349255946
- Sigstore integration time:
-
Permalink:
pyvista/sphinx-examples-as-code@5ead8d7ac522b8f558b03171296568d1bb3b8722 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/pyvista
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@5ead8d7ac522b8f558b03171296568d1bb3b8722 -
Trigger Event:
push
-
Statement type: