Skip to content

Flask-Jeroboam is a Flask extension modelled after FastAPI that uses Pydantic to provide easy-to-configure data validation for request parsing and response serialization.

License

jcbianic/flask-jeroboam

Repository files navigation

jeroboam-logo

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.

PyPI Python Version License Commit

Read the documentation at https://flask-jeroboam.readthedocs.io/ Maintainability Test Coverage Tests Black


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 wine1, 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.

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.

Footnotes

  1. Outside of the Bordeaux region Jeroboam bottle contain 3 litres, like in Burgundy or Champagne.

About

Flask-Jeroboam is a Flask extension modelled after FastAPI that uses Pydantic to provide easy-to-configure data validation for request parsing and response serialization.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Languages