A Flask extension, inspired by FastAPI that uses Pydantic to provide easy-to-configure data validation for request parsing and response serialization.
Project description
Flask-Jeroboam
Flask-Jeroboam is a Flask extension modelled after FastAPI. It uses Pydantic to provide easy-to-configure data validation in request parsing and response serialization.
Documentation: https://flask-jeroboam.readthedocs.io/
Source Code: https://github.com/jcbianic/flask-jeroboam
Flask-Jeroboam is a thin layer on top of Flask to make request parsing, response serialization and auto-documentation as smooth and easy as in FastAPI.
Its main features are:
- Request parsing based on typed annotations of endpoint arguments
- Response serialization facilitation
- (Planned) OpenAPI auto-Documentation based on the first two
How to install
You can install flask-jeroboam via pip or any other tool wired to PyPI:
$ pip install flask-jeroboam
How to use
A toy example
Flask-Jeroboam subclasses both Flask and Blueprint classes. This means that the Jeroboam and Blueprint will behave exactly like their Flask counterparts unless you activate their specific behaviours.
from flask_jeroboam import Jeroboam
app = Jeroboam()
@app.get("/ping")
def ping():
return "pong"
if __name__ == "__main__":
app.run()
This toy example would work exactly like a regular Flask app. If you run this file, then hitting the endpoint with curl localhost:5000/ping
would return the text response pong
.
Let's try a more significant and relevant example and build a simplified endpoint to retrieve a list of wines. We are wine-themed, after all.
Searching for wines
Let's consider an endpoint that provides search capability onto a wine repository. It parses and validates three arguments from the query string and feeds them into a CRUD function get_wines
that return a list of wines as dictionnaries.
Additionally, this endpoint only needs to return the name of the cuvee and the appellation, and discard any other informations. Let's take a look at what it might look like:
from flask_jeroboam import Jeroboam, InboundModel, OutboundModel
from pydantic.fields import Field
from typing import List, Optional
from docs_src.readme.crud import get_wines
app = Jeroboam(__name__)
class GenericPagination(InboundModel):
page: int = Field(1, ge=1)
per_page: int = Field(10, ge=1, le=100)
@property
def offset(self) -> int:
return (self.page - 1) * self.per_page
class WineOut(OutboundModel):
cuvee: str
appellation: str
@app.get("/wines", response_model=List[WineOut])
def read_wine_list(pagination: GenericPagination, search: Optional[str]):
wines = get_wines(pagination, search)
return wines
if __name__ == "__main__":
app.run()
Once you've started your server, then hitting the endpoint with curl "localhost:5000/wines?page=1&perPage=2&search=Champagne"
would return:
[
{
"cuvee": "Brut - Blanc de Blancs",
"appellation": "Champagne"
},
{
"cuvee": "Grande Cuvée - 170ème Edition",
"appellation": "Champagne"
}
]
All examples in the documentation can be found in docs_src/X
folder and should run as is. Their corresponding tests can be found in tests/test_docs/X
.
See the documentation on more advanced usage: https://flask-jeroboam.readthedocs.io/
Motivation
I just wanted to use FastAPI's way of defining view arguments and response models without leaving Flask.
A word on performance
One thing Flask-Jeroboam won't give you is performance improvement. Underneath Flask, werkzeug still handles the heavy lifting of a wsgi, so transitioning to Flask-Jeroboam won't speed up your app. Please remember that FastAPI's performance comes from Starlette, not FastAPI itself.
Intended audience
The intended audience of Flask-Jeroboam is Flask developers who find FastAPI very convincing but also have excellent reasons to stick to Flask.
About the name of the project
A Jeroboam is a large bottle, or flask, containing 5 litres of wine[^1], instead of 0,75. Winemakers use this format for fine wines destined for ageing because it provides better ageing conditions. First, the ratio between the volume of wine it contains and the surface of exchange between the wine and the air is more favourable and slows down the oxidation reaction. These larger containers also take longer to cool down or warm up, leading to less thermal violence to the wine during conservation.
In other words, they are more durable flasks for fine wines. The intention is to hold this promise for APIs.
The wine-themed name is a tribute to the Bordeaux-based wine tech startup where the development of this package started.
[^1]: Outside of the Bordeaux region Jeroboam bottle contain 3 litres, like in Burgundy or Champagne.
License
Distributed under the terms of the MIT license, Flask-Jeroboam is free and open-source software.
Issues
If you encounter any problems, please file an issue following available templates. Templates are available for feature requests, bug reports, documentation updates and implementation betterments.
Credits
The main inspiration for this project comes from @tiangolo's FastAPI. Starting from v0.1.0 it also includes forked code from FastAPI. Appropriate credits are added to the module or functions docstrings.
Flask and pydantic are the two direct dependencies and do most of the work.
I used @cjolowicz's Hypermodern Python Cookiecutter template to generate this project.
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 flask_jeroboam-0.1.0b3.tar.gz
.
File metadata
- Download URL: flask_jeroboam-0.1.0b3.tar.gz
- Upload date:
- Size: 35.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4b78802960acda313e33f450e85ba5f1f17b5d387fb05d042b8e311dd3ac643 |
|
MD5 | dfa7bf4d664c04a3dd21800165a48f41 |
|
BLAKE2b-256 | ed36c41d3d6e98f2cc876f9f4f756062d3b6cf88a596a7443f71d72b72ea474d |
File details
Details for the file flask_jeroboam-0.1.0b3-py3-none-any.whl
.
File metadata
- Download URL: flask_jeroboam-0.1.0b3-py3-none-any.whl
- Upload date:
- Size: 43.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e90660ac5d4b1a738308bc7afa7fd9ea95234e85549a582779f8fadedfc580ab |
|
MD5 | c4e5a1620e838501100d952cfe99a395 |
|
BLAKE2b-256 | a27251e879b3790267ab53aecf8d032271265ccb029e42c696068c0d5e80cec2 |