Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i count hoow many time i've reloaded page in python Flask?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading