librelist archives

« back to archive

flask extension and init_app

flask extension and init_app

From:
daniel gaspar
Date:
2014-05-23 @ 23:34
Hello,

I'm developing an extension but the init_app (app factory) only works if i
do something like this:

https://gist.github.com/dpgaspar/7677d3d419accbdbaf4d

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

This has 2 reasons:

1 - this extension has some security Models (tables) associated with it
    and on init if they do not exist they are created.
    for this i am using flask-sqlalchemy 'create_db' that only works
inside app_context

2 - It easier for me to create a property like this to retrieve app
    @property
    def get_app(self):
        if self.app:
            return self.app
        else:
            return current_app