so i'm creating simple web page using Flask, just for practice and i have this little problem. i want to count how many times i've reloaded page. for example:
count = 0
@app.route("/")
def home():
print(count)
count = 1
return "testing"
but this doesn't work. if you guys know anything about it please help. <3 Thanks!
CodePudding user response:
the above code will work, but since you are trying to access the count
variable from a function, you need to declare in the function that its a global
:
count = 0
@app.route("/")
def home():
global count
print(count)
count = 1
return "testing"
more info on global variables here
CodePudding user response:
This isn't the problem related to the flask. It's related to the python global variable. You just need to access your global variable declared globally inside the function using the global
variable. Check here for more details related to python variable types.
The code can be updated as
count = 0
@app.route("/")
def home():
global count
print(count)
count = 1
return "testing"
Thanks