Skip to main content

Build and document REST APIs with Flask and apispec

Project description

===========
flask-apispec
===========

.. image:: https://img.shields.io/pypi/v/flask-apispec.svg
:target: http://badge.fury.io/py/flask-apispec
:alt: Latest version

.. image:: https://img.shields.io/travis/jmcarp/flask-apispec/master.svg
:target: https://travis-ci.org/jmcarp/flask-apispec
:alt: Travis-CI

.. image:: https://img.shields.io/codecov/c/github/jmcarp/flask-apispec/master.svg
:target: https://codecov.io/github/jmcarp/flask-apispec
:alt: Code coverage

**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
-------

.. code-block::

pip install flask-apispec

Quickstart
----------

.. code-block:: python

from flask import Flask
from flask_apispec import use_kwargs, marshal_with

from marshmallow import fields, Schema

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:

.. code-block:: python

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/`.

.. code-block:: python

from apispec import APISpec
from flask_apispec.extension import FlaskApiSpec

spec = APISpec(
title='pets',
version='v1',
plugins=['apispec.ext.marshmallow'],
)
docs = FlaskApiSpec(app, spec)

docs.register(get_pets)
docs.register(PetResource)

Notes
-----

**flask-apispec** isn't stable yet, and the interface and internals may change. Bug reports and pull requests are much appreciated.

**flask-apispec** is strongly inspired by Flask-RESTful_ and Flask-RESTplus_, but attempts to provide similar functionality with greater flexibility and less code.

.. _webargs: https://webargs.readthedocs.org/
.. _marshmallow: https://marshmallow.readthedocs.org/
.. _apispec: https://apispec.readthedocs.org/
.. _Flask-RESTful: https://flask-restful.readthedocs.org/
.. _Flask-RESTplus: https://flask-restplus.readthedocs.org/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-apispec-0.2.0.tar.gz (1.0 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

flask_apispec-0.2.0-py3.4.egg (23.0 kB view details)

Uploaded Egg

flask_apispec-0.2.0-py2.py3-none-any.whl (13.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file flask-apispec-0.2.0.tar.gz.

File metadata

  • Download URL: flask-apispec-0.2.0.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for flask-apispec-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0d3554ea372674b96240e67cbebc7d8d519fedbb9cb47c1aa21ffc3ada00c439
MD5 8501889ab8560cc98bd936b4670bc874
BLAKE2b-256 0d0f1449a9adb39a2e08fbb445680d9a8334d9ad448eb80f7552b583563473b1

See more details on using hashes here.

File details

Details for the file flask_apispec-0.2.0-py3.4.egg.

File metadata

File hashes

Hashes for flask_apispec-0.2.0-py3.4.egg
Algorithm Hash digest
SHA256 631649e5ca36f9f5e5bd38570238d699b48cdd661f68b3da6ba62ff40004efb7
MD5 8f35a62632404bc1e91e768a836a342e
BLAKE2b-256 fe914edea8130f3aba55d5c007374f8e387aee4d8ecf64c048702b2da336c5ca

See more details on using hashes here.

File details

Details for the file flask_apispec-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flask_apispec-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 66ca6b37d1a619463285df3125a2c21d74bf8c7569407016dfea2391b43e6a7d
MD5 fbd9a42aa1fcaf678f38eeb52e5ec338
BLAKE2b-256 f85743d7fda4de2993b5962a3f0cfdd9b48c184ac92bb387e279269b6bc1d695

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page