Skip to main content

Flask Toolkits

Downloads

Installation

pip install flask-toolkits

Description

Flask toolkits implements and provides several features from FastAPI like:

  • Automatic API documentation (define the function and we'll generate the swagger/openapi spec for you)
  • Passing parameters through view/router function which is unable in Flask before
  • Easy Middleware setup
  • Parameters and schema validation using Pydantic
  • Response classes that could return any type of data without worried to get error
  • much more..

Changelogs

  • v0.0
    • First Upload
  • v0.1
    • Integration with flask-http-middleware
    • pydantic support for JSON arguments and validation
    • Multiple response type generator
    • Added JSONResponse class to replace jsonify roles in send dictionary data with encoding improvements.
  • v0.2
    • Supported enumeration API documentation
    • Added support for type hint from typing's generic (ex: Optional, Union, List)
    • Fixed input parameter validations
  • v0.3
    • Support File and Form input parameters validation and automatic swagger.
    • Added constraint feature for parameters (ex: limit, max/min length, greater/less than, equals than etc)
  • v0.4
    • Support Authorization header in openapi spec.
    • Added Authorization processing function for security and can be used as login or auth.
  • v0.5
    • Support add_url_rule and route for endpoint definition
    • Support auto swagger for multiple methods in a single endpoints
  • v0.6
    • Support alias on endpoint parameters (path, query, header, etc) to enable non-pythonic terms of parameter names
  • v0.7
    • support response structure generator function to helps creating the response schema and examples

Key Tools inside this toolkit

  • Automatic API documentation (swagger/openapi)
  • Request-Response direct HTTP middleware (flask-http-middleware)
  • Automatic parameters validation (pydantic)
  • Response generator (JSON, Plain Text, HTML)

Automatic Parameters Validation

The original Blueprints class from flask can't insert most of arguments inside endpoint. Here our APIRouter allows you to have arguments inside your endpoint

from typing import Optional
from flask_toolkits import APIRouter, Body, Header, Query
from flask_toolkits.responses import JSONResponse


router = APIRouter("email", import_name=__name__, static_folder="/routers/email", url_prefix="/email")


@router.post("/read", tags=["Email Router"])
def get_email(
    id: int,
    name: Optional[str],
):
    return JSONResponse({"id": id, "name": name})

Automatic API Documentation

Here our APIRouter allows you to auto-documenting your endpoint through AutoSwagger. Define the new router using APIRouter class, lets put it in another pyfile

email_view.py

from typing import Optional
from flask_toolkits import APIRouter, Body, Header, Query
from flask_toolkits.responses import JSONResponse


router = APIRouter("email", import_name=__name__, static_folder="/routers/email", url_prefix="/email")


@router.post("/read", tags=["Email Router"])
def get_email(
    id: int = Body(),
    name: Optional[str] = Body(None),
    token: int = Header(),
    race: Optional[str] = Query(None)
):
    return JSONResponse({"id":id, "name": name})

main.py

from flask import Flask
from flask_toolkits import AutoSwagger

from email_view import router as email_router


app = Flask(__name__)

auto_swagger = AutoSwagger()

app.register_blueprint(email_router)
app.register_blueprint(auto_swagger)


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

then you can go to http://localhost:5000/docs and you will found you router is already documented

alt text


Supported Field Parameters

flask-toolkits provide multiple field parameters such as Header, Query, Body, Path, File, Form


Easy Security Scheme Setup and Documentation

flask-toolkits helps you to define your security scheme for authorization easier than before. In advance this also give you automated documentation.

Basic Usage

lets assume you have your own bearer security schema. You just have to create a new instance of HTTPBearerSecurity() to enable automatic documentation on it.

from flask import request
from flask_toolkits import APIRouter
from flask_toolkits.security import HTTPBearerSecurity

router = APIRouter("api", __name__)

@router.get("/home", security=HTTPBearerSecurity())
def home(message: str):
    if my_security_scheme(request):
        return JSONResponse({"message": message})
    return JSONResponse({"message": "invalid authorization"})

this is how it looks like alt text

on you clicked it alt text

Define your own security scheme

If you want to define your own security scheme you can follow below guidance

from flask import request
from flask_toolkits import APIRouter
from flask_toolkits.security import HTTPBearerSecurity

class JWTBearer(HTTPBearerSecurity):
    def __init__(self):
        super().__init__()

    def __call__(self, req):
        data = self.get_authorization_data(req)
        if data != "abcdefghij":
            raise Exception("This is not good")
        return req

router = APIRouter("api", __name__)

@router.get("/home", security=JWTBearer())
def home(message: str):
    if my_security_scheme(request):
        return JSONResponse({"message": message})
    return JSONResponse({"message": "invalid authorization"})

Overriding __call__ method inside the subclass would define your security schema for the routers that are using your security scheme


Define to all endpoints in a router

Just pass it to APIRouter and all its endpoint will use that security scheme!

router_with_bearer = APIRouter("api", __name__, security=JWTBearer())

but don't worries! You can also override it by just defining in the router decorator!

@router_with_bearer.get("/home", security=AnotherBearerSecurity())
def home():
    return {"message": "hello"}

Parameter Alias

In case you have non-pythonic terms with unicode character (-, +, _, =) for your paramter names, you can apply the alias into the parameters easily

@app.get("/test-alias")
def test_alias(
    apikey: str = Header(alias="x-api-key")
):
    return JSONResponse({"apikey": apikey})

here you will also have your swagger is defined with that alias alt text


Response Structure

Creating the response example and schema easily by just defining the class and pass it to create_response_example or accessing as_response() from BaseSchema objects

from flask_toolkits.responses import response_json_example


class PersonResponse(BaseSchema):
    name: str
    age: int

class FailedResponse(BaseSchema):
    message: str
    error_code: int

@router.route(
    '/hello_world/<first>/<int:number>', tags=["My Hello"],
    responses={
        200: response_json_example(PersonResponse(name="Alex", age=20)),
        400: FailedResponse(message="Data not found", error_code=101).as_response()
    },
)
def hello_world(
    name: str = Query(),
    age: int = Query()
):
    resp = {
        "name": name,
        "age": age
    }

    return JSONResponse(resp)

alt text alt text


Multiple HTTP Methods in a single endpoint

add_url_rule and route method for Flask's App or Blueprints object are now supported. This also allows you to have multiple HTTP methods in a single endpoint function

@app.route("/test-multiple-method", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
def go_multi_method(
    name: str = Body()
):
    return JSONResponse({"result": name})

Here you will get null if you hit it using GET but you'll get the value on you hit with other methods that support Body. You won't loose your validation since it only applied for methods that support that kind of params.


Request-Response direct HTTP middleware

import time
from flask import Flask
from flask_toolkits.middleware import MiddlewareManager, BaseHTTPMiddleware

app = Flask(__name__)

class MetricsMiddleware(BaseHTTPMiddleware):
    def __init__(self):
        super().__init__()

    def dispatch(self, request, call_next):
        t0 = time.time()
        response = call_next(request)
        response_time = time.time()-t0
        response.headers.add("response_time", response_time)
        return response

app.wsgi_app = MiddlewareManager(app)
app.wsgi_app.add_middleware(MetricsMiddleware)

@app.get("/health")
def health():
    return {"message":"I'm healthy"}

Metadata

Release files for flask-toolkits 0.7.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-toolkits 0.7.10
File Size Uploaded
flask-toolkits-0.7.10.tar.gz 1.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-toolkits 0.7.10
File Interpreter ABI Platform
flask_toolkits-0.7.10-py3-none-any.whl Python 3 none any Details

Total release size: 2.0 MB

Release files / flask-toolkits-0.7.10.tar.gz

Download URL flask-toolkits-0.7.10.tar.gz
Size 1.0 MB
Tags Source
SHA-256 checksum
How to use checksums
82519db56181ab450f23f1650f4a04e17b268419fce1caa8448c199848dd8fdc
BLAKE2b-256 checksum
How to use checksums
330d665a8e57d4f6d94e14095a852c5e3e3b8d49178d06f3da8121d084389615
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / flask_toolkits-0.7.10-py3-none-any.whl

Download URL flask_toolkits-0.7.10-py3-none-any.whl
Size 1.0 MB
Tags Python 3
SHA-256 checksum
How to use checksums
95eec867fc900b27a86d84d616f2cf5eda9b622dce7095d334a6626ce4a87e76
BLAKE2b-256 checksum
How to use checksums
3d39f885d5c9365d8786d61d4ecb3cf26accdbc6be128ec47baa68f3451511af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.7.10 This release

2 release files

0.7.9

2 release files

0.7.8

2 release files

0.7.7

2 release files

0.7.6

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.12

2 release files

0.6.11

2 release files

0.6.10

2 release files

0.6.9

2 release files

0.6.8

2 release files

0.6.7

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.1

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page