Bringing FastAPI Developer experience to Flask.
Project description
Flask-Jeroboam
Flask-Jeroboam is a Flask extension modeled 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 latters
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 APIBlueprint will behave exactly like their Flask counterparts unless you activate their extra behaviors.
from flask-jeroboam import Jeroboam
app = Jeroboam()
@app.get("ping")
def ping():
return "pong"
This toy example would work exactly like a regular Flask app. You would start your server just like with Flask. flask run
would do perfectly fine here.
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 afterall.
Searching for wines
Let's consider an endpoint meant to provide search capability onto a wine repository. It takes three arguments from the query string, feed them into a CRUD function get_wines
that return a list of wines as well as the total count of wines matching the query.
Additionnaly, this particular endpoint only need 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 with Flask-Jeroboam:
from flask_jeroboam import Jeroboam, Parser, Serializer
app = Jeroboam(__name__)
class PaginatedSearch(Parser):
page: int = Field(default=1)
per_page: int = Field(default=10)
search: Optional[str]
class WineOut(Serializer):
cuvee: str
appellation: str
class WineListOut(Serializer):
wines: WineOut
count: int
total_count: int
@app.get("/wines", response_model=WineListOut)
def read_wine_list(wine_search: PaginatedSearch):
wines, total_count = get_wines(wine_search)
return {"wines": wines, "count": len(wines), "total_count": total_count}
if __name__ == "__main__":
app.run()
Once you've started your server, then hitting the endpoint with curl "localhost:5000/wines?page=1&per_page=2&search=Champagne"
would return something like:
{
"wines": [
{
"appellation": "Champagne",
"cuvee": "Brut - Blanc de Blancs"
},
{
"appellation": "Champagne",
"cuvee": "Grande Cuvée - 170ème Edition"
}
],
"count": 2,
"total_count": 3
}
See the documentation on more advanced usage: https://flask-jeroboam.readthedocs.io/
Motivation
FastAPI has been rapidly gaining ground in Python Web Development since its inception in late 2018 (1). It is indeed an amazing framework with killer documentation. Besides best-in-class performance, it brings a very compelling API for request parsing and response serialization that speed up API development and provide an incredibly smooth Developer Experience.
Although trying to reproduce FastAPI Starlette-based performance in another framework like Flask would be rather hard and non-sensical, its API for defining endpoints is fair game. There are some excellent Flask extensions dealing with request parsing, response serialization, and auto-documentation, but nothing exactly like FastAPI. That is what I started exploring with Flask-Jeroboam.
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 attractive but also have excellent reasons to stick to Flask.
About the name of the project
A Jeroboam is a large bottle, or flask, containing either 3 or 6 liters of wines depending on the region of production. They are mainly used for fine wines destined to aging because they provide better condition for doing so. Their ratio between the volume of wine it contains and the surface of exchange between the wine and the air is more favorable and slows donw the oxidation reaction. Theses 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.
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 along with a detailed description.
Credits
The main inspiration for this project comes from @tiangolo's FastAPI. 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.0.3a0.tar.gz
.
File metadata
- Download URL: flask_jeroboam-0.0.3a0.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54acb76266b62e761d94fd5c9e313c240b76a381e410643bfd9010f8c7443ac8 |
|
MD5 | fe1a367a28cd7fb171d5e31f1f6a7f41 |
|
BLAKE2b-256 | 03ca6c88207389b003a84d575e8f2c8e88d71c4c8a62274b8ea260546812bc7e |
File details
Details for the file flask_jeroboam-0.0.3a0-py3-none-any.whl
.
File metadata
- Download URL: flask_jeroboam-0.0.3a0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53136c56032a216f7ba66f7e2d740b1c057c2390fef7e128a04630c6f34a8052 |
|
MD5 | f252854d9855b772b3c8ff3bd51e357e |
|
BLAKE2b-256 | 86dcbb0165e59351f0f5939abd0835c78f505555a52ff19de4bced090efb2367 |