Build your own Python package (2)
Here is the content for the series of Build your own Python package:
- Make your package installable
- Generate doc for your package
- Format your codes
- Add testing for your codes
Generate doc for your package
When your package become larger and larger, your might need a documentation for it. Instead of starting with a blank file, fortunately, Sphinx
can generate a doc based on your comments and make your life easier ;-)
- install
sphinx
pip install sphinx
-
setup
cd demo tree >>> . >>> ├── setpy.py >>> └── src >>> ├── __init__.py >>> └── test.py mkdir doc cd doc # setup following the guide sphinx-quickstart # change based on your requirement vi ./source/conf.py sphinx-apidoc -o source ../src/ make html
-
re-build
cd doc # clean build folder make clean # >>> if you change the codes >>> ## clean rst files in source folder cd source rm `ls *rst | grep -v "^index.rst$"` cd ../ sphinx-apidoc -o source ../src/ # <<< if you change the codes <<< make html
example for conf.py
WARNING
Remember to install the sphinx_rtd_theme
via`pip install sphinx_rtd_theme`
, if you want to use this theme.
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('../../src'))
# -- Project information -----------------------------------------------------
project = 'zjxpack'
copyright = '2021, Jia-Xin Zhu'
author = 'Jia-Xin Zhu'
# The full version, including alpha/beta/rc tags
release = '0.1.0'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.doctest',
'sphinx.ext.intersphinx',
'sphinx.ext.todo',
'sphinx.ext.coverage',
'sphinx.ext.mathjax',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
# pip install sphinx_rtd_theme before you use this theme
html_theme = 'sphinx_rtd_theme'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# Add latex
latex_engine = 'xelatex'
latex_elements = {
'fontpkg': r'''
\setmainfont{DejaVu Serif}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}
''',
'preamble': r'''
\usepackage[titles]{tocloft}
\cftsetpnumwidth {1.25cm}\cftsetrmarg{1.5cm}
\setlength{\cftchapnumwidth}{0.75cm}
\setlength{\cftsecindent}{\cftchapnumwidth}
\setlength{\cftsecnumwidth}{1.25cm}
''',
'fncychap': r'\usepackage[Bjornstrup]{fncychap}',
'printindex': r'\footnotesize\raggedright\printindex',
}
latex_show_urls = 'footnote'
Enjoy Reading This Article?
Here are some more articles you might like to read next: