How to create rich links in your Sphinx documentation

This will be short, but it seems there's some difficulty doing this, so I thought I'd share.

The gist is, any time you reference a class or method in your own library, in the Python standard library, or in another third-party extension, you can provide a link directly to that project's documentation. This is pretty amazing and only requires a little bit of extra work from you. Here's how.

The Simplest Type of Link

Just create a link using the full import path of the class or attribute or method. Surround it with backticks like this:

Use :meth:`requests.Request.get` to make HTTP Get requests.

That link will show up in text as:

Use requests.Request.get to make HTTP Get requests.

There are a few different types of declarations you can use at the beginning of that phrase:

:attr:
:class:
:meth:
:exc:

The full list is here.

I Don't Want to Link the Whole Thing

To specify just the method/attribute name and not any of the modules or classes that precede it, use a squiggly, like this:

Use :meth:`~requests.Request.get` to make HTTP Get requests.

That link will show up in text as:

Use get to make HTTP Get requests.

I Want to Write My Own Text

This gets a little trickier, but still doable:

Use :meth:`the get() method <requests.Request.get>` to make HTTP Get requests.

That link will show up in text as:

Use the get() method to make HTTP Get requests.

I want to link to someone else's docs

In your docs/conf.py file, add 'sphinx.ext.intersphinx' to the end of the extensions list near the top of the file. Then, add the following anywhere in the file:

    # Add the "intersphinx" extension
    extensions = [
        'sphinx.ext.intersphinx',
    ]
    # Add mappings
    intersphinx_mapping = {
        'urllib3': ('http://urllib3.readthedocs.org/en/latest', None),
        'python': ('http://docs.python.org/3', None),
    }

You can then link to other projects' documentation and then reference it the same way you do your own projects, and Sphinx will magically make everything work.

I want to write the documentation inline in my source code and link to it

Great! I love this as well. Add the 'sphinx.ext.autodoc' extension, then write your documentation. There's a full guide to the inline syntax on the Sphinx website; confusingly, it is not listed on the autodoc page.

    # Add the "intersphinx" extension
    extensions = [
        'sphinx.ext.autodoc',
    ]

Hope that helps! Happy linking.

Liked what you read? I am available for hire.

2 thoughts on “How to create rich links in your Sphinx documentation

  1. Robert W.

    Kevin, further to linking to one set of docs from another, I followed your `intersphinx_mapping` syntax above. My version looks like this:
    ““
    intersphinx_mapping = {
    ‘leapyear-python’: (‘https://leapyear-python-docs.readthedocs-hosted.com/en/latest’, None),
    ‘python’: (‘http://docs.python.org/3’, None),
    }
    “`

    Then in my top-level `index.rst` file I effected this change:
    “`
    .. toctree::

    User Guide
    Installation Guide
    POC Setup Checklist
    LeapYear Python Client
    Reference
    “`

    All but the 4th one exist in my main project. The 4th one is the subproject I referenced above in `conf.py`.

    Yet when ReadTheDocs rebuilt the docs, nothing appeared to change.

    What am I missing?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments are heavily moderated.