current_app and the g Object
Access the active app and per-request storage.
current_app and the g Object is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet current_app
current_app is a proxy that always points to the app handling the work right now. Import it whenever you need the active application.
from flask import current_appWhy Not Import app?
Importing your app object directly causes circular imports as projects grow. current_app sidesteps that by resolving the app at runtime.
Reading Config
A common use is reading settings: current_app.config gives you the active app's configuration without importing the app module.
key = current_app.config["SECRET_KEY"]It Needs a Context
current_app only works while an app context is active. Inside a view that is automatic, since the request pushed one for you.
Meet the g Object
g is a simple scratchpad for one request. Attach anything to it and read it back later in the same request.
from flask import g
g.user = "alice"g Is Per Request
The g object is reset for every request. Data you stash on it never leaks into another visitor's request.
A Typical g Use
Use g to share work across functions in one request, like caching the current user so you fetch it from the database only once.
if "db" not in g:
g.db = connect()g Is Not Storage
Do not treat g as a database. It forgets everything the moment the request ends, so use it only for that request's lifetime.
g Lives in the App Context
Surprisingly, g belongs to the app context, not the request context. In practice both exist during a request, so it just works.
Cleaning Up With g
Pair g with teardown handlers to close what you opened, like closing a database connection after the response is sent.
@app.teardown_appcontext
def close(exc):
g.pop("db", None)current_app and g Together
They pair nicely: read settings from current_app, then stash request-specific results on g so the rest of the request can reuse them.
Quick Check
Let us pin down what g is for.
Recap
Two handy proxies: current_app finds the active app and its config, while g is a per-request scratchpad you wipe clean each time. 🎉
Frequently asked questions
Is the “current_app and the g Object” lesson free?
Yes — the full text of “current_app and the g Object” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “current_app and the g Object”?
Access the active app and per-request storage. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “current_app and the g Object” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- App Context vs Request Context
- current_app and the g Object
- Context Locals and Thread Safety
- Push a Context in Scripts and Shells