Flask-Autodoc is a Flask extension that automatically creates documentation for your endpoints based on the routes, function arguments and docstrings.
Flask-Autodoc is compatible with Python versions 2 and 3; and it depends only on Flask.
To install Flask-Autodoc, run pip:
pip install flask-autodoc
or clone this directory and run setup:
python setup.py install
Start using Flask-Autodoc by importing it and initializing it:
from flask import Flask
from flask.ext.autodoc import Autodoc
app = Flask(__name__)
auto = Autodoc(app)
by default, Flask-Autodoc will only document the routes explicitly decorated with doc:
@app.route('/user/<int:id>')
@auto.doc()
def show_user(id):
return user_from_database(id)
to generate the documentation, use the html() method:
@app.route('/documentation')
def documentation():
return auto.html()
To access the documentation without rendering html:
@app.route('/documentation')
def documentation():
return auto.generate()
the documentation will be returned as a list of rules, where each rule is a dictionary containing:
- methods: the set of allowed methods (ie ['GET', 'POST'])
- rule: relative url (ie '/user/int:id')
- endpoint: function name (ie 'show_user')
- doc: docstring of the function
- args: function arguments
- defaults: defaults values for the arguments
To use a custom template for your documentation, give a template argument to the html method. This will use a template from the flask templates directory.
Additional arguments (other than group, groups, and template) will be passed down to the template:
auto.html(
template='custom_documentation.html'
title='My Documentation',
author='John Doe',
)
title and author will be available in the template:
<!-- templates/custom_documentation.html -->
...
{% if title is defined %}
{{title}}
{% endif %}
...
Endpoints can be grouped together in different documentation sets. It is possible for instance to show some endpoints to third party developers and have full documentation for primary developers.
To assign an endpoint to a group, pass the name of the group as argument of the doc decorator:
@app.route('/user/<int:id>')
@auto.doc('public')
def show_user(id):
to assign an endpoint to multiple groups, pass a list of group names as the groups argument to doc:
@app.route('/user/<int:id>')
@auto.doc(groups=['public','private'])
def show_user(id):
to generate the documentation for a specific group, pass the name of the group to the html or generate methods:
auto.html('public')
auto.html(groups=['public','private'])
auto.generate('public')
Apps in the examples directory are an api for a blog:
- simple is a simple app
- factory uses blueprints
Run with
python simple/blog.py
and connect to /doc/public and /doc/private to see public and private documentations.