196

我正在尝试使用 Sphinx 在 Python 中记录一个超过 5,000 行的项目。它有大约 7 个基本模块。据我所知,为了使用 autodoc,我需要为项目中的每个文件编写如下代码:

.. automodule:: mods.set.tests
    :members:
    :show-inheritance:

这太乏味了,因为我有很多文件。如果我可以指定我想要记录“mods”包会容易得多。然后,Sphinx 可以递归地遍历包并为每个子模块创建一个页面。

有这样的功能吗?如果不是,我可以编写一个脚本来制作所有 .rst 文件,但这会占用大量时间。

4

6 回答 6

155

你可以查看我制作的这个脚本。我认为它可以帮助你。

此脚本解析目录树以查找 python 模块和包,并适当地创建 ReST 文件以使用 Sphinx 创建代码文档。它还创建了一个模块索引。

更新

该脚本现在作为apidoc成为 Sphinx 1.1 的一部分。

于 2010-04-24T04:03:35.630 回答
86

从 Sphinx 版本 3.1(2020 年 6 月)开始,sphinx.ext.autosummary(终于!)具有自动递归。

因此,不再需要硬编码模块名称或依赖Sphinx AutoAPISphinx AutoPackageSummary等第三方库来进行自动包检测。

Python 3.7 包文档示例(参见 Github 上的代码ReadTheDocs上的结果):

mytoolbox
|-- mypackage
|   |-- __init__.py
|   |-- foo.py
|   |-- mysubpackage
|       |-- __init__.py
|       |-- bar.py
|-- doc
|   |-- source
|       |--index.rst
|       |--conf.py
|       |-- _templates
|           |-- custom-module-template.rst
|           |-- custom-class-template.rst

conf.py

import os
import sys
sys.path.insert(0, os.path.abspath('../..'))  # Source code dir relative to this file

extensions = [
    'sphinx.ext.autodoc',  # Core library for html generation from docstrings
    'sphinx.ext.autosummary',  # Create neat summary tables
]
autosummary_generate = True  # Turn on sphinx.ext.autosummary

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

index.rst(注意新:recursive:选项):

Welcome to My Toolbox
=====================

Some words.

.. autosummary::
   :toctree: _autosummary
   :template: custom-module-template.rst
   :recursive:

   mypackage

这足以自动总结包中的每个模块,无论嵌套多么深。对于每个模块,它会总结该模块中的每个属性、函数、类和异常。

但奇怪的是,默认sphinx.ext.autosummary模板不会继续为每个属性、函数、类和异常生成单独的文档页面,并从汇总表链接到它们。可以扩展模板来执行此操作,如下所示,但我不明白为什么这不是默认行为 - 这肯定是大多数人想要的......?我已将其作为功能请求提出

我必须在本地复制默认模板,然后添加到它们:

  • 复制site-packages/sphinx/ext/autosummary/templates/autosummary/module.rstmytoolbox/doc/source/_templates/custom-module-template.rst
  • 复制site-packages/sphinx/ext/autosummary/templates/autosummary/class.rstmytoolbox/doc/source/_templates/custom-class-template.rst

使用选项的钩子custom-module-template.rstindex.rst上面:template:。(删除该行以查看使用默认站点包模板会发生什么。)

custom-module-template.rst(右侧注明的附加行):

{{ fullname | escape | underline}}

.. automodule:: {{ fullname }}
  
   {% block attributes %}
   {% if attributes %}
   .. rubric:: Module Attributes

   .. autosummary::
      :toctree:                                          <-- add this line
   {% for item in attributes %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

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

   .. autosummary::
      :toctree:                                          <-- add this line
   {% for item in functions %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

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

   .. autosummary::
      :toctree:                                          <-- add this line
      :template: custom-class-template.rst               <-- add this line
   {% for item in classes %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

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

   .. autosummary::
      :toctree:                                          <-- add this line
   {% for item in exceptions %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

{% block modules %}
{% if modules %}
.. rubric:: Modules

.. autosummary::
   :toctree:
   :template: custom-module-template.rst                 <-- add this line
   :recursive:
{% for item in modules %}
   {{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

custom-class-template.rst(右侧注明的附加行):

{{ fullname | escape | underline}}

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}
   :members:                                    <-- add at least this line
   :show-inheritance:                           <-- plus I want to show inheritance...
   :inherited-members:                          <-- ...and inherited members too

   {% block methods %}
   .. automethod:: __init__

   {% if methods %}
   .. rubric:: {{ _('Methods') }}

   .. autosummary::
   {% for item in methods %}
      ~{{ name }}.{{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

   {% block attributes %}
   {% if attributes %}
   .. rubric:: {{ _('Attributes') }}

   .. autosummary::
   {% for item in attributes %}
      ~{{ name }}.{{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}
于 2020-06-27T17:16:50.600 回答
50

我不知道autosummary在询问原始问题时 Sphinx 是否有扩展,但现在很可能在不使用sphinx-apidoc或类似脚本的情况下设置这种自动生成。下面是适用于我的一个项目的设置。

  1. 在文件中启用autosummary扩展名(以及autodocconf.py并将其autosummary_generate选项设置为True. 如果您不使用自定义*.rst模板,这可能就足够了。否则将您的模板目录添加到排除列表,或者autosummary将尝试将它们视为输入文件(这似乎是一个错误)。

    extensions = ['sphinx.ext.autodoc', 'sphinx.ext.autosummary']
    autosummary_generate = True
    templates_path = [ '_templates' ]
    exclude_patterns = ['_build', '_templates']
    
  2. autosummary::在文件的目录树中使用index.rst。在此示例中,模块文档将自动生成project.module1project.module2放入_autosummary目录中。

    PROJECT
    =======
    
    .. toctree::
    
    .. autosummary::
       :toctree: _autosummary
    
       project.module1
       project.module2
    
  3. 默认情况下autosummary,只会为模块及其功能生成非常简短的摘要。要更改它,您可以将自定义模板文件放入_templates/autosummary/module.rst(将使用Jinja2解析):

    {{ fullname }}
    {{ underline }}
    
    .. automodule:: {{ fullname }}
        :members:
    

总之,没有必要将_autosummary目录置于版本控制之下。此外,您可以将其命名为您想要的任何名称并将其放置在源代码树中的任何位置(但将其放在下面_build将不起作用)。

于 2014-02-09T22:29:57.797 回答
21

Sphinx AutoAPI正是这样做的。

于 2019-09-09T20:38:37.193 回答
13

在每个包中,__init__.py文件可以包含.. automodule:: package.module包的每个部分的组件。

然后你可以.. automodule:: package并且它主要做你想要的。

于 2010-04-23T21:24:15.213 回答
1

也许您正在寻找的是Epydoc和这个Sphinx 扩展

于 2011-01-31T14:31:19.780 回答