python 使用sphinx 快速生成说明文档

本文档详细介绍了如何在Linux环境下使用Sphinx为Python项目创建说明文档,包括安装Sphinx、设置文件结构、修改配置文件以及生成HTML和Markdown格式的文档。Sphinx支持Google风格的代码注释,使得文档编写和维护更加便捷。
部署运行你感兴趣的模型镜像

python 使用sphinx 快速生成说明文档

以Linux环境使用为例,没有linux环境的可以安装虚拟机

1.安装sphinx

pip3 install sphinx

2.文件结构

在项目的同级目录下创建doc文件夹,进入doc文件夹,运行如下指令:

sphinx-quickstart

接着要填写一些项目信息

ubuntu18@ubuntu18:~/weiquan/sphnix-demo/doc$ sphinx-quickstart
Welcome to the Sphinx 4.5.0 quickstart utility.

Please enter values for the following settings (just press Enter to
accept a default value, if one is given in brackets).

Selected root path: .

You have two options for placing the build directory for Sphinx output.
Either, you use a directory "_build" within the root path, or you separate
"source" and "build" directories within the root path.
> Separate source and build directories (y/n) [n]: y

The project name will occur in several places in the built documentation.
> Project name: myproject  // 输入项目名称
> Author name(s): bob	// 作者
> Project release []: 0.0.1	// 项目版本

If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.

For a list of supported codes, see
https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-language.
> Project language [en]:  // 生成文档的语言

Creating file /home/ubuntu18/weiquan/sphnix-demo/doc/source/conf.py.
Creating file /home/ubuntu18/weiquan/sphnix-demo/doc/source/index.rst.
Creating file /home/ubuntu18/weiquan/sphnix-demo/doc/Makefile.
Creating file /home/ubuntu18/weiquan/sphnix-demo/doc/make.bat.

Finished: An initial directory structure has been created.
...

3.修改配置文件

修改doc/source下的conf.py文件:

...
import os
import sys
sys.path.insert(0, os.path.abspath('../../myproject')) ## 项目相对conf.py的路径
...
# sphinx.ext.napoleon: 表示支持numpy和google风格的注释
extensions = ['sphinx.ext.autodoc','sphinx.ext.napoleon']
...
# google风格的代码注释
class MyProject:
	"""This is my project"""
	def is_in_position(self, data, id=0):
        """Judge whether in the position.

        Args:
            id: 1 - coords, 0 - angles
            data: A data list, angles or coords, length 6.

        Return:
            1 : True
            0 : False
            -1: Error
        """
        ...

4.生成html文档

在doc目录下执行命令生成html文档:

make html

生成的文档会保存在doc/build/html文件夹下

生成markdown文档

1.安装依赖

pip3 install sphinx-markdown-builder

2.修改配置文件

修改doc/source下的conf.py文件:

import sphinx_markdown_builder
...
# sphinx.ext.napoleon: 表示支持numpy和google风格的注释
extensions = ['sphinx.ext.autodoc','sphinx.ext.napoleon','sphinx_markdown_builder']
...
exclude_patterns = [
    'build/*'
]

3.生成markdown文档

执行指令

sphinx-apidoc -o source ../myproject

在doc目录下运行命令:

make markdown

生成的markdown文档保存在build/markdown下面

使用Google风格的注释生成的markdown文档:
在这里插入图片描述
在编写代码的过程中顺手写上代码注释,方便维护以及后续的说明文档的生成,一举两得。

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值