How to add link in Python docstring?
Asked Answered
Q

4

5

I have a function in python 3.x

def foo():
"""Lorem ipsum for more info, see here""

I want to add a hyperlink to 'here' to point to a web site. How can I do that without installing external plugin?

Quinte answered 22/7, 2022 at 3:38 Comment(0)
D
5

Just add the link as a string into the docstring, like so:

def foo():
    """Lorem ipsum for more info, see here: www.myfancydocu.com""

The doctring is just a string, so there is no Hyperlink. But anyone that wants to look at the website can just copy the link.

There are automatic documentation-builders that build a documentation out of your code and docstrings in e.g. html. Those can probably add hyperlinks to the documentation with a specific syntax, but that syntax then depends on which documentation-builder you use. If you only have your code, then just adding the url as a string is all you can do.

Dote answered 22/7, 2022 at 13:54 Comment(0)
S
12

If you're using sphinx to generate your documentation

""" 

`here <url>`__

"""

in your docstrings should be parsed properly.

Sincere answered 18/4, 2023 at 14:39 Comment(1)
Which docstring format is that?Threlkeld
D
5

Just add the link as a string into the docstring, like so:

def foo():
    """Lorem ipsum for more info, see here: www.myfancydocu.com""

The doctring is just a string, so there is no Hyperlink. But anyone that wants to look at the website can just copy the link.

There are automatic documentation-builders that build a documentation out of your code and docstrings in e.g. html. Those can probably add hyperlinks to the documentation with a specific syntax, but that syntax then depends on which documentation-builder you use. If you only have your code, then just adding the url as a string is all you can do.

Dote answered 22/7, 2022 at 13:54 Comment(0)
A
2

If you are using mkdocstrings to generate documentation for a mkdocs project (rather than using Sphinx) you can generate a clickable url link in the documentation by embedding it between the less than (<) and greater than (>) characters. Something like this:

def foo():
    """Lorem ipsum for more info, see here <https://www.example.com>"""
    pass
Adessive answered 16/7 at 0:30 Comment(2)
Which docstring format is that?Threlkeld
I used this for a mkdocs project (mkdocs.org) where the API documentation was pulled from the function docstrings using mkdocstrings (mkdocstrings.github.io). mkdocstrings can handle different programming languages and docstring formats based on specificiations in a .yaml file.Adessive
H
0

Syntax should be:

`hyperlink_name`_. (grave accent before and after the hyperlink name)

.. _hyperlink_name: https://www.example.com

For example:

def class_name():
"""

Inspired from the `PSM-Net`_.


.. _PSM-Net: https://arxiv.org/abs/1803.08669
"""
...

If you put your cursor on the class_name the result will be:

enter image description here

Heyer answered 5/10 at 18:50 Comment(3)
Which docstring format is that?Threlkeld
Sphinx, Google style python docstring documentation, as suggested hereHeyer
Is this pycharm? did you do anything special to get the links displayed? even if i choose google at Tools>Pyhton Integrated Tools>Docstring format I get plain text.Threlkeld

© 2022 - 2024 — McMap. All rights reserved.