Skip to content

Instantly share code, notes, and snippets.

@arundhaj
Last active June 27, 2022 12:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save arundhaj/5f4c0f8c9a8efba9f92f81adea9fd4d7 to your computer and use it in GitHub Desktop.
Save arundhaj/5f4c0f8c9a8efba9f92f81adea9fd4d7 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
from werkzeug.utils import secure_filename
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', secure_filename(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