Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link to external libraries using python sphinx?

I have a python module documented on RTD: http://modernglexttextools.readthedocs.io

This is an extension of another module and I want to link between the two. I want the parameters and the return types to work as a link. Here is an example.

My conf.py

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.githubpages',
    'sphinx.ext.intersphinx',    # added this but does not help
    'sphinxcontrib.napoleon'
]

Here is an example for a method with an external class linked. The external class is ModernGL.Context. I am not sure if I have to configure where this class is documented. But it should point to this link.

def load(filename, convert=None, ctx=None) -> ModernGL.Texture:
    '''
        Load a texture. If ctx is ``None`` the default_context is used.

        Args:
            filename (str): The name of the file to load.

        Keyword Args:
            convert (str): Convert the texture before loading. Possible values are: ('L', 'RGB', 'RGBA')
            ctx (:py:class:`ModernGL.Context`): The Context to use for loading the texture.

        Returns:
            :py:class:`ModernGL.Texture`: The texture.

        Examples:

            .. code-block:: python

                import ModernGL
                from ModernGL.ext import textools

                ctx = ModernGL.create_standalone_context()
                # ctx = ModernGL.create_context()

                texture = textools.load('brick.jpg', ctx=ctx)
                texture.use()
    '''
like image 649
Szabolcs Dombi Avatar asked Mar 09 '23 19:03

Szabolcs Dombi


1 Answers

If I understand your question, it sounds like you want to only add a link from your docs to theirs, and not include their docs in yours.

Adding the Sphinx extension to extensions in your conf.py is only one piece of its usage. You also need to define an intersphinx_mapping which is a dict that tells the extension where to look for external documentation. For example:

# Looks for objects in external projects
intersphinx_mapping = {
    'moderngl': ('https://moderngl.readthedocs.io/en/stable/', None),
}

Finally to create the specific link, use this syntax:

:py:class:`ModernGL.Context`

Additionally, linking to arbitrary targets can be done with the following syntax, assuming the targets exist:

:ref:`My Label <moderngl:MyArbitraryTarget>`
like image 76
Steve Piercy Avatar answered Mar 24 '23 11:03

Steve Piercy