Use Sphinx autosummary recursively to generate API documentation

后端 未结 2 464
温柔的废话
温柔的废话 2020-12-30 04:21

I want to use Sphinx\'s autosummary extension and templates to generate API docs recursively from docstrings. I want separate pages for each module, class, method, property

相关标签:
2条回答
  • 2020-12-30 05:10

    I ended up needing the following files:

    modules.rst:

    API Reference
    =============
    
    .. rubric:: Modules
    
    .. autosummary::
       :toctree: generated
    
       sparse
    

    _templates/autosummary/module.rst:

    {{ fullname | escape | underline }}
    
    .. rubric:: Description
    
    .. automodule:: {{ fullname }}
    
    .. currentmodule:: {{ fullname }}
    
    {% if classes %}
    .. rubric:: Classes
    
    .. autosummary::
        :toctree: .
        {% for class in classes %}
        {{ class }}
        {% endfor %}
    
    {% endif %}
    
    {% if functions %}
    .. rubric:: Functions
    
    .. autosummary::
        :toctree: .
        {% for function in functions %}
        {{ function }}
        {% endfor %}
    
    {% endif %}
    

    _templates/autosummary/class.rst:

    {{ fullname | escape | underline}}
    
    .. currentmodule:: {{ module }}
    
    .. autoclass:: {{ objname }}
    
       {% block methods %}
       {% block attributes %}
       {% if attributes %}
       .. HACK -- the point here is that we don't want this to appear in the output, but the autosummary should still generate the pages.
          .. autosummary::
             :toctree:
          {% for item in all_attributes %}
             {%- if not item.startswith('_') %}
             {{ name }}.{{ item }}
             {%- endif -%}
          {%- endfor %}
       {% endif %}
       {% endblock %}
    
       {% if methods %}
       .. HACK -- the point here is that we don't want this to appear in the output, but the autosummary should still generate the pages.
          .. autosummary::
             :toctree:
          {% for item in all_methods %}
             {%- if not item.startswith('_') or item in ['__call__'] %}
             {{ name }}.{{ item }}
             {%- endif -%}
          {%- endfor %}
       {% endif %}
       {% endblock %}
    

    _templates/autosummary/base.rst:

    {{ fullname | escape | underline}}
    
    .. currentmodule:: {{ module }}
    
    .. auto{{ objtype }}:: {{ objname }}
    

    I also needed to go to sphinx/ext/autosummary/generate.py and set imported_members=True in the function generate_autosummary_docs.

    If you're not using numpydoc like me, you might need to remove the .. HACK directives.

    0 讨论(0)
  • 2020-12-30 05:14

    From Sphinx version 3.1 (June 2020), you can use the new :recursive: option to get sphinx.ext.autosummary to automatically detect every module in your package, however deeply nested, and automatically generate documentation for every attribute, class, function and exception in that module.

    See my answer here: https://stackoverflow.com/a/62613202/12014259

    0 讨论(0)
提交回复
热议问题