Set and Read the session Dict
Store per-user data across requests.
Set and Read the session Dict is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Sessions Exist
HTTP forgets you after every request. A session lets your Flask app remember a single user across many requests. 🧠
It Feels Like a Dict
Flask gives you a session object that behaves just like a Python dictionary. You read and write keys on it as usual.
from flask import sessionImport It First
You always pull session straight from flask. It is a special proxy that points at the current user's session for you.
from flask import Flask, sessionWrite a Value
To store data, assign a key on session. Here we remember who is logged in for this visitor.
@app.route('/login')
def login():
session['user'] = 'ada'
return 'Saved!'Read It Back
On a later request you read the same key from session, and the value is still there for that user.
@app.route('/me')
def me():
return session['user']Read Safely with get
Use session.get instead of brackets so a missing key returns None rather than raising a KeyError.
name = session.get('user', 'guest')Check If a Key Is There
You can test membership too. The in operator tells you whether a value was ever stored for this user.
if 'user' in session:
print('known visitor')Remove One Value
Logging out usually means dropping a key. Use session.pop with a default so it never errors when absent.
session.pop('user', None)Clear Everything
To wipe the whole session in one call, use session.clear. Every stored key for that user is gone.
session.clear()Per User, Not Global
Each browser gets its own session. What you store for one visitor never leaks into another person's session.
Keep It Small
Store only tiny markers like a user id, not big objects. The session rides along with every request, so size matters.
Quick Check
You want to read a session value without crashing if it is missing.
Recap
You learned that session is a per-user dict: set keys to save data, read with get, and clear to log out. Nice work! 🎉
Frequently asked questions
Is the “Set and Read the session Dict” lesson free?
Yes — the full text of “Set and Read the session Dict” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Set and Read the session Dict”?
Store per-user data across requests. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Set and Read the session Dict” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Set and Read the session Dict
- The SECRET_KEY and Signed Cookies
- Set Custom Cookies on a Response
- Flash Messages Between Requests