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.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

flaskerk-0.6.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flaskerk-0.6.1.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.1.tar.gz
Algorithm Hash digest
SHA256 d5d61f3ecdbd9861e1ccbecfe34497531b7c3fb4c5d91ecdc0099130981ae720
MD5 293072173e45587277716ffa088d6e55
BLAKE2b-256 b89104d65a758c72ce046d579d87fa5ea6230edb01e66dc7ee2ee189eb8e7a10

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flaskerk-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 11.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a01ba24dc356988d8fa0741d1127e0b4c5e6985f1c4bf6e6842450348fed045d
MD5 509900e0d0327844e87e20e37a392b52
BLAKE2b-256 ba0b5b08085bfa467ab2bfd37f15378fb9c0445801ba8dc0b6a0e695f45103fb

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