Application Context (Flask)
The application context in Flask makes the current app and the current_app and g proxies available during a request or task. It is pushed automatically per request, but you must push it manually in scripts and background jobs.
Flask
Flask uses context objects so that proxies like current_app, g, request, and session can be plain importable globals that still refer to the right values. The application context tracks 'which app is active' and powers current_app and g.
During a normal request, Flask pushes both an application context and a request context for you. But outside a request — in a CLI command, a test, or a background thread — those proxies have nothing to point at, so accessing current_app raises 'Working outside of application context'.
The fix is to wrap such code in with app.app_context():, which pushes the context manually. This is exactly why db.create_all() in scripts must run inside with app.app_context().
Syntax
with app.app_context():
... # current_app and g are availableExamples
Python
from flask import current_app
# in a standalone script:
with app.app_context():
db.create_all()
print(current_app.name)