A collection of useful Flask utilities I use every day in my Flask projects.
pip install flask-utilsfrom flask import Flask
from flask_utils import register_error_handlers
from flask_utils import BadRequestError
app = Flask(__name__)
register_error_handlers(app)
@app.route('/')
def index():
raise BadRequestErrorfrom typing import List, Optional
from flask import Flask
from flask_utils import validate_params
app = Flask(__name__)
@app.post('/create-user')
@validate_params({"first_name": str, "last_name": str, "age": Optional[int], "hobbies": List[str]})
def create_user():
# ...
# This will enforce the following rules:
# - first_name and last_name must be strings and are required
# - age is optional and must be an integer
# - hobbies is a list of strings
# This is just an example, you can use any type of validation you want
return "User created"You can find the full documentation at Read the Docs
Install the requirements
pip install -r requirements-dev.txtMake sure tox is at the latest version
pip install --upgrade toxRun the tests
toxOR
Run the tests multi-threaded
tox -p