Many tutorials say to create an application with Flask(__name__) and from there we can initialize the database with SQLAlchemy(app).
However, I don't like this intrusive model, as the application itself doesn't need to know anything about the database.
I think a better approach would be creating a module called database and initialize the database there.
Models should import db from database and the app should just import the models.
However, with this approach the database module must retrieve the global app by somehow, something which I disagree.
Is there a property way to initialize the database without importing the main app?
This is the neatest way i have found to set up the flask app:
In app.py:
Then in extensions.py i'd have something like this:
That way i have things seperate. There might be a way to loop the init_app in the extensions but then it would be less clean in my opinion.
To use I then import the db from the extensions file.