from flask import Flask from flask import request from flask import session from flask import url_for from flask import request import secrets counter = Flask(__name__) counter.secret_key = secrets.token_bytes() @counter.route("/",methods=['POST', 'GET']) def count(): # Apply the logged in value from the session. loggedin = False if 'loggedin' in session: loggedin = session['loggedin'] # Apply login request. if 'LOGIN' in request.form and request.form["LOGIN"] == "LOGIN": loggedin = session['loggedin'] = True if 'LOGOUT' in request.form and request.form["LOGOUT"] == "LOGOUT": loggedin = session['loggedin'] = False # Increment the count, with default if not present. count = 1 if 'count' in session: count = session['count'] + 1 session['count'] = count # Text tweaking notornot = "" if loggedin else " not" buttxt = "LOGOUT" if loggedin else "LOGIN" return """ Flask Sessions

Hello, Flask

This script passes state using a Flask session.

The current count is %d. You are%s logged in.

""" % (count, notornot, buttxt, buttxt)