sphinx创建python项目文档

本地构建文档

1、安装sphinx

2、初始化项目

sphinx-quickstart
  1. 中文:zh_CN
  2. 主题:html_theme = 'sphinx_rtd_theme'

3、配置config

在source下设置配置文件:

# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

import os
import sys

sys.path.insert(0, os.path.abspath('../'))
# os.path.abspath('../../') 需要指向项目根目录
project = 'xxxxxx'
copyright = '2024, elfin'
author = 'elfin'
release = 'v1.0.0'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
    'myst_parser',
    'sphinx_copybutton',
    'sphinx.ext.duration',
    'sphinx.ext.doctest',
    'sphinx.ext.autodoc',
    'sphinx.ext.autosummary',
    'sphinx.ext.intersphinx',
    'sphinx.ext.mathjax',
    'sphinx.ext.viewcode'       # 显示源代码
]
source_suffix = {
    ".rst": "restructuredtext",
    ".txt": "markdown",
    ".md": "markdown",
}

intersphinx_mapping = {
    'python': ('https://docs.python.org/3/', None),
    'sphinx': ('https://www.sphinx-doc.org/en/master/', None),
}
intersphinx_disabled_domains = ['std']

templates_path = ['_templates']
exclude_patterns = []

language = 'zh_CN'

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']

# -- Options for EPUB output
epub_show_urls = 'footnote'

4、生成rst文件

sphinx-apidoc -o ./source目录 ./SRC源代码目录

5、生成html

make html

查看:

  1. 直接浏览器打开html文件;
  2. python3 -m http.server 10010
posted @ 2024-06-28 16:52  巴蜀秀才  阅读(35)  评论(0编辑  收藏  举报