Generate API HTML Docs with Sphinx: A Quick Step‑by‑Step Tutorial
This guide walks you through installing Sphinx, configuring the Read the Docs theme, generating reStructuredText from Python code, and building HTML documentation for your API, complete with project setup, conf.py tweaks, and custom module directives.
Background
Need to generate HTML documentation from an API for frontend engineers.
Learning Sphinx
Sphinx is a Python‑based documentation generator originally created for Python language docs, but it can be used for any project, even to write entire books.
Installation
pip install sphinxMarkup Syntax
Sphinx uses reStructuredText, a lightweight markup language similar to Markdown. See the official reST guide.
Project Structure
# tree -L 1 .
├── bin
├── etc
├── ops
├── setup.py
└── exampleCreate docs directory
# mkdir docs
# tree -L 1 .
├── bin
├── docs
├── etc
├── ops
├── setup.py
└── exampleGenerate reST files from Python code
# sphinx-apidoc -F -o docs/ ops/api/contrib
Creating file doc/contrib.rst.
Creating file docs/conf.py.
Creating file docs/index.rst.
Creating file docs/Makefile.
Creating file docs/make.bat.For detailed usage, refer to the sphinx‑apidoc documentation.
Install Read the Docs theme
# pip install sphinx_rtd_themeConfigure conf.py
import sphinx_rtd_theme
html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]Add project to Python path
import sys, os
sys.path.append(os.path.join(os.getcwd(), "../ops/api"))Build HTML documentation
# cd docs
# mkdir html
# sphinx-build . htmlSee the sphinx‑build invocation guide for more options.
Customizing generated docs
Edit contrib.rst to include only selected classes and methods:
contrib.Domain module
-------------------
.. automodule:: contrib.Domain
:undoc-members:
.. autoclass:: domains
:members: get, post, put, deleteThe resulting documentation is shown below.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
