Setting up a Sphinx site#
19 July 2022 (last updated: 4 March 2023)
Sphinx is a static site generator that uses Python to convert RST and MD files to HTML.
To set up a Sphinx site, these are the steps:
Build a site locally.
Deploy the site for public consumption.
Personalise the site.
Publicise the site.
The entire process takes about 50 minutes or longer.
Build a site locally#
Estimated time
Prerequisites: 15 minutes
Steps: 5 minutes
Prerequisites#
Python 3.0 or later. To see if you have Python installed somewhere on your computer, open the command prompt, type
python, and press Enter.Sphinx 5.0 or later. To see if your computer has Sphinx, at the command prompt, type
sphinx-build --version, and press Enter.
Steps#
On your computer, create a directory.
Open a Python terminal and use the
cdcommand to go to this directory you just created.Create a text file called
README.rst. Optionally, add a line or two in this file.Run the
sphinx-quickstart docscommand and, at the prompts, specify the following inputs. Some files and folders are created.Separate source and build directories (y/n) [n]: Type
yand press Enter.Project name: Type the name of your site and press Enter.
Author name(s): Type your name and press Enter.
Project release [ ]: Press Enter.
Project language [en]: Press Enter.
The following files and folders are created:
docs ├── build ├── make.bat ├── Makefile └── source ├── conf.py ├── index.rst ├── _static └── _templatesRun the following command:
sphinx-build -b html docs/source/ docs/build/html.HTMLcontent is generated from thesource/index.rstfile and other files, and placed in thebuildfolder.In a browser, open the
build/index.htmlfile. This is the home page of your website.
Deploy the site for public consumption#
So that everyone in the world can see your site, push your files to your favourite deployment-solution provider.
Deploy through GitLab#
You ask GitLab to build the site for you and make it available through Pages. To do so, you first host your source on a GitLab repository, and then use a .gitlab-ci.yml file as a CI/CD file for GitLab to build and serve the site.
Estimated time
5 minutes
Sign in to GitLab and create a repository.
Add a
.gitlab-ci.ymlfile, at the root of the repository, with the following matter:stages: - deploy pages: stage: deploy image: python:3.9-slim before_script: - apt-get update && apt-get install make --no-install-recommends -y - python -m pip install sphinx script: - cd docs && make html after_script: - mv docs/build/html/ ./public/ artifacts: paths: - public rules: - if: $CI_COMMIT_REF_NAME == "main"Push the entire
docsdirectory (minus thedocs/builddirectory) from your computer to themainbranch of the GitLab repository. This is what your repository should look like:docs ├── make.bat ├── Makefile └── source ├── conf.py ├── index.rst ├── _static └── _templates .gitlab-ci.yml README.rstWait for the build to complete, after which your site is available at
<your-user-name>.gitlab.io/<your-repo-name>.
Deploy through GitHub#
You have two options for doing this.
Option 1. Upload the built HTML files#
In this method, you build the files locally, and upload only the output to GitHub. The source files are, thus, not connected to the output files.
Estimated time
5 - 7 minutes
Sign in to GitHub and create a repository.
In the
mainbranch, create adocsfolder.In the
docsfolder, create an empty file called.nojekyll.Push the contents of the local
docs/builddirectory (which you generated through Sphinx locally) tomain/docsof your GitHub repository.Enable GitHub Pages. Specify the source to be the
mainbranch anddocsfolder.Wait for the build to finish. Thereafter, your site is available at
<your-user-name>/<github-repo-name>/index.html.
Option 2: Ask GitHub to build the HTML files#
In this method, you upload the source files to GitHub, and ask GitHub to generate the output. In this method, the output files are always in sync with the source files.
I haven’t completely figured out this method yet. The GitHub build is running fine but the site deployment is going awry. After I sort it out, I’ll update this page.
Deploy through Read The Docs#
You have only one option, which is that of asking Read The Docs to build the site for you. To do so, you first host your source on a publicly available Git repository, and then use a .readthedocs.yaml file as a CI/CD file that Read The Docs will read to build the output.
Estimated time
10 minutes
Sign in to GitHub, GitLab, or BitBucket and create a repository.
Add a
.readthedocs.yamlfile, at the root of the repository, with the following matter:# Details are at https://docs.readthedocs.io/en/stable/config-file/v2.html # Required version: 2 # Set the version of Python and other tools you might need build: os: ubuntu-20.04 tools: python: "3.8" # Build documentation in the docs/ directory with Sphinx sphinx: configuration: docs/source/conf.py
Push the entire
docsdirectory (minus thedocs/builddirectory) from your computer to themainbranch of the Git repository.Sign in to Read The Docs and import this repository as a project.
Wait for the build to complete, after which your site is available at
<your-project-name>.readthedocs.io/en/latest/index.html.
Personalise the site#
Because the site is about you.
Add stuff#
Make your changes locally, test them, and then push them to your repo.
Estimated time
10 minutes or longer
Open the
index.rstfile. This file is the home page of your site. Edit it to your heart’s content.For every page that you want on your site, add an
RSTorMDfile to thedocs/sourcedirectory. Images go into thedocs/source/_staticdirectory.For every page that you want on your site, after you’ve created that page, add it as a
toctreeentry to theindex.rstfile.Test your changes while you work:
Open a Python terminal and go to the
<your-Sphinx-directory>/docsdirectory.Build the site locally by running the following command.
.\make htmlmake htmlI don't knowLook at the output in the
docs/builddirectory.
Change the theme#
The default theme of Sphinx docs is alabaster, which is a bit insipid. Find a theme that’s more you.
Estimated time
10 minutes or longer
If it’s a third-party theme, install it by running a
pip installcommand.Edit the
html_themeparameter inconf.py. The default Sphinx value isalabaster. Read the documentation of the theme of your choice to see what this value should be changed to.In the same
conf.pyfile, specify the options for HTML output. The theme documentation should have this information.If it’s a third-party theme, add a
requirements.txtfile at the root of your project (at the same level as.readthedocs.yaml) and add an entry for the theme package.
Write in Markdown (instead of ReStructured Text)#
Because some people (like me) find Markdown much more easy to handle than the recalcitrant child that ReStructured Text can often become.
Sphinx knows how to change RST file to HTML, and will do so without prompting. It also knows how to change MD files to HTML, but needs to be told specially (through the conf.py file) to do so.
Estimated time
2 minutes
Add the following entry to the
conf.pyfile:extensions = [ 'myst_parser', ]
In the
requirements.txtfile, add an entry formyst_parser. If you don’t have arequirements.txtfile, create one at the root of the project (at the same level as.readthedocs.yaml).
Publicise the site#
This part, I don’t have any gyan, except for saying, use your best offices :)