Flask Counter Using State 1
[run]
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 """ <html> <head> <meta charset="utf-8"> <title>Flask Sessions</title> <link rel="stylesheet" href="/static/genstyle.css" type="text/css"> <style> p, form * { text-align: center; } </style> </head> <body> <h1>Hello, Flask</h1> <p>This script passes state using a Flask session.</p> <form method="post"> <p>The current count is %d. You are%s logged in.</p> <div><input type="submit" name="%s" value="%s"> <input type="submit" name="next" value="next"></div> </form> </body> </html>""" % (count, notornot, buttxt, buttxt)