Overview
Flask 2.0, released on May 12, 2021, is a major release that introduces async view support and nested blueprints.
Main Features
Async views
Flask 2.0 natively supports async view functions. Handlers can use await for non-blocking I/O operations without leaving the Flask ecosystem.
python
from flask import Flask
import httpx
app = Flask(__name__)
@app.get('/weather/<city>')
async def weather(city):
async with httpx.AsyncClient() as client:
resp = await client.get(
f'https://api.weather.example/{city}'
)
return resp.json()
@app.get('/health')
async def health():
return {'status': 'ok'}
Nested blueprints
Blueprints can now be nested, allowing you to structure an application into hierarchical modules with composed URL prefixes.
python
from flask import Flask, Blueprint
# Parent blueprint
api = Blueprint('api', __name__, url_prefix='/api')
# Child blueprint
users = Blueprint('users', __name__, url_prefix='/users')
@users.get('/')
def listing():
return {'users': []}
# Nesting
api.register_blueprint(users)
app = Flask(__name__)
app.register_blueprint(api)
# Final route: /api/users/
