递归使用Sphinx autosummary生成API文档

Ham*_*asi 10 python restructuredtext jinja2 python-sphinx

我想使用Sphinx的autosummary扩展模板从docstrings递归生成API文档.我想为每个模块,类,方法,属性和功能分别设置页面.但它根本检测不到我的模板.实际上,如果我只是module.rst从中删除文件_templates/autosummary/,它会像以前一样呈现整个文件.我跟着这个问题跟着封信.如果您有兴趣,完整的存储库位于GitHub上.

编辑:它似乎确实生成了一个不同的文件,我不得不删除docs/_autosummary来读取新模板.但是,现在它生成一个带有sparse标题和description标题的文件.它没有进入{% if classes %}{% if functions %}指令.

我的目录结构如下:

  • 文档
    • conf.py
    • index.rst
    • modules.rst
    • _templates /自动摘要/ module.rst

以下是目前的相关文件:

index.rst:

.. sparse documentation master file, created by
   sphinx-quickstart on Fri Dec 29 20:58:03 2017.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to sparse's documentation!
==================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   modules

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
Run Code Online (Sandbox Code Playgroud)

modules.rst:

API Reference
=============

Modules
-------

.. autosummary::
   :toctree: _autosummary

   sparse
Run Code Online (Sandbox Code Playgroud)

_templates/autosummary/module.rst:

{{ fullname | escape | underline }}

Description
-----------

.. automodule:: {{ fullname | escape }}

{% if classes %}
Classes
-------
.. autosummary:
    :toctree: _autosummary

    {% for class in classes %}
        {{ class }}
    {% endfor %}

{% endif %}

{% if functions %}
Functions
---------
.. autosummary:
    :toctree: _autosummary

    {% for function in functions %}
        {{ function }}
    {% endfor %}

{% endif %}
Run Code Online (Sandbox Code Playgroud)

Jam*_*ham 9

从 Sphinx 3.1 版(2020 年 6 月)开始,您可以使用新:recursive:选项来sphinx.ext.autosummary自动检测包中的每个模块,无论嵌套有多深,并自动为该模块中的每个属性、类、函数和异常生成文档。

在此处查看我的答案:https : //stackoverflow.com/a/62613202/12014259


Ham*_*asi 6

我最终需要以下文件:

modules.rst:

API Reference
=============

.. rubric:: Modules

.. autosummary::
   :toctree: generated

   sparse
Run Code Online (Sandbox Code Playgroud)

_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 %}
Run Code Online (Sandbox Code Playgroud)

_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 %}
Run Code Online (Sandbox Code Playgroud)

_templates/autosummary/base.rst:

{{ fullname | escape | underline}}

.. currentmodule:: {{ module }}

.. auto{{ objtype }}:: {{ objname }}
Run Code Online (Sandbox Code Playgroud)

我还需要进入sphinx/ext/autosummary/generate.py并设置imported_members=True功能generate_autosummary_docs.

如果您numpydoc不像我一样使用,则可能需要删除.. HACK指令.