24. Sphinxの例
n用意したファイル
• compute.py
• integration.py
• main.py
nDocstring
• テンプレートに従って記載
import argparse
from compute import myadd, mymult
from integration import AddOrMult
def mycompute(a, b, c):
"""aとbの和もしくは積を計算する
Args:
a (int): 1つ目の引数
b (int): 2つ目の引数
c (str): "add"もしくは"mult"."add"が
指定されたらa+bを,"mult"が指定されたら"a*b"を返す
Returns:
int: a+bまたはa*b
"""
if c == "add":
class AddOrMult():
"""myaddもしくはmymultを計算する
"""
def __init__(self, add_func, mult_func):
super().__init__()
self.add = add_func
self.mult = mult_func
def do(self, a, b, c):
"""aとbの和もしくは積を計算する
Args:
a (int): 1つ目の引数
b (int): 2つ目の引数
c (str): "add"もしくは"mult"."add"
が指定されたらa+bを,"mult"が指定されたら"a*b"を返す
def myadd(a, b):
"""aとbの和を計算する
Args:
a (int): 1つ目の引数
b (int): 2つ目の引数
Returns:
int: aとbの和
"""
if a > 0 and b > 0:
return a + b
if a < 0 and b < 0:
return a + b
main.py
integration.py
compute.py
25. $ docker compose exec mypython sphinx-quickstart docs
Welcome to the Sphinx 4.5.0 quickstart utility.
Please enter values for the following settings (just press Enter to
accept a default value, if one is given in brackets).
Selected root path: docs
You have two options for placing the build directory for Sphinx output.
Either, you use a directory "_build" within the root path, or you separate
"source" and "build" directories within the root path.
> Separate source and build directories (y/n) [n]: y
The project name will occur in several places in the built documentation.
> Project name: AddMult
> Author name(s): Toru Tamaki
> Project release []: 0.1
If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.
For a list of supported codes, see
https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-language.
> Project language [en]:
「y」を入力
(buildとsource
に分かれる)
プロジェクトの情報を入力
(自分の名前を入れること)
ドキュメントを作成
するディレクトリ
sphinx-quickstart:設定開始
生成され
るディレ
クトリ
設定は
conf.pyに
記録され
る
26. The project name will occur in several places in the built documentation.
> Project name: AddMult
> Author name(s): Toru Tamaki
> Project release []: 0.1
If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.
For a list of supported codes, see
https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-language.
> Project language [en]:
Creating file /mnt/docs/source/conf.py.
Creating file /mnt/docs/source/index.rst.
Creating file /mnt/docs/Makefile.
Creating file /mnt/docs/make.bat.
Finished: An initial directory structure has been created.
You should now populate your master file /mnt/docs/source/index.rst and create other documentation
source files. Use the Makefile to build the docs, like so:
make builder
where "builder" is one of the supported builders, e.g. html, latex or linkcheck.
$
日本語ならjaと入力する
(enなら英語)
sphinx-quickstart:設定開始
生成され
るディレ
クトリ
設定は
conf.pyに
記録され
る
27. # Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join('..', '..')))
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
project = 'AddMult'
copyright = '2023, Toru Tamaki'
author = 'Toru Tamaki'
release = '0.1'
# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
]
templates_path = ['_templates']
exclude_patterns = []
language = 'ja'
# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']
docs/source/conf.pyの編集
conf.pyに対する
ソースコードの
ある位置の変更
自動作成の拡張を追加
(詳しくはこちら)
テーマをRead
the Docに変更
手動で修正
29. $ docker compose exec mypython sphinx-build -b html docs/source/ docs/build/html
Running Sphinx v4.5.0
making output directory... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 5 source files that are out of date
updating environment: [new config] 5 added, 0 changed, 0 removed
reading sources... [100%] modules
looking for now-outdated files... none found
pickling environment... done
checking consistency... /mnt/docs/source/modules.rst: WARNING: document isn't included in any toctree
done
preparing documents... done
writing output... [100%] modules
generating indices... genindex py-modindex done
writing additional pages... search done
copying static files... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded, 1 warning.
The HTML pages are in docs/build/html.
$
sphinx-build;HTMLファイルの作成
docs/sourceにあるrstファイルを使って
HTMLが生成された
生成されるhtmlファイ
ルをdocs/build/htmlに
保存
HTMLファイルを生成する指定
できたHTMLをブラ
ウザで表示する(も
しくはvscodeでも表
示できる)
37. 設定ファイル:Doxyfile
# Doxyfile 1.8.10
#---------------------------------------------------------------------------
# Project related configuration options
#---------------------------------------------------------------------------
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = AddMult
PROJECT_NUMBER = 0.1
PROJECT_BRIEF = Add or Mult project
PROJECT_LOGO =
OUTPUT_DIRECTORY =
...
#---------------------------------------------------------------------------
# Configuration options related to the input files
#---------------------------------------------------------------------------
INPUT =
INPUT_ENCODING = UTF-8
FILE_PATTERNS = *.py
RECURSIVE = NO
EXCLUDE =
EXCLUDE_SYMLINKS = NO
...
#---------------------------------------------------------------------------
# Configuration options related to the HTML output
#---------------------------------------------------------------------------
GENERATE_HTML = YES
HTML_OUTPUT = html
HTML_FILE_EXTENSION = .html
HTML_HEADER =
プロジェクト名など
ソースコードのファ
イル名パターン
HTMLを生成する指
定(NOならHTMLを
作らない)
38. Doxygenの実行
設定ファイル
Doxyfileを指定
この中の
index.html
$ docker compose exec mypython doxygen Doxyfile
warning: The selected output language "japanese" has not been updated
since release 1.8.15. As a result some sentences may appear in English.
Doxygen version used: 1.9.1
Searching for include files...
Searching for example files...
Searching for images...
Searching for dot files...
...
Generating member index...
Generating file index...
Generating file member index...
Generating example index...
finalizing index lists...
writing tag file...
Running plantuml with JAVA...
lookup cache used 25/65536 hits=7 misses=25
finished...
$