1. 程式人生 > >Sphinx--python模組自動生成文件

Sphinx--python模組自動生成文件

安裝

pip install 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-quickstart , 生成Sphinx預設模板,設定專案名為demo,並開啟autodoc
  • 進入source目錄,開啟index.rst
  • 將index.rst 修改為如下,實際上這裡面可以寫任何滿足rst規範的內容
.. 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`

將demo目錄加入sys.path,所以現在開啟conf.py,新增如下內容

import os
import sys
sys.path.insert(0, os.path.abspath('../..'))

執行Sphinx生成html文件

sphinx-build -b html source build
make html

開啟build/html/index.html就可以看到如下介面了