Read the Docs
Overview
Read the Docs is a widely-used platform for hosting and publishing HTML documentation built with Sphinx. It is particularly popular among open source projects due to its free hosting capabilities. The platform supports various documentation systems and offers features such as importing projects from repositories, configuring settings, and automating documentation builds, including those triggered by GitHub pull requests.
Importing Projects
Upon logging into Read the Docs, users are greeted with a dashboard that displays any projects they have already imported. The dashboard prominently features an "Import a Project" button, which initiates the process of adding new projects (see Figure 8.4).
Figure 8.4 The Read the Docs dashboard shows you any projects you’ve already imported, with a callout to import new projects.
When importing a project, Read the Docs prompts users to select a repository. Users can choose from any public repository they own or are part of an organization they belong to (see Figure 8.5).
Figure 8.5 Read the Docs can import any public repositories from your account or any organizations you’re in.
After selecting a repository, users are guided through a series of settings pages. The main settings page is shown in Figure 8.6, and the advanced settings page is depicted in Figure 8.7.
Figure 8.6 The main Read the Docs settings when importing a project
Figure 8.7 The advanced Read the Docs settings when importing a project
Once the import process is complete, the project page is displayed, showing the initial settings and indicating that no builds have occurred yet (see Figure 8.8).
Figure 8.8 The page for a Read the Docs project just after you import a repository from GitHub. It may not have any builds yet.
Configuring Read the Docs
To configure a project on Read the Docs, a .readthedocs.yaml file is created in the root directory of the project. This file specifies various settings, including the configuration schema version, the path to the Sphinx configuration, output formats, the operating system, the Python version, and the method for installing dependencies. Below is an example configuration file (Listing 8.11):
Listing 8.11 A Read the Docs configuration file
version: 2 #1
sphinx:
configuration: docs/conf.py #2
formats:
- htmlzip #3
build:
os: ubuntu-20.04 #4
tools:
python: "3.10" #5
python:
install:
- method: pip #6
path: . #7
#1 Read the Docs configuration schema version
#2 Location of the Sphinx configuration
#3 Builds only the HTML output
#4 How to specify a nondefault operating system
#5 How to specify a nondefault Python version—remember the quotes.
#6 Uses pip to install a package before building documentation
#7 Installs the package located in the root project directory
Automating Builds
Read the Docs can automate builds for GitHub pull requests. This is done by navigating to the project's page, selecting Admin, then Advanced Settings, and enabling the option to build pull requests. This feature allows Read the Docs to add a status to pull requests, indicating whether the documentation build was successful.
Running sphinx-apidoc
To run sphinx-apidoc on Read the Docs, you can leverage Sphinx's plugin-based architecture to hook into the builder-inited event. This involves adding a setup function in the docs/conf.py module to execute sphinx-apidoc programmatically. The following code snippet (Listing 8.12) demonstrates how to achieve this:
Listing 8.12 Making sphinx-apidoc run as part of the Read the Docs build process
if os.environ.get("READTHEDOCS") == "True": #1
from pathlib import Path
PROJECT_ROOT = Path(__file__).parent.parent #2
PACKAGE_ROOT = PROJECT_ROOT / "src" / "imppkg"
def run_apidoc(_): #3
from sphinx.ext import apidoc #4
apidoc.main([ #5
"--force",
"--implicit-namespaces",
"--module-first",
"--separate",
"-o",
str(PROJECT_ROOT / "docs" / "reference"), #6
str(PACKAGE_ROOT),
str(PACKAGE_ROOT / "*.c"),
str(PACKAGE_ROOT / "*.so"),
])
def setup(app): #7
app.connect('builder-inited', run_apidoc) #8
#1 Only run this in the Read the Docs environment.
#2 Calculates the needed project paths
#3 A function to run sphinx-apidoc
#4 Imports sphinx-apidoc
#5 Calls sphinx-apidoc with the same arguments as elsewhere
#6 Paths must be properly relative and must be strings.
#7 Called by Sphinx during the build
#8 Calls run_apidoc just before the main build
By implementing these configurations and scripts, Read the Docs can effectively build and host your project's documentation, ensuring it is always up-to-date with the latest code changes.
FAQ (Frequently asked questions)
What is Read the Docs?
Is Read the Docs free for open source projects?
What features does Read the Docs offer?
What does the Read the Docs dashboard display?
What is the purpose of a Read the Docs configuration file?
How do you specify a nondefault operating system in Read the Docs?
How do you specify a nondefault Python version in Read the Docs?
How do you configure Read the Docs?
How can you run sphinx-apidoc on Read the Docs?
How do you automate builds for GitHub pull requests on Read the Docs?
What does automating builds for GitHub pull requests on Read the Docs do?