-
Notifications
You must be signed in to change notification settings - Fork 0
Automation API documentation generation #25
Comments
Using SphinxSphinx is essentially a file generator. Given inputs, it automatically creates human readable files (html being one of the type of files it can generate) Note: |
Some more videos and documentation for sphinxhttps://www.youtube.com/watch?v=b4iFyrLQQh4: a simple demonstration on creating an auto generation doc https://realpython.com/lessons/setting-sphinx-example-project-and-scaffolding/: similar to the above, focuses on the sphinx-quickstart command (which we will most likely use) and goes in depth on the different options that can be enabled with the command https://www.sphinx-doc.org/en/master/usage/quickstart.html: Sphinx's documentation on how to get started with Sphinx (which looks like it was generated using Sphinx I might add). Is complex for documentation, but makes sense after watching some of the tutorial videos before hand. |
Installed sphinx in the virtual environment using pip Note: might need to add sphinx to the requirements list depending on how we plan to use it... Worked on trying to build an html autogenerated file based on the docstrings of the Item class in |
Docusaurus seems to be an alternative to Sphinx in that it can autogenerate a documentation website. However, Docusaurus relies on manual documentation in that Sphinx autodoc feature that collects the docstrings from python functions. https://v2.docusaurus.io/docs/installation: installation of Docusaurus |
Installation and configuration of Sphinx
# import os
# import sys
# sys.path.insert(0, os.path.abspath('..')) Ensure that the path points to the location of the script to be documented (in my case, it is in the parent directory extensions = [
'sphinx.ext.autodoc'
]
|
Creating Sphinx Auto Documentation
Code documentation
=========================================
.. autoclass:: ECNQueue.Item
:members:
:private-members:
.. autoclass:: ECNQueue.Queue
:members:
:private-members:
.. automodule:: ECNQueue
:members: This tells the sphinx parser to parse all of the private functions in the Item and Queue classes within ECNQueue.py, but also any other functions not included within a specific class. Save this file.
|
Sphinx ThemesAdditional themes are available for sphinx through html_theme = 'alabaster' to this: html_theme = 'sphinx_rtd_theme' |
Markdown and SphinxStill looking into this, but it seems that at the very least, markdown documents can be put into the sphinx directory. However, it may be possible to output auto generated code as markdown. Will look into this tomorrow. Allowing markdown to be parsed by Sphinx
# The file extensions of source files. Sphinx considers the files with this suffix as sources.
# The value is a dictionary mapping file extensions to file types
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown'
} This tells sphinx to not delete any |
Markdown in SphinxFollowing the steps above does allow for markdown to be parsed directly by sphinx and thus parsed into an html page. However, documentation doesn't appear to be able to automatically generated directly from a markdown file like what can be done with an rst file. |
Markdown and SphinxUsing the guide here, it is possible to create auto generated markdown files from the python docstrings. pip install sphinx-markdown-builder and adding this information to # add 'sphinx_markdown_builder` to the existing extensions list
extensions = [
'sphinx_markdown_builder'
]
# add the exclude_patterns list with this entry:
exclude_patterns = [
'build/*'
] |
Installation and configuration of SphinxSphinx is a documentation generation tool that pulls content from reStructuredText, or rst files. rst is similar to html in that it is a markup language, but is designed specifically for documentation. Sphinx is used to convert rst within code files and docstrings into html, which is easily readable. Sphinx has a variety of built-in themes that dictate how the html output will be formatted and any additional themes must be installed along with sphinx. In this guide we will be using and installing the sphinx_rtd_theme. Note: The starting directory for the following commands will be the npm run venv:reset Note: This command is a part of the dev. environment and can only be run within the /home/benne238/webqueue2 directory 2. Next, install sphinx and the sphinx rtd theme by running these commands: pip install Sphinx pip install sphinx_rtd_theme 3. Within the mkdir api/documentation Note: This guide will refer to this directory as the parent documentation directory. Everything related to sphinx will be located and generated here. 4. Next, create the sphinx configuration files and dependencies with in the documentation folder: cd api/documentation Sphinx-quick start generates all of the dependencies and directories needed set up and use Sphinx. These configurations are generated in the directory where the sphinx-quickstart command is run. sphinx-quickstart 5. Accept all of the default values (the values in brackets) and give the project a title, author and version number After accepting the output of the Creating file /home/pier/e/benne238/webqueue2/api/documentation/conf.py.
Creating file /home/pier/e/benne238/webqueue2/api/documentation/index.rst.
Creating file /home/pier/e/benne238/webqueue2/api/documentation/Makefile.
Creating file /home/pier/e/benne238/webqueue2/api/documentation/make.bat.
Finished: An initial directory structure has been created.
You should now populate your master file /home/pier/e/benne238/webqueue2/api/documentation/index.rst and create other documentation
source files. Use the Makefile to build the docs, like so:
make builder
where "builder" is one of the supported builders, e.g. html, latex or linkcheck. 6. Edit the main sphinx configuration file, nano conf.py Many settings and defaults that sphinx has will be located here and can also be modified in this file. 7. Uncomment these lines within the ...
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
... Note: webqueue2
└───api
├──API.py
├──ECNQueue.py
│
└───documentation
└──conf.py Ensure that the path in 8. Also within ...
extensions = [
'sphinx.ext.autodoc'
]
... 9. Finally, to configure the sphinx_rtd_theme theme, modify ...
html_theme = 'sphinx_rtd_theme'
... 10. Save the configuration and close the file Sphinx is now configured to use autodocumentation and output html with the rtd theme, however, no files exist yet to document the existing python scripts so any documentation generated now will not be useful.Sphinx Table of Contents creationSphinx generates html output based on a directory structure within the main documentation directory, thus the need arises to create a logical structure for all of the docs called a table of contents or TOC. Note: *The starting directory for the following commands will be the webqueue2
└───api
├──API.py
├──ECNQueue.py
│
└───documentation <<
└─ conf.py 1. Create a directory named mkdir docfiles 2. Create two directories named mkdir docfiles/ecnqueue mkdir docfiles/api Note: Now the (simplified) directory structure within the webqueue2
└───api
├──API.py
├──ECNQueue.py
│
└───documentation
├──conf.py
│
└───docfiles
│
├───api
└───ecnqueue
This directory structure mirrors the structure of the API in which there are two scripts: the Sphinx RST file configurationWithin Note: The starting directory for the following commands will be the webqueue2
└───api
├──API.py
├──ECNQueue.py
│
└───documentation <<
├──conf.py
├──index.rst
│
└───docfiles
│
├───api
└───ecnqueue 1. Open nano index.rst webqueue2/api/documentation/index.rst: .. webqueue2 api documentation master file, created by
sphinx-quickstart on Wed Nov 4 15:15:58 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to webqueue2 api's documentation!
========================================= This rst file will do several things:
2. Modify the .. webqueue2 api documentation master file, created by
sphinx-quickstart on Wed Nov 4 15:15:58 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to webqueue2 api's documentation!
=========================================
.. toctree::
:caption: ECNQueue:
:glob:
docfiles/ecnqueue/*
.. toctree::
:caption: API:
:glob:
docfiles/api/*
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search` However nothing in index.rst actually documents any of the docstrings within the python scripts. To do that, we will need to create several more Sphinx AutodocAutodoc is a Sphinx extension that allows sphinx to pull rst formatted docstrings from python scripts and automatically format the docstrings into html. Within the
These files ultimately allow for sphinx to document three different aspects of the
Create these files as described below: webqueue2
└───api
├──API.py
├──ECNQueue.py
│
└───documentation
├──conf.py
├──index.rst
│
└───docfiles <<
│
├───api
└───ecnqueue 1. Create the nano ecnqueue/item.rst and add these lines: webqueue2/api/documentation/docfiles/ecnqueue/item.rst: Item class Code documentation
=========================================
.. autoclass:: ECNQueue.Item
:members:
:private-members: 2. Create the nano ecnqueue/queue.rst and add these lines: webqueue2/api/documentation/docfiles/ecnqueue/queue.rst: Queue class Code documentation
=========================================
.. autoclass:: ECNQueue.Queue
:members:
:private-members: 1. Create the nano ecnqueue/ecnqueue.rst and add these lines: webqueue2/api/documentation/docfiles/ecnqueue/ecnqueue.rst: ECNQueue Script
=========================================
.. automodule:: ECNQueue
:members: The Syntax for autoclass: For now, we will skip the api rst file creation, but it is the same principle as creating Make HTMLAt this point, all previous steps have been only for the configuration for sphinx autodoc as no documentation for the API has been created yet. Lets change that. webqueue2
└───api
├──API.py
├──ECNQueue.py
│
└───documentation
├──conf.py
├──index.rst
│
└───docfiles <<
│
├───api
└───ecnqueue 1. In the terminal, navigate back to the documentation directory (where cd .. 2. Create the the documentation by running make html Note: ensure you are still within the python virtual environment. This will create several directories within the webqueue2
└───api
├──API.py
├──ECNQueue.py
│
└───documentation
├──conf.py
├──index.rst
│
├───build
│ └───html
│ └───docfiles <<
│ ├───api
│ └───docfiles
│
└───docfiles
│
├───api
└───ecnqueue Note: *The html documentation will always be created in the docfiles directory as indicated in the tree structure above * 1. Navigate to cd build/html/docfiles/ecnqueue The 2. Open one of the files and preview it. Making Changes to the html outputThe docstrings need to be written in Anytime the docstrings are edited, the entire project needs be rebuilt, luckily this can be accomplished very easily. 1. Make a change to the docstrings in either the Note: The starting directory for the following commands will be cd documentation 3. Ensure the virtual environment is running 4. Run this command in the terminal: make clean && make html Note: This will delete all of the files within the |
MkdocsDue to the complexity and messiness of Sphinx, we will now attempt to make documentation using mkdocs along with a feature called mkautodocs to automatically document the docstrings of the api in markdown similarly to Sphinx autodoc. Issues and information regarding mkdocs will be posted here. |
Mkdocs configuration and installationMkdocs is a html documentation generator which parses markdown files and outputs the contents of markdown into html. Mkautodoc is an extension of mkdocs that pulls markdown formated docstrings from python scripts without having to copy the docstrings manually. This project will make use of the mkdocs material theme. Note: The starting directory for commands will be the 1. Mkdocs, mkautodoc, and the mkdoc material theme can be installed directly with pip (ensure that the python virtual environment is running) pip install mkdocs pip install mkautodoc pip install mkdocs-material 2. Mkdocs needs a directory to contain all of the documentation related files and subdirectories, to do that: cd api This next command will create several things:
mkdocs new documentation Output: INFO - Creating project directory: documentation
INFO - Writing config file: documentation/mkdocs.yml
INFO - Writing initial docs: docuentation/docs/index.md 3. Edit the nano documentation/mkdocs.yml mkdocs.yml site_name: Webqueue2
markdown_extensions:
- admonition
- codehilite
- mkautodoc
theme:
name: material This file is used for a number of different configurations which change the output format of the html files produced my mkdocs, a list of all the flags can be found here Prepping the Python ScriptsIn order for mkautodoc to function, the Note: The starting directory for commands will be the webqueue2
└───api <<
├──API.py
├──ECNQueue.py
│
└───documentation
├──mkdocs.yml
│
└───docs
└───index.md 1. Create a nano setup.py 2. Copy these lines into the newly created
webqueue2/api/setup.py from distutils.core import setup
setup(name='webqueueapi',
version='1.0',
py_modules=['api', 'ECNqueue'],
) Note: This guide gives a more in depth explanation on the functionality and configuration of the 3. Save the 4. Package and install the pip install -e . Note: Alternatively, the direct path to the Obtaining file:///home/pier/e/benne238/webqueue2/api
Installing collected packages: webqueueapi
Running setup.py develop for webqueueapi
Successfully installed webqueueapi The DocumentationThese next steps will create the documentation from the doc strings in the python scripts. webqueue2
└───api <<
├──API.py
├──ECNQueue.py
├──setup.py
│
└───documentation
├──mkdocs.yml
│
└───docs
└───index.md 1. Navigate to the cd documentation/docs 2. Create a markdown file to document the nano ecnqueue.md 3. Copy these lines into the editor: # ECNQueue documentation
## Item Class
::: ECNQueue.Item
:docstring:
### Item functions
::: ECNQueue.Item
:members:
## Queue Class
::: ECNQueue.Queue
:docstring:
### Queue Functions
::: ECNQueue.Queue
:memebers: This specific markdown file does several things:
ECNQueue
├──Item Class
│ └───Item Class Functions
│
└──Queue Class
└───Queue Class Functions
4. Save the file and close the editor Note: The same process can be used for documenting the
Viewing the DocumentationNow that mkdocs is configured to use mkautodoc and now that there are markdown documents in the Note: *The starting directory for commands should be the webqueue2
└───api
├──API.py
├──ECNQueue.py
├──setup.py
│
└───documentation
├──mkdocs.yml
│
└───docs <<
├───ecnqueue.md
└───index.md 1. Navigate to the cd .. 2. Create a webpage using mkdocs mkdocs serve Output: INFO - Building documentation...
INFO - Cleaning site directory
INFO - Documentation built in 0.37 seconds
[I 201218 15:57:24 server:335] Serving on http://127.0.0.1:8000
INFO - Serving on http://127.0.0.1:8000
[I 201218 15:57:24 handlers:62] Start watching changes
INFO - Start watching changes
[I 201218 15:57:24 handlers:64] Start detecting changes
INFO - Start detecting changes This creates a locally available website at http://127.0.0.1:8000 that can be used to preview changes made to any of the markdown files. If a change is made to a markdown file and saved while the Note: if a change is made to the docstrings of one of the python scripts being autodocumented, there are a couple more steps to get the changes to take affect on the website preview. Modifying the DocstringsUnfortunately mkautodoc does not detect any changes made within the docstrings of the python scripts, and the solution includes stopping the 1. Stop the 2. Execute this command: This command will automatically reinstall the cd .. && pip install -e . && cd documentation && mkdocs serve |
Progress has been made on a robust nav section generator but it is still a work in progress. My first approach was to use a Python script to build a nav structure for the
My current thinking is to avoid loading the entire configuration file and instead generate just the nav section of the config file and append it to the end of the config file before running |
Work was done in #157 to help with automated docs generation. |
Next Steps:
|
I'd originally thought that the offline search mentioned above was required for search capabilities to be usable when serving the docs site from anything other than the development server available by running While it might be nice to have a locally searchable copy of the docs, enabling local search via the |
This is waiting on me. |
Next steps:
|
npm scripts for interacting with mkdocs have been added:
|
VersioningThe feature for adding a dropdown menu to allow easy access to different versions of documentation is a premium feature and the benefit of implementing a simple way to access previous versions of docs does not out-weigh the cost of doing so. To mitigate this problem, the workflow for making docs will go as follows:
This workflow will ensure that the most up-to-date code will always have the most up-to-date docs associated with it |
Moving
to #11 |
Next Steps (after API docs #195 ):
|
Moved to ECN/webqueue2-api#8 |
Python in this project adheres to section 3.8 of Google's guidelines for documenting python.
Sphinx is a popular documentation tool for Python and could be used to auto generate documentation.
This will depend on #24
The text was updated successfully, but these errors were encountered: