GithubHelp home page GithubHelp logo

csu-xiao-an / flask-autodoc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from acoomans/flask-autodoc

0.0 1.0 0.0 181 KB

Flask autodoc automatically creates an online documentation for your flask application.

License: MIT License

Python 87.81% HTML 12.19%

flask-autodoc's Introduction

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:

  • 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

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:

  • 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.

Screenshots

screenshots

screenshots

flask-autodoc's People

Contributors

lukeyeager avatar jwg4 avatar ballisticbuddha avatar wshayes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.