sphinx-autocodelink
Automatically add links to code blocks in Sphinx documentation.
This extension is similar to sphinx-codeautolink,
except it uses dynamic analysis to resolve links instead of static analysis.
The dynamic analysis is based on how Sphinx-Gallery resolves links
for its 'reference_url' configuration option.
Installation
pip install sphinx-autocodelink
Add the extension to your Sphinx conf.py:
extensions = [
...,
'sphinx_autocodelink',
]
Usage
Links only appear on code you actually run through one of the two mechanisms below. Each is opt-in
by use: nothing happens unless you write the directive, or add the scraper -- there's no
conf.py switch to flip.
The autocodelink directive
Write it wherever you want a code block executed and linked. It only affects that one block:
.. autocodelink::
import pkg
pkg.thing()
No figure or other output is produced, just a syntax-highlighted, linked code block. Doctest-style
content (>>>) also works, with prompts stripped before execution.
Sphinx-Gallery
Sphinx-Gallery already executes your example scripts; this hooks into that execution instead of
running anything itself. Add AutoCodeLinkScraper alongside your real image scraper(s):
from sphinx_autocodelink.gallery import AutoCodeLinkScraper
sphinx_gallery_conf = {
'image_scrapers': (AutoCodeLinkScraper(), ...), # ... = your other scraper(s), if any
}
Sphinx-Gallery's own parallel=True mode runs each example in a separate worker process, bypassing
Sphinx's usual mechanism for merging data back into the main build. AutoCodeLinkScraper writes its
records to disk instead, so they survive regardless.
Drop any module from sphinx_gallery_conf['reference_url'] once you add AutoCodeLinkScraper for
it. reference_url is Sphinx-Gallery's own, older code-linking mechanism, and it isn't aware of
this extension: both wrap the same <span> code identifiers, and Sphinx-Gallery's own embedder
doesn't check whether a match is already inside a link. Leaving reference_url configured for the
same module produces broken, nested <a> tags:
<a class="sphinx-autocodelink-a" href="..."><a href="..." class="sphx-glr-backref-...">...
Use intersphinx_mapping instead, which this extension already reads -- it covers every page, not
just gallery pages, and Sphinx-Gallery's own reference_url = None (local resolution) is redundant
with it anyway.
Backreferences index
.. autocodelink-index:: lists every linked name and the pages that reference it, filled in once
the whole site's links are known:
.. autocodelink-index::
Pass a documented dotted name to show just its own references -- handy on that name's own API page:
.. autocodelink-index:: pkg.thing
Library use
A consumer that already executes example code for its own purposes (e.g. to render a figure) can
skip the directive and AutoCodeLinkScraper entirely, and call record_namespace() directly with
the resulting namespace. For example, pyvista's pyvista-plot directive
already builds its own namespace via exec(code, ns) to render a figure; adding autolinking is one
extra call after that:
from sphinx_autocodelink import record_namespace
record_namespace(env=env, docname=env.docname, source=code, namespace=ns)
Then, from the consumer's own setup(app), call app.setup_extension('sphinx_autocodelink'):
def setup(app):
app.setup_extension('sphinx_autocodelink')
app.add_directive('pyvista-plot', PlotDirective)
...
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_autocodelink-0.1.0.tar.gz.
File metadata
- Download URL: sphinx_autocodelink-0.1.0.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94ad76b9ffcb9bdc2223f3e61be2b6207f7b6cda76d29c16d08b8a3433ddacb1
|
|
| MD5 |
5db5e1cadbc7ea4a6c83ba33dd773eb0
|
|
| BLAKE2b-256 |
d4ab54a63007b949ab77bea411cf08d2d59e3d22840a8ab0430f292b2022e327
|
Provenance
The following attestation bundles were made for sphinx_autocodelink-0.1.0.tar.gz:
Publisher:
ci.yml on user27182/sphinx-autocodelink
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sphinx_autocodelink-0.1.0.tar.gz -
Subject digest:
94ad76b9ffcb9bdc2223f3e61be2b6207f7b6cda76d29c16d08b8a3433ddacb1 - Sigstore transparency entry: 2500007045
- Sigstore integration time:
-
Permalink:
user27182/sphinx-autocodelink@12f4146e722d13e72037b3038488f0361f3c8d00 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/user27182
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@12f4146e722d13e72037b3038488f0361f3c8d00 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sphinx_autocodelink-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sphinx_autocodelink-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40028df4bb5523937bdd710f3ddb36fe39bd8a4b4c58b7ee5c19e4f1e1b21cba
|
|
| MD5 |
3c60404aecc05b487904dbed7ea49580
|
|
| BLAKE2b-256 |
3d5cb25bbe570a006f07f6a678b4b4af2272daf21d2e3135d715e014c900c84e
|
Provenance
The following attestation bundles were made for sphinx_autocodelink-0.1.0-py3-none-any.whl:
Publisher:
ci.yml on user27182/sphinx-autocodelink
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sphinx_autocodelink-0.1.0-py3-none-any.whl -
Subject digest:
40028df4bb5523937bdd710f3ddb36fe39bd8a4b4c58b7ee5c19e4f1e1b21cba - Sigstore transparency entry: 2500007051
- Sigstore integration time:
-
Permalink:
user27182/sphinx-autocodelink@12f4146e722d13e72037b3038488f0361f3c8d00 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/user27182
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@12f4146e722d13e72037b3038488f0361f3c8d00 -
Trigger Event:
push
-
Statement type: