What is the best way to implement a forced page refresh using Flask?

前端 未结 4 1609
夕颜
夕颜 2021-01-31 18:24

Background
I have a large number of fields that will be updating real time from an external process. I would like to update the Flask hosted pages periodic

4条回答
  •  灰色年华
    2021-01-31 18:41

    To avoid refreshing the entire page you want to use what is called AJAX. It looks like this is easy to implement in flask.

    Since you want it to happen periodically you need to call your AJAX functions from a timer function in javascript.

    This means you just put the javascript from the flask page inside a timer call.

    Here's approximately what the javascript would look like:

    setInterval(                               //Periodically 
      function()
      {
         $.getJSON(                            //Get some values from the server
            $SCRIPT_ROOT + '/get_values',      // At this URL
            {},                                // With no extra parameters
            function(data)                     // And when you get a response
            {
              $("#result").text(data.result);  // Write the results into the 
                                               // #result element
            });
      },
      500);                                    // And do it every 500ms
    

提交回复
热议问题