API文档生成(sphinx)

1.安装

pip install Sphinx

2.使用

2.1文档手册

Sphinx 1.3.1 中文手册 (推荐查看)教程https://fengxc.me/基于python注释使用sphinx自动化生成API文档.html

2.2创建工程

新建一个文件夹sphinx_test, 并创建两个子文件夹code, doc。目录结构如下:image

进去到doc目录, 打开powershell, 执行下边命令创建工程sphinx-quickstartimage

输入y,回车image

在这里设置工程名称、作者、版本信息、语言(中文用zh_CN表示)等image

2.3修改配置

打开doc/source/conf.py, 修改一些内容

// 如果需要自动生成API文档,sphinx.ext.autodoc这个很关键
extensions = [
    'sphinx.ext.autodoc',
]

// 配置项目路径:
import os
import sys
sys.path.insert(0, os.path.abspath('../../code'))   // 这里的地址是代码路径地址 如果code下面有__init__.py文件,则可以路径为../../。
2.4生成rst文件

在code文件夹中编写自己的python代码image

image

使用sphinx-apidoc生成rst文件,-o 后面跟的是保存rst文件的路径,你的index.rst文件在哪个目录,就指定哪个目录,然后最后面是代码路径

sphinx-apidoc -o ./source ../codeimage

2.5生成html

在doc目录下,使用make命令生成html文件

使用前,先清除一下之前的生成文件 .\make.bat cleanimage

生成html, (也可以生成pdf和其他的文档类型) .\make.bat htmlimage

这块有个红色的warning,我们后面再来解决这个问题,先暂且放着。

2.6效果展示

现在我们用浏览器打开doc/build/html/index.html,显示如下:image

这是不是和我们平时看到的python文档不太一样,那是因为我们的主题没有选对

2,7改变sphinx主题

安装主题pip install sphinx_rtd_theme

导入模块:修改source/conf.py文件

# 导入模块
import sphinx_rtd_theme

# html_theme = "alabaster"修改如下,加上html_theme_path
html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

重新生成html

// 在doc目录下执行
.\make.bat clean
.\make.bat html

效果展示image

问题修复

不知道大家有没有发现,上面生成的文档左边导航栏下面是没有内容CONTENTS的,本来应该是像下面这样的:image

还记得上面提到的在make html时的那个Warning么,这就是那个导致的

那个warning的意思是说,modules.rst没有被包含,没有被什么包含呢,是没有在index.rst里面包含,毕竟我们显示是用的index.rst。

所以我们需要在index.rst里面加上modules,不知道是不是这个版本的问题,我在好多教程里面都没有提到这个,所以踩了个坑。

修改后如下(source/index.rst):

.. SphinxTest documentation master file, created by
   sphinx-quickstart on Mon Jan  8 16:51:13 2024.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to SphinxTest's documentation!
======================================

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


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

之后,再运行make clean和make html,则warning消失,页面上会显示CONTENTS

相关推荐

  1. API文档生成工具-----Knife4介绍

    2024-01-13 06:26:03       78 阅读
  2. c#中DocFx生成API帮助文档

    2024-01-13 06:26:03       37 阅读
  3. Flask 项目自动生成 API 文档的高效实践

    2024-01-13 06:26:03       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-13 06:26:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 06:26:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 06:26:03       87 阅读
  4. Python语言-面向对象

    2024-01-13 06:26:03       96 阅读

热门阅读

  1. TIDB的忘了root用户密码和数据库密码解决办法

    2024-01-13 06:26:03       40 阅读
  2. 编程笔记 html5&css&js 036 CSS概述

    2024-01-13 06:26:03       52 阅读
  3. 【Elasticsearch】Elasticsearch集群搭建详细手册

    2024-01-13 06:26:03       58 阅读
  4. 给el-select的change事件传自己想要的参数

    2024-01-13 06:26:03       46 阅读
  5. Ubuntu按转发HDF5

    2024-01-13 06:26:03       51 阅读
  6. python类装饰器编写单体类

    2024-01-13 06:26:03       58 阅读
  7. 第一天业务题

    2024-01-13 06:26:03       58 阅读
  8. pytest框架

    2024-01-13 06:26:03       55 阅读
  9. 《设计模式的艺术》笔记 - 工厂方法模式

    2024-01-13 06:26:03       48 阅读
  10. Python pytest入门教程

    2024-01-13 06:26:03       54 阅读
  11. 怎么节约cdn流量- 速盾网络(Sudun)

    2024-01-13 06:26:03       53 阅读
  12. 人机协同中的偏序关系

    2024-01-13 06:26:03       64 阅读
  13. linux防火墙查看状态firewall、iptable

    2024-01-13 06:26:03       58 阅读
  14. 【mybatisplus使用示例】

    2024-01-13 06:26:03       57 阅读