- Notifications
You must be signed in to change notification settings - Fork154
License
jmcarp/flask-apispec
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
flask-apispec is a lightweight tool for building REST APIs in Flask.flask-apispec useswebargs for request parsing,marshmallow for response formatting, andapispec to automatically generate Swagger markup. You can useflask-apispec with vanilla Flask or a fuller-featured framework likeFlask-RESTful.
pip install flask-apispec
fromflaskimportFlaskfromflask_apispecimportuse_kwargs,marshal_withfrommarshmallowimportSchemafromwebargsimportfieldsfrom .modelsimportPetapp=Flask(__name__)classPetSchema(Schema):classMeta:fields= ('name','category','size')@app.route('/pets')@use_kwargs({'category':fields.Str(),'size':fields.Str()})@marshal_with(PetSchema(many=True))defget_pets(**kwargs):returnPet.query.filter_by(**kwargs)
flask-apispec works with function- and class-based views:
fromflaskimportmake_responsefromflask_apispec.viewsimportMethodResourceclassPetResource(MethodResource):@marshal_with(PetSchema)defget(self,pet_id):returnPet.query.filter(Pet.id==pet_id).one()@use_kwargs(PetSchema)@marshal_with(PetSchema,code=201)defpost(self,**kwargs):returnPet(**kwargs)@use_kwargs(PetSchema)@marshal_with(PetSchema)defput(self,pet_id,**kwargs):pet=Pet.query.filter(Pet.id==pet_id).one()pet.__dict__.update(**kwargs)returnpet@marshal_with(None,code=204)defdelete(self,pet_id):pet=Pet.query.filter(Pet.id==pet_id).one()pet.delete()returnmake_response('',204)
flask-apispec generates Swagger markup for your view functions and classes. By default, Swagger JSON is served at /swagger/, and Swagger-UI at /swagger-ui/.
fromapispecimportAPISpecfromapispec.ext.marshmallowimportMarshmallowPluginfromflask_apispec.extensionimportFlaskApiSpecapp.config.update({'APISPEC_SPEC':APISpec(title='pets',version='v1',plugins=[MarshmallowPlugin()], ),'APISPEC_SWAGGER_URL':'/swagger/',})docs=FlaskApiSpec(app)docs.register(get_pets)docs.register(PetResource)
https://flask-apispec.readthedocs.io/
flask-apispec is strongly inspired byFlask-RESTful andFlask-RESTplus, but attempts to provide similar functionality with greater flexibility and less code.
About
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.