Skip to content

Commit e51dd62

Browse files
authored
Merge pull request ebezzam#3 from ebezzam/feat/docs
Add documentation files.
2 parents dc358fc + 2d38391 commit e51dd62

13 files changed

Lines changed: 518 additions & 10 deletions

File tree

‎.readthedocs.yaml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# .readthedocs.yaml
2+
# Read the Docs configuration file
3+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4+
5+
# Required
6+
version: 2
7+
8+
# Set the version of Python and other tools you might need
9+
build:
10+
os: ubuntu-20.04
11+
tools:
12+
python: "3.9"
13+
14+
# Build documentation in the docs/ directory with Sphinx
15+
sphinx:
16+
configuration: docs/source/conf.py
17+
18+
# If using Sphinx, optionally build your docs in additional formats such as PDF
19+
# formats:
20+
# - pdf
21+
22+
# Optionally declare the Python requirements required to build your docs
23+
python:
24+
install:
25+
- requirements: docs/requirements.txt

‎README.rst‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
python-dev-tips, `Slides <https://docs.google.com/presentation/d/1BnezhwUy22DiF72wss8GU_YIMfhjortz-uILdIFGuoM/edit?usp=sharing>`__
33
******************************************************************************************************************************************
44

5+
.. image:: https://readthedocs.org/projects/python-dev-tips/badge/?version=latest
6+
:target: http://python-dev-tips.readthedocs.io/en/latest/
7+
:alt: Documentation Status
8+
9+
10+
.. image:: https://github.com/ebezzam/python-dev-tips/actions/workflows/python.yml/badge.svg
11+
:target: https://github.com/ebezzam/python-dev-tips/blob/main/.github/workflows/python.yml
12+
:alt: Unit tests and formatting
13+
514
Tutorial first given at ENS Ulm (January 2023) on how to develop a Python package. There is a slight focus on scientific computing, but the general principles apply to any Python project.
615

716
.. contents:: **Table of Contents**
@@ -129,12 +138,12 @@ On `GitHub <https://github.com/ebezzam/python-dev-tips/tags>`__ create a new rel
129138
TODO
130139
====
131140

141+
- change documentation links to main branch
132142
- joblib example in profile
133143
- github page
134144
- point out features in scripts: object-oriented, asserts, tqdm, type hints
135145
- matplotlib, pytest, black in dev install
136146
- example file with hydra
137147
- manifest file to not include file in package
138148
- GitHub actions for releasing to PyPi when changes to version
139-
- documentation (autodoc)
140149
- adding badges to README

‎docs/Makefile‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = source
9+
BUILDDIR = build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

‎docs/make.bat‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=source
11+
set BUILDDIR=build
12+
13+
if "%1" == "" goto help
14+
15+
%SPHINXBUILD% >NUL 2>NUL
16+
if errorlevel 9009 (
17+
echo.
18+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
19+
echo.installed, then set the SPHINXBUILD environment variable to point
20+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
21+
echo.may add the Sphinx directory to PATH.
22+
echo.
23+
echo.If you don't have Sphinx installed, grab it from
24+
echo.http://sphinx-doc.org/
25+
exit /b 1
26+
)
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33+
34+
:end
35+
popd

‎docs/requirements.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sphinx==4.0.1
2+
sphinx_rtd_theme==0.4.3
3+
docutils==0.16 # >0.17 doesn't render bullets

‎docs/source/badges.rst‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Badges
2+
======
3+
4+
Adding badges to your GitHub repository and documentation amounts to simply adding an image.
5+
6+
For example, in a `reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html>`__ file:
7+
8+
.. code:: rst
9+
10+
.. image:: https://readthedocs.org/projects/python-dev-tips/badge/?version=latest
11+
:target: http://python-dev-tips.readthedocs.io/en/latest/
12+
:alt: Documentation Status
13+
14+
which when rendered will make the following badge:
15+
16+
.. image:: https://readthedocs.org/projects/python-dev-tips/badge/?version=latest
17+
:target: http://python-dev-tips.readthedocs.io/en/latest/
18+
:alt: Documentation Status
19+
20+
You can also create `badges from GitHub Actions <https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/adding-a-workflow-status-badge>`__.
21+
22+
.. code:: rst
23+
24+
.. image:: https://github.com/ebezzam/python-dev-tips/actions/workflows/python.yml/badge.svg
25+
:target: https://github.com/ebezzam/python-dev-tips/blob/main/.github/workflows/python.yml
26+
:alt: Unit tests and formatting
27+
28+
.. image:: https://github.com/ebezzam/python-dev-tips/actions/workflows/python.yml/badge.svg
29+
:target: https://github.com/ebezzam/python-dev-tips/blob/main/.github/workflows/python.yml
30+
:alt: Unit tests and formatting
31+
32+
Finally, you can use `Badgen <https://badgen.net/>`__ to create custom badges.
33+

‎docs/source/conf.py‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# This file only contains a selection of the most common options. For a full
4+
# list see the documentation:
5+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
6+
7+
# -- Path setup --------------------------------------------------------------
8+
9+
# If extensions (or modules to document with autodoc) are in another directory,
10+
# add these directories to sys.path here. If the directory is relative to the
11+
# documentation root, use os.path.abspath to make it absolute, like shown here.
12+
#
13+
# import os
14+
# import sys
15+
# sys.path.insert(0, os.path.abspath('.'))
16+
17+
import datetime
18+
import sys
19+
from unittest import mock
20+
import os
21+
22+
sys.path.insert(0, os.path.abspath(os.path.join("..", "..")))
23+
24+
MOCK_MODULES = ["matplotlib", "matplotlib.pyplot", "numpy"]
25+
for mod_name in MOCK_MODULES:
26+
sys.modules[mod_name] = mock.Mock()
27+
28+
29+
# -- Project information -----------------------------------------------------
30+
31+
project = "pydevtips"
32+
copyright = f"{datetime.date.today().year}, Eric Bezzam"
33+
author = "Eric Bezzam"
34+
35+
36+
# -- General configuration ---------------------------------------------------
37+
38+
# Add any Sphinx extension module names here, as strings. They can be
39+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
40+
# ones.
41+
extensions = [
42+
"sphinx_rtd_theme",
43+
"sphinx.ext.intersphinx",
44+
"sphinx.ext.autosummary",
45+
"sphinx.ext.autodoc",
46+
"sphinx.ext.napoleon",
47+
]
48+
49+
autodoc_default_options = {
50+
"members": True,
51+
}
52+
autodoc_typehints = "none" # for cleaner sphinx output, don't show type hints
53+
54+
intersphinx_mapping = {
55+
"python": ("https://docs.python.org/3/", None),
56+
"NumPy [latest]": ("https://docs.scipy.org/doc/numpy/", None),
57+
"matplotlib": ("http://matplotlib.sourceforge.net/", None),
58+
}
59+
intersphinx_disabled_domains = ["std"]
60+
61+
# Add any paths that contain templates here, relative to this directory.
62+
templates_path = ["_templates"]
63+
64+
# List of patterns, relative to source directory, that match files and
65+
# directories to ignore when looking for source files.
66+
# This pattern also affects html_static_path and html_extra_path.
67+
exclude_patterns = []
68+
69+
70+
# -- Options for HTML output -------------------------------------------------
71+
72+
# The theme to use for HTML and HTML Help pages. See the documentation for
73+
# a list of builtin themes.
74+
#
75+
html_theme = "sphinx_rtd_theme"
76+
77+
# Add any paths that contain custom static files (such as style sheets) here,
78+
# relative to this directory. They are copied after the builtin static files,
79+
# so a file named "default.css" will overwrite the builtin "default.css".
80+
# html_static_path = ["_static"]

‎docs/source/documentation.rst‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
Documentation
2+
=============
3+
4+
We will be using `Sphinx <https://www.sphinx-doc.org/en/master/>`__ to generate documentation from
5+
docstrings that are written within code. Docstrings are long-form
6+
comments beneath class and functions declaration that typically describe:
7+
8+
- What the function or class does.
9+
- Its inputs and their data types.
10+
- Its outputs and their data types.
11+
12+
For example:
13+
14+
.. code:: Python
15+
16+
def add(a, b):
17+
"""
18+
Add two integers.
19+
20+
Parameters
21+
----------
22+
a : int
23+
First integer.
24+
b : int
25+
Second integer.
26+
27+
Returns
28+
-------
29+
result : int
30+
Sum of inputs.
31+
32+
"""
33+
34+
assert isinstance(a, int)
35+
assert isinstance(b, int)
36+
return a + b
37+
38+
Which will be rendered as :func:`pydevtips.utils.add`.
39+
40+
Initial setup
41+
-------------
42+
43+
So you've diligently written your docstrings (or used GitHub Copilot!), and you
44+
want to generate your documentation.
45+
46+
Here are some recommended steps to get started with Sphinx:
47+
48+
#. Create a lightweight virtual environment for building the documentation. This can save a lot of time when building and publishing documentation remotely, as we'll show with `ReadTheDocs <https://readthedocs.org/>`__.
49+
50+
.. code:: bash
51+
52+
# create new environment, press enter to accept
53+
conda create -n docs_env python=3.9
54+
55+
# activate environment
56+
conda activate docs_env
57+
58+
# install requirements stored in `docs`
59+
# (may differ from our file depending on your needs)
60+
(docs_env) cd docs
61+
(docs_env) pip install -r requirements.txt
62+
63+
#. Inside your virtual environment, run ``sphinx-quickstart`` to creates a lot of the boilerplate configurations. This will guide you through a set of questions, such as your project details. We recommend creating separate ``source`` and ``build`` directories.
64+
#. Build the documentation, e.g. with ``make html``.
65+
#. Open ``docs/build/html/index.html`` in a browser to see your initial documentation!
66+
67+
Editing your documentation
68+
--------------------------
69+
70+
The ``index.rst`` file will serve as the "homepage" for your documentation (built into ``index.html``).
71+
Typically, people have the same content as their README. Before you copy-and-paste
72+
the contents (!), you can directly insert the contents with the following line.
73+
74+
.. code:: rst
75+
76+
.. include:: ../../README.rst
77+
78+
.. note::
79+
80+
`reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html>`__
81+
is the default plaintext markup language used by Sphinx. At this point, you may be thinking:
82+
*"But my README is a Markdown file (.md)..."*. While there are `tools <https://www.sphinx-doc.org/en/master/usage/markdown.html>`__
83+
to make Sphinx compatible with Markdown, I think you will save yourself more headaches to simply
84+
switch to reStructuredText. There are also `online tools <https://cloudconvert.com/md-to-rst>`__
85+
to help you with that.
86+
87+
88+
Adding new pages to your documentation amount to:
89+
90+
#. Creating new RST files.
91+
#. Including them in your ``index.rst`` file.
92+
#. Rebuilding the documentation, e.g. with ``make html``.
93+
94+
You may also need to edit the ``conf.py`` file to use different features.
95+
Check out our `index.rst <https://raw.githubusercontent.com/ebezzam/python-dev-tips/feat/docs/docs/source/index.rst>`__
96+
and `conf.py <https://github.com/ebezzam/python-dev-tips/blob/feat/docs/docs/source/conf.py>`__
97+
files for example configurations.
98+
99+
100+
You can do a clean build of your documentation with the following commands:
101+
102+
.. code:: bash
103+
104+
make clean
105+
make html
106+
107+
108+
Pro-tips
109+
--------
110+
111+
* Changing to the ReadTheDocs theme inside `conf.py <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L75>`__.
112+
* `Intersphinx <https://docs.readthedocs.io/en/stable/guides/intersphinx.html>`__ for linking to other documentations.
113+
In the ``conf.py`` file: `add <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L43>`__
114+
the Sphinx extension, and `link <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L54>`__
115+
to the other documentation. Inside your documentation you can link to the other library, e.g.
116+
for data types:
117+
118+
.. code:: Python
119+
120+
...
121+
122+
"""
123+
Parameters
124+
----------
125+
filter : :py:class:`~numpy.ndarray`
126+
"""
127+
128+
...
129+
130+
which renders as in :func:`pydevtips.fftconvolve.RFFTConvolve.__init__`
131+
with a clickable link to NumPy's documentation.
132+
* `Mock modules <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L24>`__ to keep your documentation virtual environment light.
133+
* `Add the path <https://github.com/ebezzam/python-dev-tips/blob/feat/docs/docs/source/conf.py#L22>`__
134+
to your package, so that it doesn't have to be installed (again keeping your documentation environment light!).
135+
* `Automate year <https://github.com/ebezzam/python-dev-tips/blob/ec6b15c6718b96e2c1a00496d2cf7005755d006c/docs/source/conf.py#L32>`__.
136+
137+
138+
Publishing
139+
----------
140+
141+
With a set of HTML files, there are many ways to publish your documentation online.
142+
We present one approach through `ReadTheDocs <https://readthedocs.org/>`__ (RTD), which is
143+
free and very popular among Python developers. Another popular free options is through
144+
`GitHub Pages <https://pages.github.com/>`__. I prefer RTD to not have the GitHub username or
145+
organization in the documentation URL.
146+
147+
To publish on RTD:
148+
149+
#. Make an account: https://readthedocs.org/accounts/signup/
150+
#. Import a project from the `dashboard <https://readthedocs.org/dashboard/>`__. There are two ways to do this: (1) linking your GitHub account and selecting one of your **public** repositories, or (2) importing the project manually. When linking to GitHub, the documentation is re-built whenever there are changes to the selected branch.
151+
#. Check your project page for the build status.
152+
153+
You can (optionally) define a `.readthedocs.yaml <https://github.com/ebezzam/python-dev-tips/blob/feat/docs/.readthedocs.yaml>`__
154+
file to ensure a build environment as close as possible to your local machine.

0 commit comments

Comments
 (0)