automatic docs with jupyter books and github pages & actions - check main repo
184
The aim of this repository is to showcase a simple implementation of automatically generated documentation that uses Jupiter-books (build) and GitHub pages (deploy). -> like this one
create a repository or go to the repository you want to add documentation.
Create a folder in your repository called /docs where you'll host your documentation.
Add in the /docs folder the markdown files for your documentation
Create a _toc.yml file to set up your documentation table of content (see a more in-depth guide here)
as an example here's some code:
```YAML
- file: index
numbered: true
- part: Introduction
chapters:
- file: intro/intro1
- file: intro/intro2
- part: Core Code
chapters:
- file: core/main1
- file: core/main2
- part: API
chapters:
- file: api/index
```
You can skip the manual setup of the _toc.yml file by running the following code:
jupyter-book toc ./docs
and then modify the structure to your taste
add a requirements.txt file ( inside /docs) with the following dependencies:
jupyter-book
jupytext
sphinx_autodoc_typehints
create a folder /api inside /docs and create (inside) a file index.rst:
.. _api:
API Reference
=============
Information on specific functions, classes, and methods.
Modules
-------
For the average user's workflows.
.. autosummary::
:toctree:
:recursive:
numpy.sum
where numpy.sum is just to show a working implementation.
Create a _config.yml file where to setup your page configuration (see a more in depth guide here)
It's important to insert the right extension in order to build the documentation correctly, here's an example:
sphinx:
extra_extensions: - sphinx.ext.viewcode - sphinx.ext.napoleon - sphinx.ext.autodoc - sphinx_autodoc_typehints - sphinx.ext.autosummary - sphinx.ext.intersphinx
config:
autosummary_generate: True
autosummary_imported_members: True
intersphinx_mapping:
python:
- "https://docs.python.org/3"
- null
numpy:
- "https://docs.scipy.org/doc/numpy/"
- null
```
where `intersphinx_mapping` allows the build to get the documentation online for the specified packages (i.e. `numpy`).
Write a make file to build the documentation:
.PHONY: docs
docs:
rm -rf docs/_build/html
find docs/api ! -name 'index.rst' -type f -exec rm -f {} +
pip install -qr docs/requirements.txt
jb build docs
At this point the structure of the folder is ready to go, we can then move on to set up an action that will build and upload the documentation automatically.
Create a .github/workflows/ folder
Add in the folder a .yml file for your action:
name: page_deploy
on: push
# Trigger the workflow on push or pull request,
# but only for the main branch
# push:
# branches:
# - main
jobs:
deploy-book:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Install dependencies
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: "3.8"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Build the book
- name: Build the book
run: |
make docs
# Push the book's HTML to github-pages
- name: GitHub Pages action
uses: peaceiris/[email protected]
with:
github_token: ${{ secrets.ACCESS_TOKEN }}
publish_dir: ./docs/_build/html
To reuse this code you may need to change a couple of lines:
#change with the path to your files
run: |
jupyter-book build ./docs
#change with the path to your files
publish_dir: ./docs/_build/html
ACCESS_TOKEN with the one specific to your repo (see more about GitHub secrets here)If you decide to reuse this code, remember also to add a requirements.txt in the main repository for the dependencies of your code.
Note: you don't need to initiate gh-pages branch or habilitate your repo.


__init__.py file for each module. def difference(a: int, b: int):
"""
Subtracts two integers
"""
return a-b
to make the code intall-able we need to create a setup.py file in the main repository:
setup(
name='myproject',
description='a template for making documentation',
long_description=long_description,
long_description_content_type='text/markdown',
version='0.2',
author='your name',
author_email='youremail',
url='https://github.com/fedem-p/my_documentation_template',
packages=find_packages(),
include_package_data=True,
python_requires=">=3.7",
license='MIT',
zip_safe=False,
entry_points={
'console_scripts': ['myproject=myproject.entry_points:main'],
},
classifiers=[
'Intended Audience :: Developers',
'Programming Language :: Python :: 3.7',
'Natural Language :: English',
],
keywords='auto documentation'
)
for a more in-depth guide check this documentation
Now test your code by running
pip install -e .
from within the repository.
if it installs everything without errors you can further test that everything worked properly in this way:\
The last step is to update a couple of files:
Change the make file in this way:
.PHONY: docs
docs:
rm -rf docs/_build/html
find docs/api ! -name 'index.rst' -type f -exec rm -f {} +
pip install -qr docs/requirements.txt
pip install -r requirements.txt ## new line
pip install -e . ## new line
jb build docs
add to the index.rst file the modules that you want to reference:
For the average user's workflows.
.. autosummary::
:toctree:
:recursive:
numpy.sum
myproject
myproject.module
for a great example of this, you can check the napari repository
Finally, you can commit your changes and see if your documentation gets build properly. Repeat the same steps as in section number 6 and navigate to the API page. Check if there are links to your modules and functions.
Content type
Image
Digest
Size
546.8 MB
Last updated
over 5 years ago
docker pull federicopuppo/doc_template