0

I am trying to create a web app using Python - Flask. For which I am reading the data to be displayed in the HTML table from an XML. The problem is the data is not real time and hence whenever the XML changes I have to stop the application and re-execute it.

Is there a way this data can be parsed and displayed on run time?

Martin Evans
  • 45,791
  • 17
  • 81
  • 97

1 Answers1

0

There are at least two ways to achieve it.

  1. Polling. The idea is to make the browser (with Javascript) to request the server for a new version of your file every N seconds. But as you can guess, this is not absolurely "realtime" because some time may pass before you receive changes. The less N is, the faster you will receive changes, but also the more server performance will be needed.

Benefits: relatively easy to implement

Drawbacks: requires more server performance and bandwidth

How you can implement it: make sure you open (and parse - if you do it on server) the file NOT on server startup but when handling the request "Give me the XML". If you do that, you will not need to restart your server to get changes, just refresh the page in browser. Then you can program your browser page to refresh itself every N seconds in JavaScript, like this

setInterval(location.reload, N * 1000);
  1. Long polling. More advanced version of polling. Polling interval is quite long, like 1 minute, but polling requests are not usual, because their timeout is huge - like several minutes. Such a timeout allows server to answer the request in that time. If nothing has changed during that time, server will say that by the end of it, and the client (browser) will go on the next iteration of the long polling loop. If something has changed, the server will answer right at that moment (send changes), and the client will receive those changes immediately.

Benefits: less server performance and bandwidth are needed

Drawbacks: rather complicated to implement

See this question for more information about long polling.

Community
  • 1
  • 1
Andrew Che
  • 928
  • 7
  • 20