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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Generate API HTML Docs with Sphinx: A Quick Step‑by‑Step Tutorial

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 sphinx

Markup 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
└── example

Create docs directory

# mkdir docs
# tree -L 1 .
├── bin
├── docs
├── etc
├── ops
├── setup.py
└── example

Generate 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_theme

Configure 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 . html

See 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, delete

The resulting documentation is shown below.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonDocumentationAPIHTMLSphinxReadTheDocsreStructuredText
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.