使用Sphinx为你的python模块自动生成文档

# 前言

Sphinx是一个可以用于Python的自动文档生成工具,可以自动的把docstring转换为文档,并支持多种输出格式包括html,latex,pdf等。

安装

pip install sphinx

创建一个sphinx项目

下面的命令会自动生成一个默认的Sphinx模板

mkdir yourdir
cd yourdir
sphinx-quickstart

执行期间,它会一步步的询问对模板的设置,除了一些必须填写的选项,大部分填写默认值就行了,你会遇到这样一条叫autodoc的,需要选择yes

autodoc: automatically insert docstrings from modules (y/n) [n]

然后默认的目录就生成了,大概是这个样子

- yourdir/ # 刚才新建的目录
    - source/ # 存放Sphinx工程源码
    - build/ # 存放生成的文档
    Makefile

现在执行如下指令,就会生成一份空文档,存放在/build/html里,点击index.html就可以打开一个空的网页,虽然没有内容,但是整体的结构还是在的

sphinx-build -b html source build
make html

source/ 目录

source/目录里有两个文件,分别是conf.pyindex.rst,下面对它们进行进一步的介绍

index.rst

.rst是reStructuredText,和Markdown一样是一种标记语言,具体的语法可以看这里 reStructuredText Primer
实际上,我们在使用Sphinx的过程中最主要就是对这些rst文件进行修改,而Sphinx所做的事情就是读取这些rst文件,并转换为特定格式的文档,在前面的步骤中,index.rst实际上被转换为build/html/index.html,而实际上在rst文档内你可以写任何东西,最后这些都会被转换到输出文档中。

打开index.rst,可以看到这样的内容,这是rst的一个语法,它使用了一个toctree节点,并设置了一些参数,而这个toctree节点是必须的。

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

conf.py

这是运行sphinx生成文档时会加载的配置,你可以通过对它进行修改来改变生成文档的行为。

一个具体的例子

假设现在我们有一个叫run.py的文件,如下

# run.py
def run(name):
    """
    this is how we run

    :param name name of people who runs
    """
    print name, 'is running'

那么需要如何生成文档呢?下面一步步带你完成
- 创建一个文件夹demo/,并将run.py放到里面
- 在demo里创建一个docs目录,进入docs/目录,当然这里你可以随意选定文件夹,只是这样更规范一些
- 生成Sphinx默认模板,设置项目名为demo,并开启autodoc
运行sphinx-quickstart
- 进入source目录,打开index.rst
- 将index.rst 修改为如下,实际上这里面可以写任何满足rst规范的内容

Welcome to demo's API Documentation
======================================

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

Introduction
============
This is the introduction of demo。

API
===
.. automodule:: run
   :members:

Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

这个是用于自动从模块读取docstring的语句,可以自动从run模块读取文档

.. automodule:: run
   :members:
  • 但是现在还差一步,如果你现在直接生成文档,会告诉你找不到run模块,因为Sphinx默认只会从sys.path里加载模块,所以需要将demo目录加入sys.path,所以现在打开conf.py,添加如下内容
import os
import sys
sys.path.insert(0, os.path.abspath('../..'))
  • 运行Sphinx生成html文档
cd ..
sphinx-build -b html source build
make html

现在,打开build/html/index.html就可以看到如下界面了

demo

格式进一步优化

上面的例子涵盖了大多数常用操作,但是通常文档都不是扁平的,而是层次化的,那么要如何将文档层次化的分门别类。
实际上在rst文档中是可以以链接的形式引用其他rst文档的,也就是说我们可以自由的对文档结构进行组织使其更易读

下面我们把run的文档移动到一个单独的页面,只在index.rst里保留跳转链接。在source目录下创建run.rst

API
===
.. automodule:: run
   :members:

Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

然后将index.rst对应位置的内容删掉,并进行修改

Welcome to demo's API Documentation
===================================

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

Introduction
============
This is the introduction of demo。

API
===
:doc:'Run API</run>'

Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

:doc:'Run API</run>'表示对一个文档的引用,这里引用了当前目录的run.rst,现在重新生成html,就会看到页面显示上的变化了。

关于docstring格式,参考PEP257,PEP287

  • 10
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Sphinx 工具来生成 Python 项目的文档Sphinx 可以自动地从你的代码中提取 docstring 并生成 HTML 或其他格式的文档。 以下是一些简单的步骤: 1. 安装 Sphinx 工具。可以使用 pip 安装: ``` pip install sphinx ``` 2. 初始化 Sphinx 项目。在你的项目根目录下,运行以下命令: ``` sphinx-quickstart ``` 该命令将创建一个 `docs` 目录,并提示你回答一些问题来配置 Sphinx 项目。 3. 编写接口文档。在你的代码中使用 docstring 来描述接口,例如: ```python def my_function(arg1: str, arg2: int) -> str: """ This function does something. :param arg1: A string argument. :param arg2: An integer argument. :return: A string result. """ pass ``` 4. 配置 Sphinx 项目。在 `docs/conf.py` 文件中,添加以下内容: ```python # Add the path to your code to the Python path. import os import sys sys.path.insert(0, os.path.abspath('..')) # Tell Sphinx to use the autodoc extension. extensions = ['sphinx.ext.autodoc'] # Set the path to the modules you want to document. autodoc_mock_imports = ['my_module'] # Set the path to your project's root directory. master_doc = 'index' ``` 5. 生成文档。在 `docs` 目录下运行以下命令: ``` make html ``` 该命令将在 `docs/_build/html` 目录下生成 HTML 格式的文档。 注意:这里的 `my_module` 应该替换成你需要文档化的模块名字。如果你的项目中有依赖项,但是你不希望在文档中显示它们,可以使用 `autodoc_mock_imports` 设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值