Pushing updates from Python server to a web interface

南楼画角 提交于 2020-06-08 17:28:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!