Skip to content

Instantly share code, notes, and snippets.

@bharathjinka09
Forked from arundhaj/app.py
Created February 1, 2022 11:08
Show Gist options
  • Save bharathjinka09/5b603850f2a2effc6375ef49fcfc05f4 to your computer and use it in GitHub Desktop.
Save bharathjinka09/5b603850f2a2effc6375ef49fcfc05f4 to your computer and use it in GitHub Desktop.
Documenting Flask REST APIs with Swagger Specification and Swagger UI
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from flask import Flask, jsonify, render_template, send_from_directory
from marshmallow import Schema, fields
app = Flask(__name__, template_folder='swagger/templates')
@app.route('/')
def hello_world():
return 'Hello, World'
spec = APISpec(
title='flask-api-swagger-doc',
version='1.0.0',
openapi_version='3.0.2',
plugins=[FlaskPlugin(), MarshmallowPlugin()]
)
@app.route('/api/swagger.json')
def create_swagger_spec():
return jsonify(spec.to_dict())
class TodoResponseSchema(Schema):
id = fields.Int()
title = fields.Str()
status = fields.Boolean()
class TodoListResponseSchema(Schema):
todo_list = fields.List(fields.Nested(TodoResponseSchema))
@app.route('/todo')
def todo():
"""Get List of Todo
---
get:
description: Get List of Todos
responses:
200:
description: Return a todo list
content:
application/json:
schema: TodoListResponseSchema
"""
dummy_data = [{
'id': 1,
'title': 'Finish this task',
'status': False
}, {
'id': 2,
'title': 'Finish that task',
'status': True
}]
return TodoListResponseSchema().dump({'todo_list': dummy_data})
with app.test_request_context():
spec.path(view=todo)
@app.route('/docs')
@app.route('/docs/<path:path>')
def swagger_docs(path=None):
if not path or path == 'index.html':
return render_template('index.html', base_url='/docs')
else:
return send_from_directory('./swagger/static', path)
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment