0

I want to update my template querying every 5 second but I get the same value for every case. How do I keep refreshing and retrieve the values per each 5 second? I want to implement monitoring script querying my database every 5 second.

@app.route('/')
def index():
    while True:
      conn = MySQLdb.connect(host=host,
                             user=username,
                             passwd=password,
                             db=database,
                             port=port)
      df = sqlio.read_sql(qry1, conn)
      value = df['id'][0]
      print value
      conn.close()
      time.sleep(5)
      return render_template('index.html', thev=value)



if __name__ == '__main__':
    app.run(
        port=8001,
        host='0.0.0.0'
    )

1 Answer 1

3

The while True part of your code doesn't do anything - when a request is made, the template will be rendered once and the function exits.

You'll need to do something on the client side to refresh the data. This could be as a simple as an auto-refresh, or you could do something in javascript to asynchronously update the data without refreshing the page (lots of examples online, here's the section from the Flask mega tutorial)

Sign up to request clarification or add additional context in comments.

2 Comments

Does it mean that index() gets to be called every second without while?
No, index() is called once when the client makes a request to the server (i.e. when you open the page in a browser)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.