0
0
Flaskframework~10 mins

Flask session object - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Flask session object
Client sends request
Flask receives request
Load session data from cookie
Access/Modify session object in view
Process request and prepare response
Save session data to cookie
Send response back to client
This flow shows how Flask loads session data from the client's cookie, lets the app read or change it, then saves it back in the response cookie.
Execution Sample
Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/')
def index():
    session['count'] = session.get('count', 0) + 1
    return f"Count: {session['count']}"
This code counts how many times a user visits the page by storing a number in the session.
Execution Table
StepRequest CountSession BeforeActionSession AfterResponse Output
1First visit{}session.get('count', 0) + 1 = 1{'count': 1}Count: 1
2Second visit{'count': 1}session.get('count', 0) + 1 = 2{'count': 2}Count: 2
3Third visit{'count': 2}session.get('count', 0) + 1 = 3{'count': 3}Count: 3
4Fourth visit{'count': 3}session.get('count', 0) + 1 = 4{'count': 4}Count: 4
5Fifth visit{'count': 4}session.get('count', 0) + 1 = 5{'count': 5}Count: 5
💡 Session count increments on each visit; no exit condition as server keeps updating session.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
session['count']undefined12345
Key Moments - 3 Insights
Why does session['count'] start as undefined on the first visit?
Because the session cookie is empty on the first visit, so session.get('count', 0) returns 0, then we add 1 (see execution_table step 1).
How does Flask remember the session data between visits?
Flask stores session data in a secure cookie sent to the client. On each request, Flask reads this cookie to restore the session (see concept_flow step 'Load session data from cookie').
What happens if we don't set app.secret_key?
Flask cannot securely sign the session cookie, so session data won't be saved properly. The session will appear empty every time (not shown in execution_table but important).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is session['count'] after the third visit?
A2
B3
Cundefined
D4
💡 Hint
Check the 'Session After' column at step 3 in the execution_table.
At which step does the session first contain {'count': 1}?
AStep 1
BStep 2
CStep 3
DStep 0
💡 Hint
Look at the 'Session After' column in the execution_table for step 1.
If app.secret_key is missing, how would the session behave?
ASession would throw an error on access
BSession data would increment normally
CSession data would not persist between requests
DSession would store data on the server
💡 Hint
Refer to key_moments about secret_key importance.
Concept Snapshot
Flask session object stores user data between requests.
It uses a secure cookie to save session data.
Access session like a dictionary: session['key'] = value.
Set app.secret_key to enable session security.
Session data persists across page visits.
Use session.get('key', default) to avoid errors.
Full Transcript
The Flask session object lets you save small pieces of data for each user between their visits. When a user sends a request, Flask reads the session data from a cookie. You can read or change this session data in your view functions like a dictionary. After processing, Flask saves the updated session back into a cookie sent to the user. This example counts how many times a user visits a page by increasing a number stored in the session. The session starts empty on the first visit, so the count begins at 1. Each new visit reads the previous count, adds one, and saves it again. Remember to set app.secret_key so Flask can securely sign the session cookie. Without it, session data won't persist. This flow helps keep track of user-specific info without needing a database.