Last active
September 19, 2015 07:44
-
-
Save jwg4/1317aed4c23075dda8ff to your computer and use it in GitHub Desktop.
An example of how to use autodoc with flask extensions which generate endpoints, such as Flask-Restless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is bsed on the example at https://github.com/acoomans/flask-autodoc/blob/master/examples/simple/blog.py | |
# It adds SQLAlchemy models, and uses Flask-Restless to create a REST API for manipulating these | |
from flask import Flask | |
from flask.ext.autodoc import Autodoc | |
from flask.ext.restless import APIManager | |
from flask.ext.sqlalchemy import SQLAlchemy | |
app = Flask(__name__) | |
app.debug = True | |
auto = Autodoc(app) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' | |
db = SQLAlchemy(app) | |
manager = APIManager(app, flask_sqlalchemy_db=db) | |
class User(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
username = db.Column(db.String(80), unique=True) | |
email = db.Column(db.String(120), unique=True) | |
class Post(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
title = db.Column(db.String(40), unique=True) | |
content = db.Column(db.String(300)) | |
# Create API endpoints for SQLAlchemy models: | |
manager.create_api(User) | |
manager.create_api(Post, methods=['GET', 'POST', 'DELETE']) | |
# These are the key lines: | |
# Decorate all endpoints with Autodoc.doc(): | |
for endpoint, function in app.view_functions.iteritems(): | |
app.view_functions[endpoint] = auto.doc()(function) | |
@app.route('/doc/') | |
def doc(): | |
return auto.html() | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment