creating a database outside the application context

flask, python, sqlalchemy, sqlite

Solution

Flask-SQLAlchemy only needs an app context to operate. You can create an app context manually.

app = create_app(env)
ctx = app.app_context()
ctx.push()

# your code here

ctx.pop()

This is from the docs here and here.

Problem

I have an app factory like so ``` db = SQLAlchemy() def create_app(environment): app = Flask(__name__) app.config.from_object(config[environment]) db.init_app(app) # ... etc return app ``` then, I have a script which fetches CSVs outside of the context of the application. This script is a cron which is run every x hours I want to update the sqlite database somehow that the application is using. Is this possible?

Original source