Sphinx,最佳实践

发布于 2024-11-09 04:52:31 字数 657 浏览 6 评论 0原文

我刚刚开始使用 Sphinx 工具为我的代码生成文档。但我有点困惑,因为它并不像我想象的那么容易。我使用以下命令创建 Sphinx 文档:

sphinx-quickstart

然后在“source”文件夹中创建 *.rst 文件。似乎我需要为每个要为其创建文档的模块创建一个 *.rst 文件。对于 test.py,我创建 test.rst。在 test.rst 中,我有:

.. automodule:: test
    :members:
    :show-inheritance:

然后在 test.py 中,我有:

"""
.. module:: test
   :platform: Unix, Windows
   :synopsis: A useful module indeed.
"""

然后我使用以下方法生成文档:

sphinx-build -b html source/ build/

一切都按预期工作,但问题是它并不像我预期的那么容易。我想一定有一种更简单的方法可以跳过其中一些步骤。我想知道是否有任何方法可以为包内的所有模块生成文档,而不是为每个模块生成 *.rst 文件。

谢谢。

I just started to use Sphinx tool to generate a documentation for my code. But I'm a bit confused because it's not as easy as I expected. I create the Sphinx doc using:

sphinx-quickstart

and then I create my *.rst files into the "source" folder. Seems like I need to create a *.rst file for each module I want to create a document for. For test.py, I create test.rst. Inside test.rst, I have:

.. automodule:: test
    :members:
    :show-inheritance:

Then inside test.py, I have:

"""
.. module:: test
   :platform: Unix, Windows
   :synopsis: A useful module indeed.
"""

Then I generate the documentation using:

sphinx-build -b html source/ build/

Everything works as expected, but the problem is that it's not as easy as I expected. I guess that there must be an easier way to do it with skipping some of these steps. I wonder if there is any way to generate a documentation for all the modules inside a package instead of generating a *.rst file for each module.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伤痕我心 2024-11-16 04:52:31

没有更简单的方法了。 Sphinx 不是像 epydoc 那样的 API 文档生成器,而是专注于手写文档。因此,您需要手写大量文档。

优点是,您还可以编写 API 文档之外的文档(例如教程、使用指南,甚至最终用户文档),并且您可以逻辑地构建 API 文档,而不仅仅是可用对象的简单字母顺序列表。如果做得正确的话,这样的文档通常更容易理解并且更容易使用。

查看知名项目的文档(例如 WerkzeugSphinx 本身)查看一些最佳实践。

There is no easier way. Sphinx is not an API doc generator like epydoc, but instead focuses on handwritten documentation. Consequently you need to write a lot of the documents by hand.

The advantage is, that you can also write documentation beyond API docs (e.g. tutorials, usage guides, even end user documentation), and that you can structure API documentation logically, beyond just a simple alphabetical listing of available objects. Such documentation is generally a lot easier to comprehend and a lot easier to use, if done correctly.

Take a look at the documentation of well-known projects (e.g. Werkzeug or Sphinx itself) to see some best practices.

滥情哥ㄟ 2024-11-16 04:52:31

快速记录应用程序的一种简单方法是像往常一样将文档字符串写入类和方法中,然后根据需要在 .rst 文件中补充它们。

template.rst:

Templating
----------
Notes about templating would go here.

.. automodule:: myapp.lib.template
    :members:
    :undoc-members:

    .. py:attribute:: filepath

        Full path to template file. This attribute is added in runtime, so
        it has to be documented manually.

myapp/lib/template.py:

class Template(object):
    '''
    Class for rendering templates.

    Usage example::

        >>> t = Template('somefile')
        >>> document = t.render(variables={'ID': 1234})

    Rendered document is always returned as a unicode string.
    '''

    def render(self, **args):
        '''
        Render template. Keyword arguments are passed `as-is` to renderer.
        '''

One simple way to document your application quickly is to just write docstrings into classes and methods as per usual, and then complement them if required in the .rst files.

template.rst:

Templating
----------
Notes about templating would go here.

.. automodule:: myapp.lib.template
    :members:
    :undoc-members:

    .. py:attribute:: filepath

        Full path to template file. This attribute is added in runtime, so
        it has to be documented manually.

myapp/lib/template.py:

class Template(object):
    '''
    Class for rendering templates.

    Usage example::

        >>> t = Template('somefile')
        >>> document = t.render(variables={'ID': 1234})

    Rendered document is always returned as a unicode string.
    '''

    def render(self, **args):
        '''
        Render template. Keyword arguments are passed `as-is` to renderer.
        '''
情魔剑神 2024-11-16 04:52:31

您可以使用 sphinx-apidoc 为您创建 rst 文件。

sphinx-apidoc -o outputdir pythondir

运行输出示例:

% sphinx-apidoc -o source/ ../
File source/module1.rst already exists, skipping.
File source/module2.rst already exists, skipping.
Creating file source/module3.rst.

rst docs updated in source/.

sphinx-apidoc 不会修改现有文件,您可以使用 -f 标志强制覆盖。

You can use sphinx-apidoc to create the rst files for you.

sphinx-apidoc -o outputdir pythondir

Example run output:

% sphinx-apidoc -o source/ ../
File source/module1.rst already exists, skipping.
File source/module2.rst already exists, skipping.
Creating file source/module3.rst.

rst docs updated in source/.

sphinx-apidoc won't modify existing files, you can force overwrite with the -f flag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文