Enhance your code examples with links to reference docs
Tags: python, documentation, sphinx
Published: 2021-10-05 20:17:13

["Like the Sphinx"](https://www.flickr.com/photos/91545223@N00/9250743991) by Benson Kua is licensed with [CC BY-SA 2.0](https://creativecommons.org/licenses/by-sa/2.0/).

## Why is it not everywhere?
Some time ago when browsing Matplotlib's [documentation](https://matplotlib.org/stable/gallery/lines_bars_and_markers/simple_plot.html), I was awestruck: I could click their code examples to get taken to the reference documentation of whatever class or function was used. What a great feature! It makes navigating documentation much easier.

So I tried to track down where those links were inserted. Turns out it was an extension called Sphinx-Gallery, which converts Python files to RST while capturing the script output. Awesome! But it didn't work for ordinary RST documentation, and it executes the code - two assumptions I didn't want to make with my own documentation. After searching around and slowly realising that it really did not exist, I set out to create a tool of my own! Enter [sphinx-codeautolink](https://sphinx-codeautolink.readthedocs.io/en/latest/).

## What it does
As advertised, it matches code example contents to definitions documented elsewhere with Sphinx:

![links example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2c2ow1oywdkn75fntdi.png) 

Clicking the link would take you to `numpy.linspace`'s documentation. Other key features include:

- Backreference tables: a table of examples where a particular definition is used
- Autodoc integration: insert backreference tables to all definitions documented with autodoc
- Intersphinx integration: as seen in the image above, linking to docs of external libraries is super simple
- Python console example support (`>>>` prefixed code)
- Invisible imports, example concatenation, simple type hint resolving and more!

## How to use it
Easy. I wanted to make it work out of the box for most documentation, so if your examples are valid Python you're most likely already there. I had to add some import statements to my examples. Other than that, it's only a matter of installing the package and adding it to the list of Sphinx extensions!

```sh
$ pip install sphinx-codeautolink
```
```python
# conf.py
extensions = [
    ...,
    "sphinx_codeautolink",
]
```

Our [examples](https://sphinx-codeautolink.readthedocs.io/en/latest/examples.html) have a more comprehensive explanation of all the use cases and RST syntax.

## How it works
The principle is quite simple: all code blocks are parsed and the names contained in them are traced back to definitions documented with Sphinx elsewhere in the documentation. Sphinx does its thing and after the HTML is written, links are injected to the correct places.

Naturally I made a few assumptions, some different than the folks at Sphinx-Gallery: it is only usable with HTML documentation and I had to cut some corners with the parsing implementation. I didn't aim to emulate the Python runtime, just provide a correct result for the 99 % case. None of the shortcomings should cause problems with reasonable example code. But Sphinx-Gallery does perform better in some situations. For example where we'd have to analyse complex type hints, Sphinx-Gallery can simply look at the variable's type.

I've poured my last month on this and really enjoyed the deep dive into Sphinx and docutils. I hope you'll get some use out of it!

## Links
[Documentation](https://sphinx-codeautolink.readthedocs.io/en/latest/), [PyPI](https://pypi.org/project/sphinx-codeautolink/), [GitHub](https://github.com/felix-hilden/sphinx-codeautolink)