Skip to content

Instantly share code, notes, and snippets.

@natanfeitosa
Created March 24, 2022 05:56
Show Gist options
  • Save natanfeitosa/f3551e3113627076f16b1ac76e98136a to your computer and use it in GitHub Desktop.
Save natanfeitosa/f3551e3113627076f16b1ac76e98136a to your computer and use it in GitHub Desktop.
from flask import Flask, request, make_response
app = Flask(__name__)
@app.route('/')
def visit_counter():
visits = int(getattr(request.cookies, 'visits', 0)) + 1
msg = f'''
<h1>
{visits} visita{"s" if visits > 2 else ""}
</h1>
'''
res = make_response(msg)
res.set_cookie('visits', str(visits))
return res
@app.route('/reset')
def reset_counter():
res = make_response('Resetting... ok')
res.set_cookie('visits', '0', 0)
return res
if __name__ == "__main__":
app.run(host='0.0.0.0', port=4000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment