Using Flask Extensions¶
Flask extensions can be used with Quart, with some caveats. To do so
the very first import in your code must be import quart.flask_patch
as this will add modules purporting to be Flask modules for later use
by the extension. For example,
import quart.flask_patch
from quart import Quart
import flask_login
app = Quart(__name__)
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
...
Caveats¶
Flask extensions must use the global request proxy variable to access
the request, any other access e.g. via
_get_current_object()
will require
asynchronous access. To enable this the request body must be fully
received before any part of the request is handled, which is a
limitation not present in vanilla flask.
Trying to use Flask alongside Quart in the same runtime will likely not work, and lead to surprising errors.
The flask extension must be limited to creating routes, using the request and rendering templates. Any other more advanced functionality may not work.
Finally the flask_patching system also relies on patching asyncio, and hence other implementations or event loop policies are unlikely to work.
Supported extensions¶
A list of officially supported flask extensions exist here of those a few have been tested against Quart (the extensions tested are still supported and don’t require external services). The following flask extensions are tested and known to work with quart,
Broken extensions¶
The following flask extensions have been tested are known not to work with quart,
Flask-CORS, as it uses
app.make_response
which must be awaited. Try Quart-CORS instead.Flask-Restful as it subclasses the Quart (app) class with synchronous methods overriding asynchronous methods. Try Quart-OpenApi instead.
Flask-SQLAlchemy as it relies on thread isolation, which Quart does not support. Try Databases instead.