[PYTHON] Get started with the documentation tool Sphinx

Introduction

Explain in this entry

--From the introduction of the Python document creation tool "Sphinx" to simple document generation --Installation and usage of the hot reload module "sphinx-autobuild" --How to change the theme

Not explained in this entry

--How to install python3 and pip --Detailed description of reStructuredText --Comparison with other documentation tools like MkDocs --Detailed options of Sphinx, etc.

Premise

--Installing python3 system

Writer's environment

Create a virtual environment for Sphinx with venv

Create a virtual environment directory for Sphinx. Directory name is arbitrary

$ mkdir sphinx

Go to the directory you created and create a virtual environment The virtual environment name is sphinx-venv here.

$ cd sphinx
# 「python3 -You can create a virtual environment by using "m venv virtual environment name".
$ python3 -m venv sphinx-venv

Go to the virtual environment directory and enable the virtual environment

$ cd sphinx-venv
$ source bin/acitvate

Install sphinx with pip

$ pip install sphinx

Create a sphinx project

When you hit sphinx-quickstart, you'll hear a sloppy message and you'll be asked a few interactively. If you are not particular about it for the time being, you can set it as follows.

$ sphinx-quickstart
>Separate source directory and build directory (y/ n) [n]:y
>Project name: test-project
>Author name (s): test-author
>Project release[]: 1.0.0
>Project language[en]: ja

In my execution environment, I was asked in Japanese as above, Depending on the environment, you will be asked in English as follows.

$ sphinx-quickstart
> Separate source and build directories (y/N) [n]:y
> Project name: test-project
> Author name(s): test-author
> Project version []: 1.0.0
> Project language [en]: ja

Generate html file

First, generate an html file in the default state. To generate an html file, type the following command where the Makefile is located

$ make html

This will generate a file called index.html under the build directory. Please display it with any browser.

Add content

In Sphinx, to add content,

--Create a new file and --Register on the top page

is needed.

Create a new file

Create a file with an appropriate name under "source". Here, the file name is "test.rst".

Write the contents appropriately.

test.rst


===================
Put the title here
===================

Put a big headline
---------------
-It's a bullet point
-It's a bullet point

Register on the top page

"Index.rst" under "source" is the top page. Edit this file and register the "test.rst" created earlier. For editing, add the same name as the file name as shown below.

index.rst (before editing)


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

index.rst (after editing)


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

   test

Now, if you execute "make html" again, you will see the top page. "Put a title here" will be displayed with a link. If you click this link, you can see the contents described in test.rst.

[Bonus] Allow hot reload

In Sphinx, you usually have to edit the document and then type "make html" to reload the browser. This is awkward, so when you edit the document, it will automatically generate an html file so that it will be reloaded automatically.

$ pip install sphinx-autobuild

After installation, type the following command in the directory where "Makefile" is located.

# sphinx-autobuild [xxx.Directory with rst] [Directory where html files are generated]
#In this entry, it looks like this:
$ sphinx-autobuild source build

Then, the server will start locally, so if you display the URL (http : // localhost: 8080) with a browser, you can view the created html file.

[Bonus] Change the theme

There is the following description at the bottom of "conf.py" under the "source" directory.

conf.py


html_theme = 'alabaster'

You can change the theme by rewriting'alabaster'. Note that'alabaster'is the default theme.

The following standard themes are available in Sphix.

You can also set a third-party theme. For example, if you want to set the theme of Material Design, install the theme as follows.

$ pip install sphinx-theme-material

After installing the theme, set conf.py as follows: After setting, the theme has changed to Material Design.

conf.py


html_theme = 'material'

Recommended Posts

Get started with the documentation tool Sphinx
Get started with MicroPython
Get started with Mezzanine
The easiest way to get started with Django
Get started with Django! ~ Tutorial ⑤ ~
Get started with influxDB + Grafana
Get started with Django! ~ Tutorial ④ ~
Get started with Django! ~ Tutorial ⑥ ~
Get started with Python! ~ ② Grammar ~
Compile Sphinx documentation with Wercker
Minimum knowledge to get started with the Python logging module
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Get started with MicroPython (on macOS)
Getting started with Sphinx. Generate docstring with Sphinx
Get the weather with Python requests
How to get started with Scrapy
How to get started with Python
How to get started with Django
Automatically build Python documentation with Sphinx
Get started with machine learning with SageMaker
Get started with Python in Blender
Get started with the Python framework Django on Mac OS X
I tried to get started with Bitcoin Systre on the weekend
Write documentation in Sphinx with Python Livereload
Step notes to get started with django
Automatically create Python API documentation with Sphinx
I tried to get started with Hy
[Python] Get the variable name with str
Get Started with TopCoder in Python (2020 Edition)
Get holidays with the Google Calendar API
How Python beginners get started with Python with Progete
How to get started with laravel (Linux)
[Blender x Python] Let's get started with Blender Python !!
Python hand play (let's get started with AtCoder?)
How to get started with the 2020 Python project (windows wsl and mac standardization)
[Python] Get the files in a folder with Python
Get the latest AMI information with the AWS CLI
Get started with Python on macOS Big Sur
Get the latest Linux kernel version with Arch Linux
Until the Sphinx documentation is published on GitHub
A layman wants to get started with Python
Create a translation tool with the Translate Toolkit
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
I want to get started with the Linux kernel, what is the list head structure?
How to get started with Visual Studio Online ~ The end of the environment construction era ~
Get the sum of each of multiple columns with awk
I tried to get started with blender python script_Part 01
[Python] Get the numbers in the graph image with OCR
I tried to get started with blender python script_Part 02
Get the result in dict format with Python psycopg2
Get swagger.json with Flask-RESTX (Flask-RESTPlus) without starting the server
Try to get the contents of Word with Golang
Get comments and subscribers with the YouTube Data API
Get the operation status of JR West with Python
Get the full path referenced by .lnk with wsl
What I did to get started with Linux commands
Django 1.11 started with Python3.6
Get started Jupyter Notebook
Getting started with Android!
Make the tool simply