zoukankan      html  css  js  c++  java
  • 【Sphinx】 为Python自动生成文档

    sphinx

    前言

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

    开始

    1. 建一个存放文档的docs目录(跟项目路径同级),进入docs目录执行命令: sphinx-quickstart
    2. 填写相关信息

    修改配置文件 conf.py

    • 设置要处理的路径
    import os
    import sys
    # path_one为当前路径
    path_one = '..'
    # path_two为项目路径
    path_two = '../project_name'
    sys.path.insert(0, os.path.abspath(path_one))
    sys.path.insert(1, os.path.abspath(path_two))
    
    • 设置文档主题
    import sphinx_hand_theme
    html_theme = "sphinx_hand_theme"
    html_theme_path = [sphinx_hand_theme.get_html_theme_path()]
    
    • 参考conf.py
    # Configuration file for the Sphinx documentation builder.
    #
    # This file only contains a selection of the most common options. For a full
    # list see the documentation:
    # http://www.sphinx-doc.org/en/master/config
    
    # -- Path setup --------------------------------------------------------------
    
    # If extensions (or modules to document with autodoc) are in another directory,
    # add these directories to sys.path here. If the directory is relative to the
    # documentation root, use os.path.abspath to make it absolute, like shown here.
    #
    import os
    import sys
    sys.path.insert(0, os.path.abspath('..'))
    sys.path.insert(1, os.path.abspath('../project_name'))
    
    # 注意:这里需要安装主题
    import sphinx_hand_theme
    html_theme = "sphinx_hand_theme"
    html_theme_path = [sphinx_hand_theme.get_html_theme_path()]
    
    
    
    # -- Project information -----------------------------------------------------
    
    project = 'api_name'
    copyright = '2019, author_name'
    author = 'author_name'
    
    
    # -- General configuration ---------------------------------------------------
    
    # Add any Sphinx extension module names here, as strings. They can be
    # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
    # ones.
    extensions = ['sphinx.ext.autodoc', 
        'sphinx.ext.autosummary',
        'sphinx.ext.doctest',
        'sphinx.ext.intersphinx',
        'sphinx.ext.todo',
        'sphinx.ext.coverage',
        'sphinx.ext.mathjax',]
    
    # Add any paths that contain templates here, relative to this directory.
    templates_path = ['_templates']
    
    # The language for content autogenerated by Sphinx. Refer to documentation
    # for a list of supported languages.
    #
    # This is also used if you do content translation via gettext catalogs.
    # Usually you set "language" from the command line for these cases.
    language = 'zh_CN'
    
    # List of patterns, relative to source directory, that match files and
    # directories to ignore when looking for source files.
    # This pattern also affects html_static_path and html_extra_path.
    # 注意:这里是命令:**sphinx-quickstart**选择‘n’时才用这个配置,即不分开文件夹
    exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
    
    
    # -- Options for HTML output -------------------------------------------------
    
    # The theme to use for HTML and HTML Help pages.  See the documentation for
    # a list of builtin themes.
    #
    html_theme = 'alabaster'
    
    # Add any paths that contain custom static files (such as style sheets) here,
    # relative to this directory. They are copied after the builtin static files,
    # so a file named "default.css" will overwrite the builtin "default.css".
    html_static_path = ['_static']
    
    

    生成所需的 .rst 文档

    1. 在docs目录执行命令:sphinx-apidoc -f -o path_a path_b
    2. 注意:path_a为./docs,path_b要和path_two一致,
    3. 例子:
      sphinx-apidoc -f -o D:/code/project_name/docs D:/code/project_name

    生成 HTML

    1. 在docs目录执行命令:./make.bat html

    参考链接

    使用Sphinx为你的python模块自动生成文档
    使用 sphinx 制作简洁而又美观的文档

  • 相关阅读:
    HTTP基础及telnet简单命令
    【详解】并查集高级技巧:加权并查集、扩展域并查集
    二叉树中两节点的最近公共父节点(360的c++一面问题)
    (用POJ 1160puls来讲解)四边形不等式——区间dp的常见优化方法
    详细讲解Codeforces Round #625 (Div. 2)E
    详细讲解Codeforces Round #625 (Div. 2) D. Navigation System
    详细讲解Codeforces Round #624 (Div. 3) F. Moving Points
    详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)
    POJ
    新手建站前需要做哪些准备?
  • 原文地址:https://www.cnblogs.com/parzulpan/p/11956229.html
Copyright © 2011-2022 走看看