Skip to main content

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

Set these in conf.py:

  • sphinx_examples_as_code_link_position: where the download link(s) land within the Examples section. 'top' (default) or 'bottom'.
  • sphinx_examples_as_code_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.
  • sphinx_examples_as_code_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.

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 (or py) blocks are kept as-is; other languages become comments.
  • 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-written See Also heading) 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``). If..._base_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.py` the 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, and sphinx-design dropdowns/tab-sets are dropped entirely.

Generated .py files start with a # Examples from <qualified name> title header 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.

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

sphinx_examples_as_code-0.1.0.tar.gz (31.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinx_examples_as_code-0.1.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file sphinx_examples_as_code-0.1.0.tar.gz.

File metadata

  • Download URL: sphinx_examples_as_code-0.1.0.tar.gz
  • Upload date:
  • Size: 31.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for sphinx_examples_as_code-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cb9116d70e6cb87fcc25bbadf242d2493c0ec7108758936fe5954d28e4a1ff83
MD5 acba228e10fdedec500f70664d441a0e
BLAKE2b-256 2735d4c832f24ab4042b34348894b149ddf14baaa77db7bcd15eb0d57b3243c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_examples_as_code-0.1.0.tar.gz:

Publisher: ci.yml on pyvista/sphinx-examples-as-code

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sphinx_examples_as_code-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinx_examples_as_code-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 010a361971b42d016d2d2ca814ba52a6781b5b9a88870d63797c73bfabae4dea
MD5 8fa4bf9856737e422737774371fc6a4b
BLAKE2b-256 edff3fe149597c5cb420a7f42f5e570472daeccad0dfa26531e6e006727208f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_examples_as_code-0.1.0-py3-none-any.whl:

Publisher: ci.yml on pyvista/sphinx-examples-as-code

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page