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 Answers1

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)

Community
  • 1
  • 1
chrisb
  • 49,833
  • 8
  • 70
  • 70
  • Does it mean that `index()` gets to be called every second without `while`? –  Aug 12 '14 at 13:30
  • No, `index()` is called once when the client makes a request to the server (i.e. when you open the page in a browser) – chrisb Aug 12 '14 at 14:04