|
| 1 | +# Python Package Documentation |
| 2 | + |
| 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. *Both of these tools will run your tutorials from beginning to end providing an addition layer of testing to your package!* |
| 14 | +* OPTIONAL: Use [doctest](https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html) to run the examples in your code's docstrings as a way to make sure that your code's functions and methods (the API) are running as you expect them to. |
| 15 | +``` |
| 16 | + |
| 17 | +In addition to your: |
| 18 | +* [README.md file](readme-file-best-practices), |
| 19 | +* [CONTRIBUTING.md and development guides and LICENSE file](contributing-license-coc), |
| 20 | + |
| 21 | +you should also have user-facing documentation for your Python |
| 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). |
| 31 | + |
| 32 | +```{note} |
| 33 | +Examples of documentation websites that we love: |
| 34 | +
|
| 35 | +* [GeoPandas](https://geopandas.org/en/stable/) |
| 36 | + * [View rst to create landing page](https://raw.githubusercontent.com/geopandas/geopandas/main/doc/source/index.rst) |
| 37 | +* [verde](https://www.fatiando.org/verde/latest/) |
| 38 | + * [View verde landing page code - rst file.](https://github.com/fatiando/verde/blob/main/doc/index.rst) |
| 39 | +* [Here is our documentation if you want to see a myST example of a landing page.](https://github.com/pyOpenSci/python-package-guide/blob/main/index.md) |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +## Sphinx, the most common tool used in the scientific Python ecosystem |
| 44 | + |
| 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. |
| 47 | + |
| 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 |
| 49 | +html for a website based upon a set of templates. Sphinx is written |
| 50 | +using Python tool which |
| 51 | +is why it's commonly used in the Python ecosystem. |
| 52 | + |
| 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! |
| 60 | +``` |
| 61 | + |
| 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: |
| 66 | + |
| 67 | +* You can apply documentation themes for quick generation of beautiful documentation. |
| 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/). |
| 71 | + |
| 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 |
| 74 | +community include: |
| 75 | + |
| 76 | +* [pydata-sphinx-theme](https://pydata-sphinx-theme.readthedocs.io/) |
| 77 | +* [sphinx-book-theme](https://sphinx-book-theme.readthedocs.io/) |
| 78 | +* [furo](https://pradyunsg.me/furo/quickstart/) |
| 79 | + |
| 80 | + |
| 81 | +```{tip} |
| 82 | +This book is created using sphinx and the `furo` theme. |
| 83 | +``` |
| 84 | + |
| 85 | +## Documentation syntax: markdown vs. myST vs. rst syntax to create your docs |
| 86 | + |
| 87 | +There are three commonly used syntaxes for creating Python documentation: |
| 88 | +1. [markdown](https://www.markdownguide.org/): Markdown is an easy-to-learn text |
| 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, |
| 90 | +colored call out blocks and other custom elements to your documentation, you will |
| 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 |
| 94 | +of rST combined with a simple-to-write markdown syntax. |
| 95 | + |
| 96 | +While you can chose to use any of the syntaxes listed above, we suggest using |
| 97 | +`myST` because: |
| 98 | + |
| 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. |
| 101 | +* Most of your core python package text files, such as your README.md file, are already in `.md` format |
| 102 | +* `GitHub` and `Jupyter Notebooks` support markdown thus it's more widely used in the scientific ecosystem. |
| 103 | + |
| 104 | + |
| 105 | +```{tip} |
| 106 | +If you are on the fence about myST vs rst, you might find that **myST** is easier |
| 107 | +for more people to contribute to. |
| 108 | +``` |
| 109 | + |
0 commit comments