swagger ui for flask

Solutions on MaxInterview for swagger ui for flask by the best coders in the world

showing results for - "swagger ui for flask"
Amanda
30 Jul 2017
1from flask import Flask
2from flask.ext.restplus import Api, Resource, fields
3
4app = Flask(__name__)
5api = Api(app, version='1.0', title='Todo API',
6    description='A simple TODO API extracted from the original flask-restful example',
7)
8
9ns = api.namespace('todos', description='TODO operations')
10
11TODOS = {
12    'todo1': {'task': 'build an API'},
13    'todo2': {'task': '?????'},
14    'todo3': {'task': 'profit!'},
15}
16
17todo = api.model('Todo', {
18    'task': fields.String(required=True, description='The task details')
19})
20
21listed_todo = api.model('ListedTodo', {
22    'id': fields.String(required=True, description='The todo ID'),
23    'todo': fields.Nested(todo, description='The Todo')
24})
25
26
27def abort_if_todo_doesnt_exist(todo_id):
28    if todo_id not in TODOS:
29        api.abort(404, "Todo {} doesn't exist".format(todo_id))
30
31parser = api.parser()
32parser.add_argument('task', type=str, required=True, help='The task details', location='form')
33
34
35@ns.route('/<string:todo_id>')
36@api.doc(responses={404: 'Todo not found'}, params={'todo_id': 'The Todo ID'})
37class Todo(Resource):
38    '''Show a single todo item and lets you delete them'''
39    @api.doc(description='todo_id should be in {0}'.format(', '.join(TODOS.keys())))
40    @api.marshal_with(todo)
41    def get(self, todo_id):
42        '''Fetch a given resource'''
43        abort_if_todo_doesnt_exist(todo_id)
44        return TODOS[todo_id]
45
46    @api.doc(responses={204: 'Todo deleted'})
47    def delete(self, todo_id):
48        '''Delete a given resource'''
49        abort_if_todo_doesnt_exist(todo_id)
50        del TODOS[todo_id]
51        return '', 204
52
53    @api.doc(parser=parser)
54    @api.marshal_with(todo)
55    def put(self, todo_id):
56        '''Update a given resource'''
57        args = parser.parse_args()
58        task = {'task': args['task']}
59        TODOS[todo_id] = task
60        return task
61
62
63@ns.route('/')
64class TodoList(Resource):
65    '''Shows a list of all todos, and lets you POST to add new tasks'''
66    @api.marshal_list_with(listed_todo)
67    def get(self):
68        '''List all todos'''
69        return [{'id': id, 'todo': todo} for id, todo in TODOS.items()]
70
71    @api.doc(parser=parser)
72    @api.marshal_with(todo, code=201)
73    def post(self):
74        '''Create a todo'''
75        args = parser.parse_args()
76        todo_id = 'todo%d' % (len(TODOS) + 1)
77        TODOS[todo_id] = {'task': args['task']}
78        return TODOS[todo_id], 201
79
80
81if __name__ == '__main__':
82    app.run(debug=True)
83
queries leading to this page
swagger with flask tutorialflaskrestful 2b swaggerinstall swagger flask 1 1swagger flask restfulswagger python flask tutorialswagger api python flaskflask swaggerswagger flaskswagger ui with flaskswagger for flask rest apiflask swagger examplehow to setup example in swagger flaskpython flask swagger examplehow to add swagger to flask apppython flask swagger uiflask swagger ui tutorialflask swagger documentationpython swagger flaskhow to use flask routes with swaggerflask swaggeradd swagger to flask restfulswagger flask restfulswagger ui python flaskflask swagger exampleadd swagger to flask projectflask swagger ui implemetationflask add swaggerhow toi use swagger with flask apiswagger for flaskswagger with flaskflask swagger ui tutorial using connexionadd swagger flaskswagger with python flaskflask restful 2b swaggerswagger for flask pyhonsimple swagger for flask apiflask ui swaggerflask swagger projectswagger with flask restfulflask with swaggerhow to set example in swagger flaskswagger python flaskswagger for flask pythonflask with swagger uiadd swagger to flask apimedium add swagger ui to flaskflask swagger uiflask swagger uiflask generate swagger documentationhow to install swagger in flaskswagger with flask restfulhow to use swagger ui with flaskswagger flask apiflask post api swagger uiflask swagger ui exampleflask swagger tutorialhow to reach swagger doc in flask restfulhow to configure example in swagger flaskflask api swaggerswagger ui for flask