Initialisation using the app factory pattern, requires application context
philliproso opened this issue · 1 comments
philliproso commented
If I want to initialise flask-AppBuilder using an application factory pattern I get the standard out of application context error. I can solve this by initialising within the "with app.app_context():", is this continued expected behaviour?
Initialise extensions possibly in a separate file
db = SQLA()
appbuilder = AppBuilder()
Initialise app
app = Flask(name)
app.config.from_object('config')
Initialise extensions
db.init_app(app)
appbuilder.init_app(app, db.session)
dpgaspar commented
Yes, this is the expected behaviour. When you init_app all security permission are checked and inserted if they don't exist. So you need db access on init_app.
I'm using it like this on init.py::
db = SQLA()
appbuilder = AppBuilder()
from app import views
def create_app():
app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)
with app.app_context():
appbuilder.init_app(app, db.session)
return app
On run.py::
import sys
from app import create_app
config = 'config'
if len(sys.argv) > 1:
config = sys.argv[1]
create_app(config).run(host='0.0.0.0', port=8080, debug=True)
This example accepts the config file as an argument, so you can easilly use diferent configurations for the same app.