flask session timeout

To set a timeout for a Flask session, follow these steps:

  1. Import the necessary modules:
from datetime import timedelta
from flask import Flask, session
  1. Initialize the Flask app:
app = Flask(__name__)
  1. Set the session configuration to include a permanent session timeout:
app.permanent_session_lifetime = timedelta(minutes=20)
  1. Use the session object to store and retrieve data:
def index():
    session['username'] = 'example_user'
    return 'Session username set'

def get():
    return 'Session username: ' + session.get('username', 'Guest')
  1. Run the Flask app:
if __name__ == '__main__':