I'm trying to create a simple, but not trivial application with Flask and I keep running into a situation which is not explained well in the docs.
I'd like my app to be split between different modules, but it looks like it's hard to access the main app
object (or db
from flask-sqlalchemy
) from any other place than the main module. It looks like you have to really write your own way of treating it as a global that everything has access to, which requires some questionable code and a bit of time.
Both app
and db
are created at the level where the main application is instantiated and every example assumes they're available at the top level. But if I split different url handlers into different modules, I lose the ability to use @app.route
decorator. I can work around that with app.add_url_rule('/...', some.module...)
, but then again, app is not passed to that function.
Same thing happens with the db
- I don't know if I should stick it into g
, or make it accessible via app
somehow. Doing that doesn't help me with moving the models into a separate module though - how am I supposed to access db
when importing them? All examples again assume it's just available as a local variable.
Am I missing something obvious, or am I trying to use flask for the wrong task here?
Flask is a lightweight Python web framework that provides useful tools and features for creating web applications in the Python Language. It gives developers flexibility and is an accessible framework for new developers because you can build a web application quickly using only a single Python file.
A Flask application is an instance of the Flask class. Everything about the application, such as configuration and URLs, will be registered with this class.
Files named __init__.py are used to mark directories on disk as Python package directories. So it really depends on your structure of projects. Ideally if you have a project with multiple folders, you might have to make them as python package directories.
The answer to your question is blueprints. In fact, if you look at documentation, it is mentioned that blueprints are there to enable division of application in modules.
Sample code:
from flask import Blueprint mod = Blueprint(blueprint_name, __name__, template_folder="folder_name" static_folder="folder_name") @mod.route('/mymodule') def view(): # your code
In app.py
from yourmodule import mod app.register_blueprint(mod)
This is the right way to divide app into modules. Check out blueprint docs for more details.
Moreover if you require to access app in any other module, you should use current_app
proxy of the app.
from flask import current_app
Check out this document which guides how to divide flask app into modules for large projects by the creator of flask.
Have you seen the new blueprints
feature (I haven't used it yet, but sounds like this may be helpful in your case).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With