You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Generate well formatted function and package-level documentation for Python packages using Sphinx & Read the Docs
8
8
9
-
## Package-level documentation for Python
9
+
## Documentation for Python packages
10
10
11
-
There are several levels of documentation possible for Python packages:
12
-
- code-level documentation (formatted docstrings)
13
-
- package-level documentation via [`sphinx`](https://www.sphinx-doc.org/en/master/index.html)
14
-
- package websites (via [Read the Docs](https://readthedocs.org/))
11
+
In Python, we use formatted docstrings to generate our code-level documentation.
12
+
We then use a tool called [`sphinx`](https://www.sphinx-doc.org/en/master/index.html)
13
+
to take those formatted docstrings to generate our API reference documentation
14
+
for our package website, and several of our Markdown files in our packages
15
+
GitHub repository to generate other pages for our package website
16
+
(Contributing, Code of Conduct, etc).
17
+
We then serve the website up on some platform, such as [Read the Docs](https://readthedocs.org/)
18
+
(others exist as well).
15
19
16
-
##Code & package-level documentation
20
+
### API reference docs
17
21
18
22
- We learned the basics of how to write formatted docstrings using numpy-style documentation.
19
23
20
24
- These docstrings can not only be accessed via `?function_name`, but can also be used to automatically generate package-level documentation via [`sphinx`](https://www.sphinx-doc.org/en/master/index.html)
21
25
22
-
- We already did this with our toy `foocat` package by:
23
-
- adding sphinx as a dev dependency via `poetry add --dev sphinx`
24
-
- create the `.rst` files for your package functions by running: `poetry run sphinx-apidoc -f -o docs/source <package_name>` from the project root
25
-
- and then ran `poetry run make html` from the docs directory
26
+
- We already did this with our toy `pycounts` package by:
27
+
- adding our doc dependencies into our `dev` dependency group via `poetry add --group dev myst-nb sphinx-autoapi sphinx-rtd-theme
28
+
- and then running `make html` from the `docs` directory
26
29
27
-
## Code & package-level documentation
30
+
- The `py-pkgs-cookiecutter` template also has some [extensions added to `docs/conf.py`](https://github.com/py-pkgs/py-pkgs-cookiecutter/blob/00ca3be8cd9964f863481a2ffe1763f0158087f3/%7B%7B%20cookiecutter.__package_slug%20%7D%7D/docs/conf.py#L18)
31
+
that are needed for this to work.
28
32
29
33
- To have `sphinx` correctly render the docstring as package-level documentation, we need to either write our docstrings in the correct format for restructured text (RST) or we can use the `sphinx` extension `napolean` that can render Numpy- or Google-style docstrings (which are much easier for you to write and read).
30
34
31
35
### Example of RST formatted docstrings:
32
36
33
37
```
34
-
:type path: str
38
+
""":type path: str
35
39
:param field_storage: The :class:`FileStorage` instance to wrap
36
40
:type field_storage: FileStorage
37
41
:param temporary: Whether or not to delete the file when the File
38
42
instance is destructed
39
43
:type temporary: bool
40
44
:returns: A buffered writable file descriptor
41
45
:rtype: BufferedFileStorage
46
+
:example: my_funtion(4)
47
+
"""
42
48
```
43
49
44
50
### Example of Numpy-style docstrings:
45
51
46
52
```
47
-
Summary line.
53
+
"""Summary line.
48
54
49
55
Extended description of function.
50
56
@@ -59,21 +65,16 @@ Summary line.
59
65
-------
60
66
bool
61
67
Description of return value
68
+
69
+
Examples
70
+
--------
71
+
>>> my_funtion(4)
72
+
"""
62
73
```
63
74
64
-
##Code & package-level documentation
75
+
### Rendering the docs locally
65
76
66
-
When using Numpy-style docstrings, we need to do the following:
67
-
68
-
- add `sphinxcontrib-napoleon` as a dev dependency via `poetry add --dev`
69
-
70
-
- add `extensions = ['sphinx.ext.napoleon']` to the docs configuration file (`docs/conf.py`) - we have already done this for you in the Cookiecutter template.
- As we did in the toy `foocat` package, we render the docs via running `poetry run make html` from the docs directory
75
-
76
-
-*Note: it is not essential that you locally render the docs, as we will see next that Read the Docs does this for your on their remote machines, however it is a best practice to do so because it is a lot faster than Read the Docs and therefore editing and proof-reading is more efficient when done locally.*
77
+
It is not essential that you locally render the docs, as we will see next that Read the Docs does this for your on their remote machines, however it is a best practice to do so because it is a lot faster than Read the Docs and therefore editing and proof-reading is more efficient when done locally.
77
78
78
79
## Adding new page to your package-level documentation
79
80
@@ -100,7 +101,6 @@ Which will result in this rendering:
It is common for packages to have vignettes/tutorials (think demos with narratives) showing how to use the package in a more real-world scenario than the documentation examples show. In your Python package, this ideally might go in an "Examples" section of the docs. The `pypkgs-cookiecutter` gives a template file for you to use as a starting place (`docs/example.ipynb`). As indicated in the section above you can rename this file and add others, just make sure you update the `toctree` in `docs/index.md`.
@@ -157,9 +157,17 @@ sphinx:
157
157
158
158
*note 2 - all your documentation dependencies need to be in `pyproject.toml` for ReadtheDocs to be able to successfully render your docs!*
159
159
160
-
## Publishing your Python package for this milestone:
160
+
### Sphinx themes
161
+
162
+
Sometimes you want to have a different theme for your project's docs.
163
+
This is possible by changing `html_theme` in `doc.conf.py`.
164
+
See the link below to view and read the docs for other Sphinx themes:
To get your packages README and important links to show-up on the TestPyPI and PyPI pages for your package, add the following information to the [tool.poetry] table in pyproject.toml
170
+
To get your packages `README` and important links to show-up on the TestPyPI and PyPI pages for your package, add the following information to the `[tool.poetry]` table in `pyproject.toml`.
0 commit comments