0
0
Flaskframework~20 mins

Why sessions manage user state in Flask - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do sessions store user data on the server side?

In Flask, sessions help keep track of user information across requests. Why is it important that session data is stored on the server side rather than only in the browser?

ABecause storing data on the server keeps it secure and prevents users from changing it directly.
BBecause browsers cannot store any data at all, so server storage is the only option.
CBecause server storage makes the website load faster by avoiding browser caching.
DBecause sessions only work if data is stored in cookies on the client side.
Attempts:
2 left
💡 Hint

Think about what could happen if users could change their own session data.

component_behavior
intermediate
2:00remaining
What happens to session data when a user closes their browser?

Consider a Flask app using sessions to track user login status. What happens to the session data if the user closes their browser and then reopens the site?

AThe browser saves all session data permanently, so the user never needs to log in again.
BThe session data stays forever on the server and automatically logs the user back in.
CThe server deletes the session data immediately when the browser closes.
DThe session data is lost if the session cookie is not persistent, so the user must log in again.
Attempts:
2 left
💡 Hint

Think about how cookies control session lifetime.

📝 Syntax
advanced
2:00remaining
Identify the correct way to set a session variable in Flask

Which option correctly sets a session variable named 'username' to 'alice' in a Flask route?

Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/')
def index():
    # Set session variable here
    pass
    return 'Done'
Asession['username'] = 'alice'
Bsession.username = 'alice'
Csession.add('username', 'alice')
Dsession.set('username', 'alice')
Attempts:
2 left
💡 Hint

Remember how to use dictionary syntax in Python.

state_output
advanced
2:00remaining
What is the output after modifying session data in Flask?

Given this Flask route, what will be the output when visiting '/count' three times in a row?

Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/count')
def count():
    if 'visits' in session:
        session['visits'] += 1
    else:
        session['visits'] = 1
    return f"Visit number: {session['visits']}"
AVisit number: 1 every time because session resets
BVisit number: 1, then 2, then 3 on consecutive visits
CVisit number: 3 on the first visit and stays the same
DError because session['visits'] is not initialized
Attempts:
2 left
💡 Hint

Think about how session data persists between requests.

🔧 Debug
expert
3:00remaining
Why does this Flask session code raise an error?

Examine this Flask route code. Why does it raise a RuntimeError: Working outside of request context when trying to access session?

Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

user_session = session

@app.route('/')
def index():
    user_session['logged_in'] = True
    return 'Logged in'
ABecause the route function must return a JSON response, not a string.
BBecause the secret key is missing, so session cannot be used.
CBecause session can only be accessed inside a request context, not at the module level.
DBecause session must be imported from flask.sessions, not flask.
Attempts:
2 left
💡 Hint

Think about when Flask creates the session object.