Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

jmcarp/flask-apispec

Repository files navigation

Latest versionDocumentation statusTravis-CICode coverage

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.

Install

pip install flask-apispec

Quickstart

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)

Documentation

https://flask-apispec.readthedocs.io/

Notes

flask-apispec is strongly inspired byFlask-RESTful andFlask-RESTplus, but attempts to provide similar functionality with greater flexibility and less code.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors29


[8]ページ先頭

©2009-2025 Movatter.jp