Skip to main content

A Flask extension, inspired by FastAPI that uses Pydantic to provide easy-to-configure data validation for request parsing and response serialization.

Project description

jeroboam-logo

Flask-Jeroboam

English | Français

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 Download License Commit

Read the documentation at https://flask-jeroboam.readthedocs.io/ Coverage Tests Ruff


Documentation: https://flask-jeroboam.readthedocs.io/

Documentation (Français): docs/fr/

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.

Key Features

  • Automatic Request Parsing: Parse and validate request data using typed annotations of endpoint arguments
  • Response Serialization: Effortlessly serialize responses with Pydantic models
  • OpenAPI Auto-Documentation: Generate interactive API documentation automatically
  • Type Safety: Leverage Python type hints for robust, self-documenting code
  • Flask Compatible: Drop-in replacement for Flask applications with full backward compatibility

Installation

You can install flask-jeroboam via pip or any other tool wired to PyPI:

$ pip install flask-jeroboam

Quick Start

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/

Why Flask-Jeroboam?

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_jeroboam-0.2.0.tar.gz (333.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_jeroboam-0.2.0-py3-none-any.whl (43.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_jeroboam-0.2.0.tar.gz.

File metadata

  • Download URL: flask_jeroboam-0.2.0.tar.gz
  • Upload date:
  • Size: 333.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flask_jeroboam-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ce908e764588def4bc294ee8593388e3b2ca6856806b0f63c83a819c0150e33b
MD5 6d6fad77e2c8f54b48a52caf3ab04e0d
BLAKE2b-256 81950340515e6e0285d4d19fc31ec81119e7b3991f8d7e384bc723213abee611

See more details on using hashes here.

File details

Details for the file flask_jeroboam-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: flask_jeroboam-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 43.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flask_jeroboam-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 37339f9f7747642580bd3fbae26e97ea0367e09d7e685d3b5eec7c517fc17031
MD5 adc51ab11e7d23cb7d13214581603d94
BLAKE2b-256 3b6f4a8ee68fc4eb0c55ba035a15082ecd3f5b71374745a9c607c2508a74d16e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page