No project description provided
Project description
Flaskerk
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)- builtin converters (string, path, any, int, float, uuid)
data
(JSON data from request)resp
(response) this will be transformed to JSON data after validationx
(HTTP Exceptions list)tags
(tags for this API route)
- 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=[e233], tags=['model'])
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:
e233.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flaskerk-0.6.3.tar.gz
.
File metadata
- Download URL: flaskerk-0.6.3.tar.gz
- Upload date:
- Size: 11.0 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b44438e7547e16a61acd17ae59637e4432866d2f90913af79ad4cb2ee95dcdbc |
|
MD5 | f1debfacd2fd6c5367ebd24540d341f8 |
|
BLAKE2b-256 | 1811a8ac878b6a71906771d2910861a22c25eefde1801b9e6f8d40c9bb95d13d |
File details
Details for the file flaskerk-0.6.3-py3-none-any.whl
.
File metadata
- Download URL: flaskerk-0.6.3-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4d85db341f834a52f2105cdc6e99a8e6d1333aedd830406dd5020c36a4017bf |
|
MD5 | 0305a4627c606e54f2e0a18ea6b7dc23 |
|
BLAKE2b-256 | b34e70127e49c648004746f11a605d7a9d601544fafa0c31ebfb4a25725cd758 |