acoomans / flask-autodoc

Flask autodoc automatically creates an online documentation for your flask application.
MIT License
98 stars 49 forks source link

Flask-Autodoc

Flask-Autodoc is a Flask extension that automatically creates documentation for your endpoints based on the routes, function arguments and docstrings.

Build Pypi version Pypi license Python 2 Python 3

Requirements

Flask-Autodoc is compatible with Python versions 2 and 3; and it depends only on Flask.

Install

To install Flask-Autodoc, run pip:

pip install flask-autodoc

or clone this directory and run setup:

python setup.py install

Usage

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()

Custom documentation

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:

Custom template

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 %}
...

Documentation sets

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')

Examples

Apps in the examples directory are an api for a blog:

Run with

python simple/blog.py

and connect to /doc/public and /doc/private to see public and private documentations.

Screenshots

screenshots

screenshots