问题
I've written an algorithm in python and a web interface around that. After you submit the form and start the algorithm, I'd like to push and update data on the page as it's running. How can I accomplish this?
回答1:
To have real-time or semi-real time communications between the web page the options are
Automatically refresh the page after certain seconds using meta refresh tag in HTML
<head>
Fetch updated data with JavaScript and AJAX HTTP GET: https://api.jquery.com/jquery.get/
Use server-sent sent events: http://www.html5rocks.com/en/tutorials/eventsource/basics/
Use WebSockets: http://www.html5rocks.com/en/tutorials/websockets/basics/
All approaches, excluding the first one, require rudimentary JavaScript skills besides knowing server-side Python. The latter two approaches recommend advanced understanding of real-time communications. Thus, if you are not familiar with the web development I recommend picking the meta refresh tag.
On the server side you need to start a process or a thread which to handle the long running process, then have this process to write its progress to a database. When the web UI updates itself, it reads the latest results from the database and pushes/pulles them back to the browser.
来源:https://stackoverflow.com/questions/31197747/pushing-updates-from-python-server-to-a-web-interface