Try to automatically generate Python documents with Sphinx

Automatically create python documentation

This is a memo about the procedure for automatically creating a python document using sphinx. For the time being, please forgive me because it is a summary in the situation of "moving". sphinx can automatically convert comments written in python code to html. This time, we will try to automate the document with a configuration that links and lists the source code of multiple directories as shown below on a common top page.

 - dirA
    - test.py
    - test2.py
 - dirB
    - testB.py
 - docs 

Initially, docs is empty because it is the directory where the automatically generated documents will be stored. Execution environment: Ubuntu18.04 Python:3.6

0. Preparation of Python program

Each python code prepared for the experiment has a comment in the source code as shown below. If you write a comment in the notation docstring, the description will be automatically added to the HTML file when using sphinx. Add one of the docstrings, Google Style, etc. as appropriate. https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html

class test():
    """test Class
    """
    
    def __init__(self):
        self.val = 0
    
    def SetVal(self, val):
       """SetVal function
       """
       self.val=val

    def GetVal(self):
        """GetVal function
        """
        return self.val

1. Install sphinx

$ sudo apt install python3-sphinx

2. Generate sample Makefile

2.1 Run sphinx-quickstart

The sphinx-quickstart command will generate a set of Makefiles for building html files in the specified directory. When you execute the command, you will be asked to enter questions and answers interactively. This time, set only the following, and execute the rest by default.

$sphinx-quickstart docs
...
ProjectName: sphinxTest
Auther name: hoge
Language: ja

After executing the above, some files and directories will be created under docs. Then edit the generated file for the environment in which you are generating the document.

2.2 Editing conf.py

import os
import sys
sys.path.insert(0, os.path.abspath('../dirA'))
sys.path.insert(0, os.path.abspath('../dirB'))
extensions = ["sphinx.ext.autodoc","sphinx.ext.autosummary"]
html_theme = 'default'

3. Automatically generate Sphinx compatible documents

3.1 Run sphinx-apidoc

Execute the following command to automatically generate the code document that exists in the two directories.

$ sphinx-apidoc -f -o docs/dirA ./dirA
$ sphinx-apidoc -f -o docs/dirB ./dirB

After executing the above command, under docs dirA / modules.rst, test.rst, test2.srt are generated. Each file is written in a format called reStructeredText. See below for details. https://www.sphinx-doc.org/ja/master/usage/restructuredtext/basics.html modules.rst is a file that is automatically generated when shinx-apidoc is executed. A list of files existing under the target directory is automatically generated. For example, this time the description is as follows.

dirA
====

.. toctree::
   :maxdepth: 4

   test
   test2

It means that modules.rst has a tree structure that refers to test.rst and test2.rst.

3.2 Editing index.rst

When you run sphinx-quickstart, a file called ./docs/index.rst is generated. Use this as the top page of the html file. If you refer to the modules of each directory from index.rst, the files in each directory will be automatically added to the html document. So, modify ./docs/index.rst.

Welcome to test's documentation!
================================

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

   ./dirA/modules
   ./dirB/modules
<Omitted below>

4. Build the html file

Finally build the html file

$ sphinx-build -b html ./docs ./docs/_build/

This will automatically generate an html file under _build. Double-click on ./docs/_build/index.html The file created this time will appear as shown below. toppage.png

codepage.png

that's all. Thank you for your hard work.

Recommended Posts

Try to automatically generate Python documents with Sphinx
I tried to automatically generate a password with Python3
Automatically build Python documentation with Sphinx
[Evangelion] Try to automatically generate Asuka-like lines with Deep Learning
Beginners automatically generate documents with Pytorch's LSTM
Try to reproduce color film with Python
Try logging in to qiita with Python
Automatically create Python API documentation with Sphinx
Try to generate an image with aliasing
Try scraping with Python.
Library comparison summary to generate PDF with Python
Try auto to automatically price Enums in Python 3.6
Try to solve the man-machine chart with Python
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
Try to detect fish with python + OpenCV2.4 (unfinished)
Try to solve the programming challenge book with python3
[First API] Try to get Qiita articles with Python
Try to make a command standby tool with python
Try to solve the internship assignment problem with Python
Try to operate DB with Python and visualize with d3
Try to automate pdf format report creation with Python
Try to factorial with recursion
Connect to BigQuery with Python
Try to understand Python self
Generate XML (RSS) with Python
Post to slack with Python 3
Try running Python with Try Jupyter
Switch python to 2.7 with alternatives
Write to csv with Python
Try face recognition with Python
Perform a Twitter search from Python and try to generate sentences with Markov chains.
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
Try to display various information useful for debugging with python
Create a Mastodon bot with a function to automatically reply with Python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
I want to automatically attend online classes with Python + Selenium!
Try to bring up a subwindow with PyQt5 and Python
Automatically generate frequency distribution table in one shot with Python
Try to automate the operation of network devices with Python
It's Halloween so I'll try to hide it with Python
Try to decipher the garbled attachment file name with Python
How to log in to AtCoder with Python and submit automatically
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
Try scraping with Python + Beautiful Soup
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Getting started with Sphinx. Generate docstring with Sphinx
Try to profile with ONNX Runtime
Convert list to DataFrame with python
MP3 to WAV conversion with Python
[python] Copy script to generate copy log
To do tail recursion with Python2
How to get started with Python
Automatically generate model relationships with Django
Try converting cloudmonkey CLI to python3 -1
What to do with PYTHON release?