0
0
Flaskframework~10 mins

G object for request-scoped data in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - G object for request-scoped data
Start Request
Create g object
Store data in g
Use g data in view
Request ends
g data cleared
Each web request creates a fresh g object to store data accessible only during that request.
Execution Sample
Flask
from flask import Flask, g, request
app = Flask(__name__)

@app.before_request
def before():
    g.user = request.headers.get('User')

@app.route('/')
def index():
    return f"Hello, {g.user}!"
This code saves the user from request headers into g before handling the request, then uses it in the response.
Execution Table
StepActiong.user valueOutput
1Start request, create gunsetNo output yet
2before_request runs, sets g.user = 'Alice''Alice'No output yet
3index() reads g.user'Alice'Returns 'Hello, Alice!'
4Request ends, g clearedunsetNo output
💡 Request ends, g object data is cleared and not shared between requests
Variable Tracker
VariableStartAfter Step 2After Step 3Final
g.userunset'Alice''Alice'unset (cleared after request)
Key Moments - 2 Insights
Why does g.user reset to None after the request ends?
Because g is request-scoped, it only lives during one request. After the request finishes (see step 4 in execution_table), g data is cleared automatically.
Can g.user be shared between different requests?
No, each request gets a new g object. Sharing data between requests requires other methods like sessions or databases.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of g.user at Step 3?
A'Alice'
BNone
C'Bob'
DUndefined
💡 Hint
Check the 'g.user value' column at Step 3 in the execution_table.
At which step does the g object get cleared?
AStep 2
BStep 4
CStep 3
DNever
💡 Hint
Look at the 'Action' and 'g.user value' columns in the execution_table for Step 4.
If we did not set g.user in before_request, what would index() return?
A'Hello, None!'
B'Hello, !'
CError because g.user is missing
D'Hello, g.user!'
💡 Hint
g.user is not set, so accessing it raises AttributeError.
Concept Snapshot
Flask's g object stores data only during one request.
Use g to save info like current user or db connection.
Set g values in before_request or view functions.
Data in g is cleared after request ends.
Do not use g for data sharing between requests.
Full Transcript
In Flask, the g object is a special place to store data that lasts only during one web request. When a request starts, Flask creates a fresh g object. You can put information there, like the current user or database connection, so different parts of your code can access it easily. For example, in a before_request function, you can save the user from the request headers into g.user. Then, in your route function, you read g.user to personalize the response. After the request finishes, Flask clears g automatically, so the data does not leak into other requests. This makes g perfect for temporary, request-scoped data but not for sharing data between requests.