from flask import Flask from flask import request from flask import session from flask import request from flask import render_template import secrets counter = Flask(__name__) counter.secret_key = secrets.token_bytes() # Come at the app without a form, you're new. Expect to have lost # your mind. (May or not be ideal) @counter.route("/",methods=['GET']) def init(): # Initialize the state. loggedin = session['loggedin'] = False count = session['count'] = 1 return render_template('counterpage.html', count=1, loggedin=False) @counter.route("/",methods=['POST']) def count(): # Get the original logged in value. (This example doesn't make it # hard to log in, but if we were more realistic, we'd still believe # the contents of the session.) loggedin = session['loggedin'] # Apply login request. if 'LOGIN' in request.form: loggedin = session['loggedin'] = True if 'LOGOUT' in request.form: loggedin = session['loggedin'] = False # Increment the count. session['count'] += 1 return render_template('counterpage.html', count=session['count'], \ loggedin=loggedin)