Use sphinx to automatically generate python API documentation

Sphinx can automatically obtain the ('''''' comment) in the code and automatically generate the documentation, and then we will start to use it to generate the documentation

  1. Run  sphinx-quickstart doc in the project root directory , the example is as follows     # api document is placed in this directory

333

2 Follow the prompts step by step, I only deal with the following items, the others are the default enter (you can enter conf to modify if necessary)
> Separate source and build directories (y/n) [n]: y
> Project name: TEST
> Author name(s): XF
> Project language [en]: zh_cn
> autodoc: automatically insert docstrings from modules (y/n) [n]: y
> viewcode: include links to the source code of documented Python objects (y/n) [n]: y

3. Modify doc/source/conf.py
    import os
    import sys
    import django # This is best to load the top and put it together with other imports
    sys.path.insert(0, os.path.abspath('../.. ')) #PATH refers to the project root directory
    # Next, add settings to the environment variable, and this configuration will be used when it is started
    os.environ['DJANGO_SETTINGS_MODULE'] ='JCSSJK.settings'
    # Key, use this Sentence load module and context
    django.setup()   

3333

4. Generate code documents and go to the project root directory

sphinx-apidoc -o doc/source . 

Note: -o is followed by the path to save the rst file, which directory your index.rst is in, then you specify which directory. Then in the back is your project (code) path

5. Make html

So far, the automatic document generation has been basically completed. The default theme may be a bit ugly at first, you can use sphinx_rtd_theme, first download it with pip and then modify it in conf:444

In addition, if you want to modify the content of the rst file, you can refer to the official document: http://www.pythondoc.com/sphinx/rest.html#id15

Finally, show the document I generated, don’t spray if you don’t like it, haha ​​==:

 

 

Recommended reading: https://mp.weixin.qq.com/s/rCSZAxIhEYRXBIrRfV2haw

Guess you like

Origin blog.csdn.net/lin_keys/article/details/84310214