SPHINX

Navigation

  • Documentation »
  • Using Sphinx »
  • Extensions »
  • sphinx.ext.linkcode – Add external links to source code

On this page

  • sphinx.ext.linkcode – Add external links to source code
    • Configuration

Site navigation

Get started

  • Getting Started
  • Installing Sphinx
  • Tutorial: Build your first project

User Guides

  • Using Sphinx
    • reStructuredText
    • Markdown
    • Configuration
    • Builders
    • Extensions
      • sphinx.ext.autodoc – Include documentation from docstrings
      • sphinx.ext.autosectionlabel – Allow reference sections using its title
      • sphinx.ext.autosummary – Generate autodoc summaries
      • sphinx.ext.coverage – Collect doc coverage stats
      • sphinx.ext.doctest – Test snippets in the documentation
      • sphinx.ext.duration – Measure durations of Sphinx processing
      • sphinx.ext.extlinks – Markup to shorten external links
      • sphinx.ext.githubpages – Publish HTML docs in GitHub Pages
      • sphinx.ext.graphviz – Add Graphviz graphs
      • sphinx.ext.ifconfig – Include content based on configuration
      • sphinx.ext.imgconverter – A reference image converter using Imagemagick
      • sphinx.ext.inheritance_diagram – Include inheritance diagrams
      • sphinx.ext.intersphinx – Link to other projects’ documentation
      • sphinx.ext.linkcode – Add external links to source code
      • Math support for HTML outputs in Sphinx
      • sphinx.ext.napoleon – Support for NumPy and Google style docstrings
      • sphinx.ext.todo – Support for todo items
      • sphinx.ext.viewcode – Add links to highlighted source code
    • HTML Theming
    • Internationalization
    • Sphinx Web Support
  • Writing Sphinx Extensions
  • LaTeX customization
  • Sphinx Extensions API

Community

  • Get support
  • Contribute to Sphinx
  • Sphinx FAQ
  • Sphinx authors

Reference

  • Command-Line Tools
  • Configuration
  • Extensions
    • sphinx.ext.autodoc – Include documentation from docstrings
    • sphinx.ext.autosectionlabel – Allow reference sections using its title
    • sphinx.ext.autosummary – Generate autodoc summaries
    • sphinx.ext.coverage – Collect doc coverage stats
    • sphinx.ext.doctest – Test snippets in the documentation
    • sphinx.ext.duration – Measure durations of Sphinx processing
    • sphinx.ext.extlinks – Markup to shorten external links
    • sphinx.ext.githubpages – Publish HTML docs in GitHub Pages
    • sphinx.ext.graphviz – Add Graphviz graphs
    • sphinx.ext.ifconfig – Include content based on configuration
    • sphinx.ext.imgconverter – A reference image converter using Imagemagick
    • sphinx.ext.inheritance_diagram – Include inheritance diagrams
    • sphinx.ext.intersphinx – Link to other projects’ documentation
    • sphinx.ext.linkcode – Add external links to source code
    • Math support for HTML outputs in Sphinx
    • sphinx.ext.napoleon – Support for NumPy and Google style docstrings
    • sphinx.ext.todo – Support for todo items
    • sphinx.ext.viewcode – Add links to highlighted source code
  • reStructuredText
  • Glossary
  • Changelog
  • Projects using Sphinx

sphinx.ext.linkcode – Add external links to source code¶

Module author: Pauli Virtanen

New in version 1.2.

This extension looks at your object descriptions (.. class::, .. function:: etc.) and adds external links to code hosted somewhere on the web. The intent is similar to the sphinx.ext.viewcode extension, but assumes the source code can be found somewhere on the Internet.

In your configuration, you need to specify a linkcode_resolve function that returns an URL based on the object.

Configuration¶

linkcode_resolve¶

This is a function linkcode_resolve(domain, info), which should return the URL to source code corresponding to the object in given domain with given information.

The function should return None if no link is to be added.

The argument domain specifies the language domain the object is in. info is a dictionary with the following keys guaranteed to be present (dependent on the domain):

  • py: module (name of the module), fullname (name of the object)

  • c: names (list of names for the object)

  • cpp: names (list of names for the object)

  • javascript: object (name of the object), fullname (name of the item)

Example:

def linkcode_resolve(domain, info):
    if domain != 'py':
        return None
    if not info['module']:
        return None
    filename = info['module'].replace('.', '/')
    return "https://somesite/sourcerepo/%s.py" % filename
© Copyright 2007-2023, the Sphinx developers. Created using Sphinx 7.2.3.