0
0
Flaskframework~10 mins

G object for request-scoped data in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask global object used for request-scoped data.

Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Ag
Brequest
Csession
Dapp
Attempts:
3 left
💡 Hint
Common Mistakes
Importing request instead of g.
Using session which is for user sessions, not request data.
2fill in blank
medium

Complete the code to store a value in the Flask g object during a request.

Flask
from flask import g

def before_request():
    g.[1] = 'user123'
Drag options to blanks, or click blank then click option'
Auser_id
Bsession_id
Capp_name
Drequest_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using session_id which is not typically stored in g.
Using unrelated names like app_name.
3fill in blank
hard

Fix the error in accessing the g object attribute inside a view function.

Flask
from flask import g

@app.route('/')
def index():
    user = g.[1]
    return f"Hello, {user}!"
Drag options to blanks, or click blank then click option'
Auserid
BuserId
CUser_id
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase or different casing like userId or User_id.
Using all lowercase but missing underscore like userid.
4fill in blank
hard

Fill both blanks to correctly check if an attribute exists in g and use it.

Flask
from flask import g

@app.route('/profile')
def profile():
    if hasattr(g, '[1]'):
        user = getattr(g, '[2]')
    else:
        user = 'Guest'
    return f"Profile: {user}"
Drag options to blanks, or click blank then click option'
Auser_id
Busername
Cuser
Dprofile_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using different attribute names in hasattr and getattr.
Using attribute names not set in g.
5fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores lengths of strings in g if length is greater than 3.

Flask
from flask import g

words = ['apple', 'bat', 'carrot']
g.lengths = {word: len(word) for word in words if len(word) [1] 3 and 'a' [2] word}
Drag options to blanks, or click blank then click option'
A:
B>
Cin
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of : in dictionary comprehension.
Using == instead of > for length comparison.
Using == instead of in to check substring presence.