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
Copy file name to clipboardExpand all lines: documentation/package-documentation-best-practices.md
+10-5Lines changed: 10 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -31,6 +31,16 @@ have 4 elements on the home page of their documentation:
31
31
***Community:** Instructions for how to help and/or get involved. This section might include a development guide.
32
32
***Documentation:** The actual project documentation. You may have two sections of documentation - the user facing documentation and your API reference.
alt: Image showing the landing page for GeoPandas documentation which has 4 sections including Getting started, Documentation, About GeoPandas, Community.
40
+
---
41
+
The documentation landing page of GeoPandas, a spatial Python library, has the 4 element specified above. Notice that the landing page is simple and directs users to each element using a Sphinx card.
42
+
```
43
+
34
44
NOTE: in many cases you can include your **README** file and your **CONTRIBUTING** files
35
45
in your documentation given those files may have some of the components listed above.
36
46
@@ -43,11 +53,6 @@ Below is an example of doing this using `myst` syntax.
In addition to your README, CONTRIBUTING and DEVELOPMENT guides,
3
+
<!-- TODO: make this into include files so we can have a summary
4
+
important points page -->
5
+
6
+
```{important}
7
+
8
+
## Take Aways: Key Python Package Tools to Use
9
+
10
+
* Use Sphinx to build your documentation
11
+
* Publish your documentation on ReadTheDocs (or GitHub pages if you are more advanced and also prefer to maintain your website locally)
12
+
* Use `myST` syntax to write your documentation
13
+
* Use sphinx gallery to write tutorials using .py files that automagically have downloadable .py and jupyter notebook files. Use nbsphinx if you prefer writing tutorials in jupyter notebook format and don't need a grid formatted gallery.
14
+
15
+
```
16
+
17
+
In addition to your:
18
+
*[README.md file](readme-file-best-practices.md),
19
+
*[CONTRIBUTING.md and development guides and LICENSE file](contributing-file.md),
20
+
4
21
you should also have user-facing documentation for your Python
5
-
package. Below we you will learn about the most common tools that
6
-
are used to build Python packaging documentation.
22
+
package. Most often, user-facing documentation is contained on a hosted
23
+
website.
24
+
25
+
Below you will learn about the most common tools that
26
+
are used to build scientific Python packaging documentation. You will also
27
+
learn about hosting options for your documentation.
28
+
29
+
Here, we focus on tools and infrastructure that you can use.
30
+
[Click here if you want to learn more about documentation best practices](package-documentation-best-practices.md).
7
31
8
32
```{note}
9
33
Examples of documentation that we love:
@@ -16,31 +40,38 @@ Examples of documentation that we love:
16
40
```
17
41
18
42
19
-
## What tools to use to build your documentation: sphinx
43
+
## Sphinx, the most common tool used in the scientific Python ecosystem
20
44
21
-
Most python packages use [sphinx](https://www.sphinx-doc.org/) to build their documentation.
45
+
Most scientific Python packages use [sphinx](https://www.sphinx-doc.org/) to
46
+
build their documentation. As such, we will focus on sphinx in this guide.
22
47
23
-
```{tip}
24
-
Sphinx is a static-site generator. A static site generator is a tool that creates
48
+
Sphinx is a [static-site generator](https://www.cloudflare.com/learning/performance/static-site-generator/). A static site generator is a tool that creates
25
49
html for a website based upon a set of templates. Sphinx is written
26
50
using Python tool which
27
51
is why it's commonly used in the Python ecosystem.
28
52
29
-
There are however other tools that could be used such as [mkdocs](https://www.mkdocs.org/). We won't
30
-
got into others tools in this guide given sphinx is currently the most
31
-
popular in the scientific Python ecosystem. However, you are welcome to use whatever documentation tool that you prefer for your Python package development!
53
+
```{tip}
54
+
There are other static site generator tools that could be used to create user-facing documentation including
55
+
[mkdocs](https://www.mkdocs.org/). We won't
56
+
discuss others tools in this guide given sphinx is currently the most
57
+
popular in the scientific Python ecosystem.
58
+
59
+
You are welcome to use whatever documentation tool that you prefer for your Python package development!
32
60
```
33
61
34
-
Sphinx can be extended with various elements including themes and directives (extensions) as follows:
62
+
### Sphinx sites can be optimized for documentation with extensions and themes
63
+
64
+
The functionality of sphinx can be extended using extensions and themes. A few
65
+
examples include:
35
66
36
67
* You can apply documentation themes for quick generation of beautiful documentation.
37
-
* You can [automatically create documentation for your package's functions and classes (API) from docstrings in your code using the autodoc extension](https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html)
38
-
* You can [run and testing code examples in your docstrings using doctest](https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html)
39
-
* While sphinx natively supports the rst syntax. You can add custom syntax parsers to support easier to write syntax such as [myst](https://myst-parser.readthedocs.io/).
68
+
* You can [automatically create documentation for your package's functions and classes (that package's API) from docstrings in your code using the autodoc extension](https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html)
69
+
* You can [run and test code examples in your docstrings using the doctest extension](https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html)
70
+
* While sphinx natively supports the `rST` syntax. You can add custom syntax parsers to support easier-to-write syntax using tools such as [myst](https://myst-parser.readthedocs.io/).
40
71
41
-
While you are free to use whatever sphinx theme you prefer,
42
-
the most common modern templates that recommend for the Python scientific
43
-
community are:
72
+
You are free to use whatever sphinx theme that you prefer. However, the most
73
+
common sphinx themes that we recommend for the Python scientific
@@ -57,46 +88,113 @@ There are three commonly used syntaxes for creating Python documentation:
57
88
1.[markdown](https://www.markdownguide.org/): Markdown is an easy-to-learn text
58
89
syntax. It is the default syntax use in Jupyter Notebooks. There are tools that you can add to a sphinx website that allow it to render markdown as html. However, using markdown to write documentation has limitations. For instance if you want to add references,
59
90
colored call out blocks and other custom elements to your documentation, you will
60
-
need to use either myST or rST.
61
-
1.[rST (ReStructured Text):](https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html). rST is the native syntax that sphinx supports. rST was the default syntax used for documentation for many years given the limitations associated with markdown. However, in recent years myST has risen to the top as a favorite for documentation given the flexibility that it allows.
62
-
1.[myST:](https://myst-parser.readthedocs.io/en/latest/intro.html) myST is a combination of vanilla of `markdown` and `rST` syntax. It is a nice option if you are more comfortable writing markdown. `myst` is preferred by many because it offers both the rich functionality
91
+
need to use either **myST** or **rST**.
92
+
1.[rST (ReStructured Text):](https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html). **rST** is the native syntax that sphinx supports. rST was the default syntax used for documentation for many years given the limitations of markdown. However, in recent years myST has risen to the top as a favorite for documentation given the flexibility that it allows.
93
+
1.[myST:](https://myst-parser.readthedocs.io/en/latest/intro.html) myST is a combination of vanilla of `markdown` and `rST` syntax. It is a nice option if you are comfortable writing markdown. `myst` is preferred by many because it offers both the rich functionality
63
94
of rST combined with a simple-to-write markdown syntax.
64
95
65
-
While you can chose to use any of the syntaxes listed above, `myST` is a
66
-
preferred syntax to because:
96
+
While you can chose to use any of the syntaxes listed above, we suggest using
97
+
`myST` because:
67
98
68
99
* It is a simpler syntax and thus easier to learn;
100
+
* The above simplicity will make it easier for more people to contribute to your documentation.
69
101
* Most of your core python package text files, such as your README.md file, are already in `.md` format
70
-
*`GitHub` and `Jupyter Notebooks` support this default syntax/file format.
102
+
*`GitHub` and `Jupyter Notebooks` support markdown thus it's more widely used in the scientific ecosystem.
71
103
72
-
There is no wrong or right when choosing a syntax to write your documentation.
73
-
Choose whichever syntax works best for you and your community!
74
104
75
105
```{tip}
76
-
If you are on the fence about myST vs rst, you might find that *myST* is easier
106
+
If you are on the fence about myST vs rst, you might find that **myST** is easier
77
107
for more people to contribute to.
78
108
```
79
109
80
-
## How to host your Python package documentation
110
+
## Sphinx extensions to support python package tutorials
111
+
112
+
Adding well constructed tutorials to your package will make it easier for someone
113
+
new to begin using your package.
114
+
115
+
There are two sphinx tools that make it easy to add tutorials to your package:
116
+
117
+
*[Sphinx Gallery](https://sphinx-gallery.github.io/stable/index.html) and
1. You can host your documentation yourself using [GitHub Pages](https://pages.github.com/) or another online hosting service.
86
-
2. You can host your documentation using [Read the Docs](https://readthedocs.org/).
128
+
If you prefer to write your tutorials using python **.py** scripts, you
129
+
may enjoy using sphinx gallery. Sphinx gallery uses **.py** files with
130
+
text and code sections that mimics the Jupyter Notebook format. When you build
131
+
your documentation, the gallery extension:
87
132
88
-
If you don't want to maintain a documentation website for your Python package,
89
-
we suggest using the Read the Docs website. Read the Docs allows you to:
133
+
1. Runs the code in each tutorial: this acts as a check to ensure your package is working as you think it is!
134
+
1. Creates a downloadable Jupyter Notebook **.ipynb** file and a **.py** script for your tutorial that a user can quickly download and run.
135
+
1. Creates a rendered **.html** page with the code elements and code outputs in a user-friendly tutorial gallery.
136
+
1. Creates a gallery landing page with visual thumbnails for each tutorial that you create
90
137
91
-
* Quickly launch a website using the documentation found in your GitHub repository.
92
-
* Track versions of your documentation as you release updates.
138
+
<!-- TODO: Add thumbnails out tutorial outputs -->
93
139
94
-
## Tutorials
95
-
* sphinx gallery...
140
+
### Sphinx Gallery benefits
141
+
* easy-to-download notebook and .py outputs for each tutorials
142
+
* .py files are easy to work with in the GitHub pull request environment.
143
+
* Nice gridded gallery output
144
+
145
+
#### Sphinx gallery challenges
146
+
147
+
The downsides of using sphinx gallery include:
148
+
149
+
* the **.py** files can be finicky to configure, particularly if you have matplotlib plot outputs.
150
+
151
+
For example: To make allow for plots to render, you need to name each file with `plot_`
152
+
at the beginning.
153
+
154
+
* Many users these days are used to working in Jupyter Notebooks. .py may be slightly less user friendly to work with
155
+
156
+
These nuances can make it challenging for potential contributors to add
157
+
tutorials to your package. This can also present maintenance challenge.
158
+
159
+
Add about the gallery setup -
160
+
```bash
161
+
tutorials/
162
+
index.rst # landing page for your gallery
163
+
tutorial.py # a tutorial
164
+
plot_tutorial.py # a tutorial that produces a plot output
165
+
tutorial_outputs/
166
+
add fils here...
167
+
```
168
+
169
+
### [nbsphinx - tutorials using Jupyter Notebooks](https://nbsphinx.readthedocs.io/en/latest/)
170
+
171
+
If you prefer to use Jupyter Notebooks to create tutorials you can use nbsphinx.
172
+
nbsphinx operates similarly to sphinx gallery in that:
173
+
174
+
* It runs your notebooks and produces outputs in the rendered tutorials
175
+
176
+
* Pro/con By default it does not support downloading of **.py** and **.ipynb** files. However you can add a [link to the notebook at the top of the page with
177
+
some additional conf.py settings (see: epilog settings)](https://nbsphinx.readthedocs.io/en/0.8.10/prolog-and-epilog.html)
# Hosting your Python package documentation and optimizing online content
2
+
3
+
## How to host your Python package documentation
4
+
5
+
We suggest that you setup a hosting for your Python package
6
+
documentation. Two, free and most commonly used ways to
7
+
quickly create a documentation website hosting environment are below.
8
+
9
+
1. You can host your documentation yourself using [GitHub Pages](https://pages.github.com/) or another online hosting service.
10
+
1. You can host your documentation using [Read the Docs](https://readthedocs.org/).
11
+
12
+
If you don't want to maintain a documentation website for your Python package,
13
+
we suggest using the Read the Docs website. Read the Docs allows you to:
14
+
15
+
* Quickly launch a website using the documentation found in your GitHub repository.
16
+
* Track versions of your documentation as you release updates.
17
+
* Provides support for Google analytics.
18
+
* Allows you to turn on integration with pull requests where you can view documentation build progress (success vs failure).
19
+
20
+
21
+
22
+
## Optimizing your documentation so search engines (and other users) find it
23
+
24
+
If you are interested in more people finding your package, you may want to
25
+
add some core sphinx extensions (and theme settings) that will help search
26
+
engines such as Google find your documentation.
27
+
28
+
29
+
### Google Analytics
30
+
Some of the [sphinx themes such as the `pydata-sphinx-theme` and
31
+
sphinx-book-theme have built in support for google analytics](https://pydata-sphinx-theme.readthedocs.io/en/latest/user_guide/analytics.html#google-analytics). However, if the theme that you chose does not offer
32
+
Google Analytics support, you can use the [`sphinxcontrib-gtagjs` extension](https://github.com/attakei/sphinxcontrib-gtagjs).
33
+
This extension will add a Google Analytics site tag to each page of your
If you are interested in optimizing your documentation for search engines such as Google, you want a sitemap.xml file. You can submit this sitemap to Google and it will index your entire site. This over time can make the content on your site more visible to others when they search.
39
+
40
+
This extension is light weight.
41
+
42
+
It [requires that you to add it to your sphinx `conf.py` extension list and site your documentation base url.](https://sphinx-sitemap.readthedocs.io/en/latest/getting-started.html).
OpenGraph is an extension that allows you to add metadata to your documentation
47
+
content pages. [The OpenGraph protocol allows other websites to provide a
48
+
useful preview of the content on your page when shared](https://www.freecodecamp.org/news/what-is-open-graph-and-how-can-i-use-it-for-my-website/#what-is-open-graph). This is important
49
+
for when the pages in your documentation are shared.
0 commit comments