Flask remains one of the most flexible WSGI frameworks in Python. However, building enterprise-grade APIs often requires manual integration of OpenAPI spec generators, task offloading mechanisms, and strict parameter validation.
The v0.2.x release of Flask Nova addresses these gaps directly.
Key Features in Flask Nova 0.2.x:
-
Async Task Offloading: Run blocking I/O with
to_thread()and CPU tasks withto_process(). -
Pydantic
FieldSupport: Enforcege,le,min_length, and regex on path/query parameters. -
OpenAPI 3.0 Enhancements: Auto-group Blueprints and add
status_code&externalDocsto route decorators. -
Observability: Native RFC 7807 problem details, ANSI-colored JSON logs, and
trace_idinjection.
Asynchronous Task Offloading in Flask: to_thread and to_process
Running asynchronous code inside Flask requires careful handling of blocking operations. Flask Nova introduces two task offloaders:
from flask_nova import to_thread, to_process
@app.get("/analytics")
async def get_analytics():
# Run blocking database I/O in a worker thread
data = await to_thread(fetch_heavy_db_records)
# Run CPU-bound processing in a separate process pool
processed = await to_process(compute_complex_metrics, data)
return {"status": "success", "data": processed}
Pydantic Field Validation for Path and Query Parameters
In v0.2.x, path and query parameters accept Pydantic Field definitions for granular type constraints:
from pydantic import Field
from flask_nova import FlaskNova
app = FlaskNova()
@app.get("/users/<int:user_id>")
def get_user(
user_id: int = Field(..., ge=1, description="Unique positive user ID"),
page: int = Field(1, ge=1, le=100, description="Page number"),
search: str | None = Field(None, min_length=3, max_length=50)
):
return {"user_id": user_id, "page": page, "search": search}
Automatic OpenAPI 3.0 Docs and Route Metadata
- Blueprint Tagging: Blueprint names automatically append to OpenAPI tags for organized documentation.
-
Route Metadata: Decorators accept
status_code,additionalOperations,externalDocs,servers, anddeprecated. -
Explicit File Requests: Updated File binder with multi-file support and explicit
content_typechecks.
from flask_nova import File
@app.post("/upload",
status_code=201,
externalDocs={"description": "Upload specs", "url": "https://docs.example.com/upload"}
)
def upload_file(
avatar: File = File(name="avatar", content_type="image/png")
):
avatar.save(f"/uploads/{avatar.filename}")
return {"message": "File uploaded successfully"}
Observability & Error Handling
-
RFC 7807 Logging: Set
ANSI_COLOR_JSON_LOG: boolto switch Flask's logger to colorful JSON formatted logs. -
Traceability: All
HTTPExceptioninstances injecttraceparentheaders andtrace_idby default into response contexts. -
Template Serialization: Standard
render_templatecalls support response serialization out of the box.
Installation
pip install --upgrade flask-nova
Check out the full CHANGELOG.md on GitHub!
Top comments (0)