0
0
Flaskframework~20 mins

Session data storage in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session Storage 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 session code?
Consider this Flask route that sets and reads session data. What will be printed when accessing the '/count' URL twice 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 count: {session['visits']}"
AKeyError on second visit
BVisit count: 0 on first visit, Visit count: 1 on second visit
CVisit count: 1 on first visit, Visit count: 2 on second visit
DVisit count: 1 on both visits
Attempts:
2 left
💡 Hint
Think about how Flask session stores data between requests for the same client.
📝 Syntax
intermediate
1:30remaining
Which option correctly sets a session variable in Flask?
You want to store the username 'alice' in the Flask session. Which code snippet does this correctly?
Asession['username'] = 'alice'
Bsession.username = 'alice'
Csession.set('username', 'alice')
Dsession.add('username', 'alice')
Attempts:
2 left
💡 Hint
Flask session behaves like a dictionary.
🔧 Debug
advanced
2:00remaining
Why does this Flask session code raise a RuntimeError?
Examine this Flask code snippet. Why does it raise a RuntimeError: Working outside of request context?
Flask
from flask import session

def set_user():
    session['user'] = 'bob'

set_user()
ABecause 'user' is a reserved session key
BBecause session can only be accessed inside a request context
CBecause session requires a database connection
DBecause the secret_key is missing
Attempts:
2 left
💡 Hint
When does Flask create the session object?
state_output
advanced
2:00remaining
What is the session content after this sequence of requests?
Given this Flask route, what will be the session content after visiting '/add/5' then '/add/3'?
Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/add/<int:num>')
def add(num):
    total = session.get('total', 0)
    total += num
    session['total'] = total
    return f"Total: {total}"
A{}
B{'total': 3}
C{'total': 5}
D{'total': 8}
Attempts:
2 left
💡 Hint
Session data persists and accumulates across requests from the same client.
🧠 Conceptual
expert
2:30remaining
Which statement about Flask session storage is true?
Select the correct statement about how Flask session stores data by default.
AFlask session stores data on the client inside a signed cookie
BFlask session stores data on the server in a database by default
CFlask session data is encrypted and stored in server memory
DFlask session requires Redis to store session data
Attempts:
2 left
💡 Hint
Think about where Flask keeps session data without extra setup.