python-sphinx

How to add link to source code in Sphinx


enter image description here

class torch.FloatStorage[source]
    byte()
        Casts this storage to byte type
    char()
        Casts this storage to char type

Im trying to get some documentation done, i have managed to to get the format like the one shown above, But im not sure how to give that link of source code which is at the end of that function! The link takes the person to the file which contains the code,But im not sure how to do it,


Solution

  • This is achieved thanks to one of the builtin sphinx extension.

    The one you are looking for in spinx.ext.viewcode. To enable it, add the string 'sphinx.ext.viewcode' to the list extensions in your conf.py file.

    In summary, you should see something like that in conf.py

    extensions = [
        # other extensions that you might already use
        # ...
        'sphinx.ext.viewcode',
    ]