Challenge - 5 Problems
Flask Session Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask session code?
Consider this Flask route that sets and reads session data. What will be the printed output when a user visits the route twice in the same browser session?
Flask
from flask import Flask, session app = Flask(__name__) app.secret_key = 'secret' @app.route('/') def index(): if 'visits' in session: session['visits'] += 1 else: session['visits'] = 1 print(f"Visits: {session['visits']}") return f"You have visited {session['visits']} times."
Attempts:
2 left
💡 Hint
Think about how session data persists across requests in the same browser session.
✗ Incorrect
The session object stores data per user session. On first visit, 'visits' is set to 1. On second visit, it increments to 2. So the printed output reflects the count increasing.
📝 Syntax
intermediate1:30remaining
Which option correctly sets a value in Flask session?
You want to store the username 'alice' in the Flask session. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
Remember, Flask session behaves like a dictionary.
✗ Incorrect
Flask session is a dictionary-like object. You set values by assigning to keys like session['key'] = value. Methods like set() or add() do not exist.
🔧 Debug
advanced2:00remaining
What error occurs with this Flask session code?
This Flask route tries to increment a session counter but raises an error. What is the error and why?
Flask
from flask import Flask, session app = Flask(__name__) app.secret_key = 'secret' @app.route('/') def index(): session['count'] += 1 return f"Count is {session['count']}"
Attempts:
2 left
💡 Hint
What happens if you try to increment a key that is not yet in the session?
✗ Incorrect
The code tries to increment session['count'] without initializing it first. Since 'count' key is missing, a KeyError is raised.
❓ state_output
advanced2:30remaining
What is the session content after these requests?
Given this Flask app, what is the content of session after visiting /set and then /increment?
Flask
from flask import Flask, session app = Flask(__name__) app.secret_key = 'secret' @app.route('/set') def set_value(): session['counter'] = 5 return 'Counter set to 5' @app.route('/increment') def increment(): session['counter'] += 1 return f"Counter incremented to {session['counter']}"
Attempts:
2 left
💡 Hint
Think about the order of requests and how session data persists.
✗ Incorrect
Visiting /set sets session['counter'] to 5. Then visiting /increment adds 1, making it 6. So session contains {'counter': 6}.
🧠 Conceptual
expert3:00remaining
Why must Flask app have a secret_key to use session?
Flask sessions store data client-side in cookies. Why is setting app.secret_key required for sessions to work securely?
Attempts:
2 left
💡 Hint
Think about how Flask protects session data stored in cookies.
✗ Incorrect
Flask signs session cookies with secret_key to ensure data is not altered by clients. It does not encrypt data by default, nor store it server-side.