flask-apispec
flask-apispec is a lightweight tool for building REST APIs in Flask. flask-apispec uses webargs for request parsing, marshmallow for response formatting, and apispec to automatically generate Swagger markup. You can use flask-apispec with vanilla Flask or a fuller-featured framework like Flask-RESTful.
Install
pip install flask-apispec
Quickstart
from flask import Flask
from flask_apispec import use_kwargs, marshal_with
from marshmallow import Schema
from webargs import fields
from .models import Pet
app = Flask(__name__)
class PetSchema(Schema):
class Meta:
fields = ('name', 'category', 'size')
@app.route('/pets')
@use_kwargs({'category': fields.Str(), 'size': fields.Str()})
@marshal_with(PetSchema(many=True))
def get_pets(**kwargs):
return Pet.query.filter_by(**kwargs)
flask-apispec works with function- and class-based views:
from flask import make_response
from flask_apispec.views import MethodResource
class PetResource(MethodResource):
@marshal_with(PetSchema)
def get(self, pet_id):
return Pet.query.filter(Pet.id == pet_id).one()
@use_kwargs(PetSchema)
@marshal_with(PetSchema, code=201)
def post(self, **kwargs):
return Pet(**kwargs)
@use_kwargs(PetSchema)
@marshal_with(PetSchema)
def put(self, pet_id, **kwargs):
pet = Pet.query.filter(Pet.id == pet_id).one()
pet.__dict__.update(**kwargs)
return pet
@marshal_with(None, code=204)
def delete(self, pet_id):
pet = Pet.query.filter(Pet.id == pet_id).one()
pet.delete()
return make_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/.
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask_apispec.extension import FlaskApiSpec
app.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
Notes
flask-apispec is strongly inspired by Flask-RESTful and Flask-RESTplus, but attempts to provide similar functionality with greater flexibility and less code.
Metadata
Release files for flask-apispec 0.11.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flask-apispec-0.11.4.tar.gz | 2.3 MB | Without provenance | Details for flask-apispec-0.11.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_apispec-0.11.4-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for flask_apispec-0.11.4-py2.py3-none-any.whl |
Total release size: 4.7 MB
Release files / flask-apispec-0.11.4.tar.gz
| Download URL | flask-apispec-0.11.4.tar.gz |
|---|---|
| Size | 2.3 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a6fedd3a06510a8a19340df676393b336b4ba9d5c159cbb304b62672e3115da
|
|
BLAKE2b-256 checksum How to use checksums |
aca190402387bbe235f3206c0aa0bd1815fce042ac2370b1d3c8ceaa16149074
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/26.0 requests/2.24.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.48.2 importlib-metadata/4.11.3 keyring/21.3.0 rfc3986/1.4.0 colorama/0.4.3 CPython/3.8.5
|
Release files / flask_apispec-0.11.4-py2.py3-none-any.whl
| Download URL | flask_apispec-0.11.4-py2.py3-none-any.whl |
|---|---|
| Size | 2.4 MB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
dba490a42ffe5e45040a667a5551678099c677eb65b6d5e420cf59ce1dcb6adb
|
|
BLAKE2b-256 checksum How to use checksums |
e5060b443700936d93236ad571e6afea21a8de81ed73fbfd26e74ee1a62fd9ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/26.0 requests/2.24.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.48.2 importlib-metadata/4.11.3 keyring/21.3.0 rfc3986/1.4.0 colorama/0.4.3 CPython/3.8.5
|
Security for flask-apispec 0.11.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.