0
0
Flaskframework~20 mins

G object for request-scoped data in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask G Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask route using g?
Consider this Flask route that uses the g object to store data during a request. What will be the output when accessing /show?
Flask
from flask import Flask, g
app = Flask(__name__)

@app.before_request
def before():
    g.user = 'Alice'

@app.route('/show')
def show():
    return f"User is {g.user}"
AUser is Alice
BAttributeError: 'g' object has no attribute 'user'
CKeyError: 'user'
DUser is None
Attempts:
2 left
💡 Hint
Remember that g is used to store data during a request and is reset for each request.
state_output
intermediate
2:00remaining
What happens to g data across multiple requests?
If you set g.value = 10 in one request, what will g.value be in the next request if not set again?
Flask
from flask import Flask, g
app = Flask(__name__)

@app.route('/set')
def set_value():
    g.value = 10
    return 'Value set'

@app.route('/get')
def get_value():
    return str(getattr(g, 'value', 'No value'))
ANo value
BAttributeError
C10
D0
Attempts:
2 left
💡 Hint
Think about how Flask handles the g object for each request.
🔧 Debug
advanced
2:00remaining
Why does this code raise RuntimeError when accessing g?
This Flask app tries to access g.user outside a request context. What causes the error?
Flask
from flask import Flask, g
app = Flask(__name__)

def get_user():
    return g.user

print(get_user())
ABecause <code>g</code> is a global variable accessible anytime
BBecause <code>g.user</code> was never set
CBecause <code>g</code> is only available during a request context
DBecause Flask app is not running
Attempts:
2 left
💡 Hint
Think about when Flask creates the g object.
🧠 Conceptual
advanced
2:00remaining
What is the main purpose of Flask's g object?
Choose the best description of what the g object is used for in Flask.
ATo store data that lasts across multiple requests and users
BTo manage database connections globally
CTo store configuration settings for the Flask app
DTo store data during a single request, accessible across functions
Attempts:
2 left
💡 Hint
Think about the lifespan of data stored in g.
📝 Syntax
expert
3:00remaining
Which option correctly sets and accesses g data in Flask?
Select the option that correctly sets g.user in a before_request function and accesses it in a route.
A
from flask import Flask, g
app = Flask(__name__)

def before():
    g.user = 'Bob'

@app.route('/')
def index():
    return f"User: {g.user}"
B
from flask import Flask, g
app = Flask(__name__)

@app.before_request
def before():
    g.user = 'Bob'

@app.route('/')
def index():
    return f"User: {g.user}"
C
from flask import Flask, g
app = Flask(__name__)

@app.before_request
def before():
    user = 'Bob'

@app.route('/')
def index():
    return f"User: {g.user}"
D
from flask import Flask, g
app = Flask(__name__)

@app.before_request
def before():
    g.user = 'Bob'

@app.route('/')
def index():
    return f"User: {user}"
Attempts:
2 left
💡 Hint
Remember to use the @app.before_request decorator and access g.user properly.