pdoc3 / pdoc

:snake: :arrow_right: :scroll: Auto-generate API documentation for Python projects
https://pdoc3.github.io/pdoc/
GNU Affero General Public License v3.0
1.12k stars 145 forks source link

Can I simplify the docs module hierarchy? #380

Closed abrahammurciano closed 2 years ago

abrahammurciano commented 2 years ago

Background

Suppose I have a directory structure like so:

main_module/
    __init__.py (contains some introductory docs)
    sub_mod_a/
        __init__.py (imports Class1 and Class2)
        file_1.py (contains Class1)
        file_2.py (contains Class2)
    sub_mod_b/
        __init__.py (imports Class3 and Class4)
        file_3.py (contains Class3)
        file_4.py (contains Class4)

The classes are imported by their sub_mod_x module so you'd be able to import classes like this:

from main_module.sub_mod_a import Class1

instead of the more verbose:

from main_module.sub_mod_a.file_1 import Class1

Question

I'd like the pdoc3 page for main_module.sub_mod_a to show the full documentation of all the classes that module imports in its __init__.py instead of showing links to main_module.sub_mod_a.file_1 and main_module.sub_mod_a.file_2.

Is there a way to do this?

abrahammurciano commented 2 years ago

Answer

I found that by setting the __all__ variable in my __init__.py files to all the things I wanted to import, pdoc3 took it into account and included the documentation one level higher, like I wanted.

For example, in my described setup, I changed my main_module/sub_mod_a/__init__.py to look like this:

from .file_1 import Class1
from .file_2 import Class2

# I added this:
__all__ = (
    "Class1",
    "Class2",
)