1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Sphinx documentation tree without full path to module

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to create an automated documentation sphinx with autodoc and autosummary extension. I'm also using the pydata_sphinx_theme.

    My goal is to automatically create a tree of objects (let's stick with python functions for now) that will be shown in the primary sidebar of the documentation.

    The template (see lower) does the job, nevertheless it always shows the full path to each function - e.g. my_package.my_python_module1.function_A. The goal is to get rid of the whole path and see just the ending object.

    Code structure:
    ├───my_package
    │ └───my_python_module1 (contains function_A)
    │ └───my_directory
    │ └───my_python_module2 (contains function_B)


    Generated documentation tree:
    ├───my_package
    │ └───my_package.my_python_module1
    │ └───my_package.my_python_module1.function_A
    │ └───my_package.my_directory
    │ └───my_package.my_directory.my_python_module2
    │ └───my_package.my_directory.my_python_module2.function_B

    Desired documentation tree
    ├───my_package
    │ └───my_python_module1
    │ └───function_A
    │ └───my_directory
    │ └───my_python_module2
    │ └───function_B


    One of the recommended options is to use add_module_names = False in conf.py - this does not work for the pydata_sphinx_theme.

    Also playing around with the "fullname" (like replacing with "name") in code lower also solves the job only partially.

    Full template code of custom-module-template.rst:

    {{ fullname | escape | underline}}

    .. automodule:: {{ fullname }}

    {% block attributes %}
    {% if attributes %}
    .. rubric:: Module attributes

    .. autosummary::
    :toctree:
    {% for item in attributes %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block functions %}
    {% if functions %}
    .. rubric:: {{ _('Functions') }}

    .. autosummary::
    :toctree:
    :nosignatures:
    {% for item in functions %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block classes %}
    {% if classes %}
    .. rubric:: {{ _('Classes') }}

    .. autosummary::
    :toctree:
    :template: custom-class-template.rst
    :nosignatures:
    {% for item in classes %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block exceptions %}
    {% if exceptions %}
    .. rubric:: {{ _('Exceptions') }}

    .. autosummary::
    :toctree:
    {% for item in exceptions %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block modules %}
    {% if modules %}
    .. autosummary::
    :toctree:
    :template: custom-module-template.rst
    :recursive:
    {% for item in modules %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}


    So the question is: how to adjust the template in order to get rid of the full path?

    Continue reading...

Compartilhe esta Página