📖 Scenario: You are building a simple Flask web app that tracks user visits using sessions. You want to control how long the session lasts before it expires automatically.
🎯 Goal: Create a Flask app that sets a session lifetime of 5 minutes and stores a visit count in the session. The app should increment the count on each visit and reset after the session expires.
📋 What You'll Learn
Create a Flask app with session support
Set the session lifetime to 5 minutes
Store a visit count in the session
Increment the visit count on each page load
Ensure the session expires after 5 minutes of inactivity
💡 Why This Matters
🌍 Real World
Web apps often need to remember user data temporarily, like login status or preferences. Managing session lifetime helps improve security and user experience by controlling how long this data stays active.
💼 Career
Understanding session management is essential for backend web developers working with Flask or similar frameworks to build secure and user-friendly applications.
Progress0 / 4 steps
1
Set up Flask app and secret key
Create a Flask app instance called app and set app.secret_key to the string 'mysecret'.
Flask
Hint
The secret key is needed to use sessions securely in Flask.
2
Configure session lifetime
Import timedelta from datetime and set app.permanent_session_lifetime to timedelta(minutes=5).
Flask
Hint
Use timedelta to specify the session lifetime duration.
3
Create route to track visits with session
Import session and redirect, url_for from flask. Create a route / with a function index() that sets session.permanent = True, increments session['visits'] by 1 (initialize to 1 if not present), and returns the visit count as a string.
Flask
Hint
Mark the session as permanent to apply the lifetime. Use session like a dictionary to store visit counts.
4
Run the Flask app
Add the code to run the Flask app with app.run(debug=True) inside the if __name__ == '__main__': block.
Flask
Hint
This code starts the Flask server so you can test your app in a browser.
Practice
(1/5)
1. What does setting session.permanent = True in a Flask app do?
easy
A. It makes the session use the permanent_session_lifetime duration.
B. It deletes the session immediately after the request ends.
C. It disables session cookies in the browser.
D. It resets the session lifetime to zero.
Solution
Step 1: Understand session permanence in Flask
Setting session.permanent = True tells Flask to keep the session alive for a set duration instead of ending it when the browser closes.
Step 2: Connect to permanent_session_lifetime
The session will then use the time defined in app.permanent_session_lifetime to decide how long it lasts.
Final Answer:
It makes the session use the permanent_session_lifetime duration. -> Option A
Quick Check:
session.permanent = True means use permanent_session_lifetime [OK]
Hint: Permanent session means use app.permanent_session_lifetime [OK]
2. Which of the following is the correct way to set a session lifetime of 30 minutes in Flask?
easy
A. app.permanent_session_lifetime = timedelta(minutes=30)
B. session.lifetime = 30
C. app.session_lifetime = 30
D. session.permanent_session_lifetime = 30
Solution
Step 1: Identify the correct Flask attribute for session lifetime
The Flask app uses app.permanent_session_lifetime to set how long permanent sessions last.
Step 2: Use the correct data type
This value must be a timedelta object from Python's datetime module, e.g., timedelta(minutes=30).
Final Answer:
app.permanent_session_lifetime = timedelta(minutes=30) -> Option A
Quick Check:
Use app.permanent_session_lifetime with timedelta [OK]
Hint: Use app.permanent_session_lifetime with timedelta object [OK]
Common Mistakes:
Setting session.lifetime instead of app.permanent_session_lifetime
Assigning integer instead of timedelta
Using session.permanent_session_lifetime which does not exist
3. Given this Flask code snippet, what will be the session lifetime if the user sets session.permanent = True and app.permanent_session_lifetime = timedelta(hours=1)?
5. You want to create a Flask app where sessions last 2 hours, but only if the user chooses 'Remember me'. Otherwise, sessions should expire when the browser closes. Which code snippet correctly implements this?