콘텐츠로 이동

Referencing python source code

If you want to add source code description to your page, you can use the following syntax.

::: my_project.two_numbers.TwoNumbers
    options:
        show_root_heading: true

my_project.two_numbers.TwoNumbers dataclass

Example class that adds two numbers.

Source code in src/my_project/two_numbers.py
@dataclass
class TwoNumbers:
    """Example class that adds two numbers."""

    num_1: int
    num_2: int

    def add(self) -> int:
        """
        Add two numbers together.

        Examples:
            >>> TwoNumbers(1, 2).add()
            3

            >>> TwoNumbers(1, -1).add()
            0

        Returns:
            The sum of the two numbers.

        Note:
            This function only supports integers.

        Todo: You can put title here.
            * Add support for floats.
            * Add support for strings.
        """
        assert isinstance(self.num_1, int)
        assert isinstance(self.num_2, int)
        return self.num_1 + self.num_2

add()

Add two numbers together.

Examples:

>>> TwoNumbers(1, 2).add()
3
>>> TwoNumbers(1, -1).add()
0

Returns:

Type Description
int

The sum of the two numbers.

Note

This function only supports integers.

You can put title here.
  • Add support for floats.
  • Add support for strings.
Source code in src/my_project/two_numbers.py
def add(self) -> int:
    """
    Add two numbers together.

    Examples:
        >>> TwoNumbers(1, 2).add()
        3

        >>> TwoNumbers(1, -1).add()
        0

    Returns:
        The sum of the two numbers.

    Note:
        This function only supports integers.

    Todo: You can put title here.
        * Add support for floats.
        * Add support for strings.
    """
    assert isinstance(self.num_1, int)
    assert isinstance(self.num_2, int)
    return self.num_1 + self.num_2

If you want to include all submodules, you can use the following syntax.

::: my_project
    options:
        show_root_heading: true
        show_submodules: true

two_numbers

TwoNumbers dataclass

Example class that adds two numbers.

Source code in src/my_project/two_numbers.py
@dataclass
class TwoNumbers:
    """Example class that adds two numbers."""

    num_1: int
    num_2: int

    def add(self) -> int:
        """
        Add two numbers together.

        Examples:
            >>> TwoNumbers(1, 2).add()
            3

            >>> TwoNumbers(1, -1).add()
            0

        Returns:
            The sum of the two numbers.

        Note:
            This function only supports integers.

        Todo: You can put title here.
            * Add support for floats.
            * Add support for strings.
        """
        assert isinstance(self.num_1, int)
        assert isinstance(self.num_2, int)
        return self.num_1 + self.num_2

add()

Add two numbers together.

Examples:

>>> TwoNumbers(1, 2).add()
3
>>> TwoNumbers(1, -1).add()
0

Returns:

Type Description
int

The sum of the two numbers.

Note

This function only supports integers.

You can put title here.
  • Add support for floats.
  • Add support for strings.
Source code in src/my_project/two_numbers.py
def add(self) -> int:
    """
    Add two numbers together.

    Examples:
        >>> TwoNumbers(1, 2).add()
        3

        >>> TwoNumbers(1, -1).add()
        0

    Returns:
        The sum of the two numbers.

    Note:
        This function only supports integers.

    Todo: You can put title here.
        * Add support for floats.
        * Add support for strings.
    """
    assert isinstance(self.num_1, int)
    assert isinstance(self.num_2, int)
    return self.num_1 + self.num_2

If you want to show only the source code, you can use the following syntax.

::: my_project.two_numbers.TwoNumbers
    options:
        show_docstring_attributes: false
        show_docstring_functions: false
        show_docstring_classes: false
        show_docstring_modules: false
        show_docstring_description: false
        show_docstring_examples: false
        show_docstring_other_parameters: false
        show_docstring_parameters: false
        show_docstring_raises: false
        show_docstring_receives: false
        show_docstring_returns: false
        show_docstring_warns: false
        show_docstring_yields: false
        members: false
        show_bases: false
        show_source: true
Source code in src/my_project/two_numbers.py
@dataclass
class TwoNumbers:
    """Example class that adds two numbers."""

    num_1: int
    num_2: int

    def add(self) -> int:
        """
        Add two numbers together.

        Examples:
            >>> TwoNumbers(1, 2).add()
            3

            >>> TwoNumbers(1, -1).add()
            0

        Returns:
            The sum of the two numbers.

        Note:
            This function only supports integers.

        Todo: You can put title here.
            * Add support for floats.
            * Add support for strings.
        """
        assert isinstance(self.num_1, int)
        assert isinstance(self.num_2, int)
        return self.num_1 + self.num_2

See mkdocstrings for more about the options.


If you want to change the heading level in the table of contents, you can use the # as in markdown.

### ::: my_project.two_numbers.TwoNumbers
    options:
        show_docstring_description: false
        show_docstring_examples: false
        members: false
        show_bases: false
        show_source: true
Source code in src/my_project/two_numbers.py
@dataclass
class TwoNumbers:
    """Example class that adds two numbers."""

    num_1: int
    num_2: int

    def add(self) -> int:
        """
        Add two numbers together.

        Examples:
            >>> TwoNumbers(1, 2).add()
            3

            >>> TwoNumbers(1, -1).add()
            0

        Returns:
            The sum of the two numbers.

        Note:
            This function only supports integers.

        Todo: You can put title here.
            * Add support for floats.
            * Add support for strings.
        """
        assert isinstance(self.num_1, int)
        assert isinstance(self.num_2, int)
        return self.num_1 + self.num_2