Skip to content

Commit 8c739ba

Browse files
committed
cleaned up docs page
1 parent 21d1f38 commit 8c739ba

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

book/lectures/205-package_documentation_python.qmd

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,51 @@ title: "Package Documentation: Python"
66

77
- Generate well formatted function and package-level documentation for Python packages using Sphinx & Read the Docs
88

9-
## Package-level documentation for Python
9+
## Documentation for Python packages
1010

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).
1519

16-
## Code & package-level documentation
20+
### API reference docs
1721

1822
- We learned the basics of how to write formatted docstrings using numpy-style documentation.
1923

2024
- 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)
2125

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
2629

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.
2832

2933
- 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).
3034

3135
### Example of RST formatted docstrings:
3236

3337
```
34-
:type path: str
38+
""":type path: str
3539
:param field_storage: The :class:`FileStorage` instance to wrap
3640
:type field_storage: FileStorage
3741
:param temporary: Whether or not to delete the file when the File
3842
instance is destructed
3943
:type temporary: bool
4044
:returns: A buffered writable file descriptor
4145
:rtype: BufferedFileStorage
46+
:example: my_funtion(4)
47+
"""
4248
```
4349

4450
### Example of Numpy-style docstrings:
4551

4652
```
47-
Summary line.
53+
"""Summary line.
4854
4955
Extended description of function.
5056
@@ -59,21 +65,16 @@ Summary line.
5965
-------
6066
bool
6167
Description of return value
68+
69+
Examples
70+
--------
71+
>>> my_funtion(4)
72+
"""
6273
```
6374

64-
## Code & package-level documentation
75+
### Rendering the docs locally
6576

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.
71-
72-
## Editing & Rendering package-level documentation
73-
74-
- 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.
7778

7879
## Adding new page to your package-level documentation
7980

@@ -100,7 +101,6 @@ Which will result in this rendering:
100101
<img src="img/rendered-index-headers.png" width=1000>
101102

102103

103-
104104
## Vignettes/tutorials
105105

106106
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:
157157
158158
*note 2 - all your documentation dependencies need to be in `pyproject.toml` for ReadtheDocs to be able to successfully render your docs!*
159159

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:
165+
166+
- Sphinx themes gallery: <https://sphinx-themes.org/>
167+
168+
### Documentation metadata in `pyproject.toml`
161169

162-
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`.
163171

164172
```
165173
readme = "README.md"

0 commit comments

Comments
 (0)