lustdante
lustdante

Reputation: 493

Importing Flask app when using app factory and flask script

This is Flask app context

app = Flask(__name__)

with app.app_context():
    # insert code here

Most of the use cases of app context involves having 'app' initialized on the same script or importing app from the base.

My application is structured as the following:

# application/__init__.py
def create_app(config):
    app = Flask(__name__)
    return app

# manage.py
from application import create_app
from flask_script import Manager

manager = Manager(create_app)
manager.add_command("debug", Server(host='0.0.0.0', port=7777))

This might be really trivial issue, but how I should call 'with app.app_context()' if my application is structured like this?

Upvotes: 2

Views: 1640

Answers (1)

Rachel Sanders
Rachel Sanders

Reputation: 5874

Flask-Script calls everything inside the test context, so you can use current_app and other idioms:

The Manager runs the command inside a Flask test context. This means that you can access request-local proxies where appropriate, such as current_app, which may be used by extensions.

http://flask-script.readthedocs.org/en/latest/#accessing-local-proxies

So you don't need to use with app.app_context() with Manager scripts. If you're trying to do something else, then you'd have to create the app first:

from application import create_app

app = create_app()

with app.app_context():
    # stuff here

Upvotes: 2

Related Questions