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?