I want to create a Flask extension that depends on another Flask extension. For the argument, let's say that this is Flask-Foo, and that it needs Flask-Redis to store certain data in the Redis database.
I know that I can add an installation dependency to Flask-Redis. However, I do not understand how to instantiate and initialize Flask-Redis.
The setting for Flask-Foo sets up the Flask-Redis object. The disadvantage of this is that it assumes that the application also does not use Flask-Redis for any other reason that is explicitly configured outside of Flask-Foo. If so, we get two objects that exist side by side, which seems wrong.
The user must create an instance and configure Flask-Redis. Flask-Foo verifies that it has been initialized for this application, and complains about something else. The problem is that it seems to overlay the template on the user — why would they need to install Flask-Redis to use Flask-Foo if they do not have other knowledge or interests in the Flask-Redis configuration? In addition, we do not ask for a problem, if this means that you Flask-Foo.init_app()always need to call after Flask-Redis.init_app()?
Do not use Flask-Redis. Use the Redis package directly and control the connection in Flask-Foo code. This is likely to avoid the above problems. But this seems fuzzy - we basically have to solve the problems that Flask-Redis solves. If Flask-Foo continues to support an alternative database, this will be more difficult, because we must maintain code to manage different types of connections.
To be clear, this is not a question specifically about Flask-Redis or how it works! I just want to understand what is usually the right way to create an extension on top of the extension.
source
share