【问题标题】:Use Sphinx autosummary recursively to generate API documentation递归使用 Sphinx 自动摘要生成 API 文档
【发布时间】:2018-06-12 22:24:47
【问题描述】:

我想使用 Sphinx 的 autosummary extensiontemplates 从文档字符串递归生成 API 文档。我想要每个模块、类、方法、属性和函数的单独页面。但它根本没有检测到我的模板。事实上,如果我只是从_templates/autosummary/ 中删除module.rst 文件,它会以与以前完全相同的方式呈现整个文件。我已经关注this SO question 到这封信。如果你有兴趣,the full repository is on GitHub

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

我的目录结构如下:

  • 稀疏
  • 文档
    • conf.py
    • index.rst
    • modules.rst
    • _templates/autosummary/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`

modules.rst:

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

Modules
-------

.. autosummary::
   :toctree: _autosummary

   sparse

_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 %}

【问题讨论】:

  • 你为什么不用sphinx-apidoc
  • 如果 sphinx-apidoc 可以做类似于我想要的事情(使用模板和成员摘要),那么我会的。不幸的是,我对 Sphinx 不太擅长,一个例子会很有用。但据我了解,sphinx-apidoc 相当于使用带有autosummary_generate = True 的自动摘要。
  • 你试过什么?您是否阅读过 sphinx-apidoc 的文档,看看它是否满足您的需求?
  • @StevePiercy 经过一番摆弄,我让它工作了。请参阅下面的答案。

标签: python jinja2 python-sphinx restructuredtext toctree


【解决方案1】:

我最终需要以下文件:

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 }}

我还需要去sphinx/ext/autosummary/generate.py并在函数generate_autosummary_docs中设置imported_members=True

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

【讨论】:

  • 我克隆了你的 GitHub 项目,它也对我有用。在generate_autosummary_docs 函数中设置imported_members=True 似乎是关键。但问题是必须编辑 Sphinx 源代码才能使其工作。
  • 我刚看到github.com/sphinx-doc/sphinx/issues/4372。您在__init__.py 中有__all__ = ["COO", "tensordot", "concatenate", "stack", "dot", "triu", "tril"],恕我直言,这应该足够了。据我所知,autosummary 没有考虑到__all__
  • 是的,它应该将__all__ 考虑在内,或者提供一种方法来包含导入的成员。
  • 很好的答案,很有帮助!您是否知道一种将文档化的模块属性(如常量定义)添加到 modules.rst 中的输出的方法?
  • @mar10 您将添加一个类似于_templates/autosummary/module.rst 中的函数和类部分的属性部分。我自己没有尝试过,但我很确定它会起作用。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多