Skip to main content

No project description provided

Project description

Flaskerk

Build Status GitHub PyPI - Python Version

Provide OpenAPI document and validation for flask service.

Mainly built for Machine Learning Model services.

If you're using Falcon, check my another library Falibrary.

Features

  • Generate API document with Redoc UI or Swagger UI :yum:
  • Less boilerplate code, annotations are really easy-to-use :sparkles:
  • Validate query, JSON data, response data with pydantic :wink:
  • Better HTTP exceptions for API services (default & customized) (JSON instead of HTML) :grimacing:

Quick Start

install with pip install flaskerk (Python 3.6+)

Simple demo

from flask import Flask, request, jsonify
from flaskerk import Flaskerk
from pydantic import BaseModel

class Query(BaseModel):
    text: str

app = Flask(__name__)
api = Flaskerk()

@app.route('/api/classify')
@api.validate(query=Query)
def classify():
    print(request.query)
    return jsonify(label=0)

if __name__ == "__main__":
    api.register(app)
    app.run()

Changes you need to make:

  • create model with pydantic
  • decorate the route function with Flaskerk.validate()
  • specify which part you need in validate
    • query (args in url)
    • data (JSON data from request)
    • resp (response) this will be transformed to JSON data after validation
    • x (HTTP Exceptions list)
  • register to Flask application

After that, this library will help you validate the incoming request and provide API document in /docs.

Parameters in Flaskerk.validate Corresponding parameters in Flask
query request.query
data request.json_data
resp \
x \

For more details, check the document.

More feature

from flask import Flask, request
from pydantic import BaseModel, Schema
from random import random
from flaskerk import Flaskerk, HTTPException

app = Flask(__name__)
api = Flaskerk(
    title='Demo Service',
    version='1.0',
    ui='swagger',
)

class Query(BaseModel):
    text: str

class Response(BaseModel):
    label: int
    score: float = Schema(
        ...,
        gt=0,
        lt=1,
    )

class Data(BaseModel):
    uid: str
    limit: int
    vip: bool

e233 = HTTPException(code=233, msg='lucky for you')

@app.route('/api/predict/<string(length=2):source>/<string(length=2):target>', methods=['POST'])
@api.validate(query=Query, data=Data, resp=Response, x=[e403])
def predict(source, target):
    """
    predict demo

    demo for `query`, `data`, `resp`, `x`
    """
    print(f'=> from {source} to {target}')  # path
    print(f'Data: {request.json_data}')  # Data
    print(f'Query: {request.query}')  # Query
    if random() < 0.5:
        e403.abort('bad luck')
    return Response(label=int(10 * random()), score=random())

if __name__ == '__main__':
    api.register(app)
    app.run()

try it with http POST ':5000/api/predict/zh/en?text=hello' uid=0b01001001 limit=5 vip=true

Open the docs in http://127.0.0.1:5000/docs .

For more examples, check examples.

FAQ

Can I just do the validation without generating API document?

Sure. If you don't register it to Flask application, there won't be document routes.

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

flaskerk-0.6.2.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

flaskerk-0.6.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file flaskerk-0.6.2.tar.gz.

File metadata

  • Download URL: flaskerk-0.6.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for flaskerk-0.6.2.tar.gz
Algorithm Hash digest
SHA256 ed7588e1e9d1f6ad604e9678b7d2e3569b7baff80041820a22ce5c4c4140c2cf
MD5 1989f0a45873e1f6c8b5698423bb1129
BLAKE2b-256 ad26b1ed1320ad84f18e480f94345106bcf9c28cd99a688dafb351f5e8cac8a2

See more details on using hashes here.

File details

Details for the file flaskerk-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: flaskerk-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for flaskerk-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4720762f3feaaf10a45d2a2453585a88492f7ba981477ea7d47a7043530d281e
MD5 294b257ebbfe352286747d44380ff569
BLAKE2b-256 d6a6e454603af7477dc68cbb8ed54d15426d0f39088dbca14529d3fefa27ac0f

See more details on using hashes here.

Supported by

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