简体   繁体   中英

Integrating an HTML GUI and Python with heavy computation

I'm just now tapping into real GUI-heavy stuff and I'm looking to integrate a GUI for a Python 3 application/("game"?). I need this to be cross-platform, responsive, and able to communicate with other users and a server.

I've looked at desktop application creation but I think that the best thing would be a web application. Now, here are the obstacles:

  1. I need the web app to be able to use all my Python 3 code as its black box; it needs to be able to send a function call to the running python code.
  2. More often than not, everything on the page is changed if a single value is changed.
  3. If another user changes a value, it would affect multiple other users instantaneously.
  4. I can't have the page constantly refresh.
  5. The HTML "GUI" needs to have some degree of separation from the Python code. (I can't have Python sending snippets of HTML, only data is allowed.)

I realize that this is a lot to address, so my question then is: How would one go about making a web app that has specific (most) data values update consistently without having to refresh the entire page?

My proposed solution would have some sort of simplistic javascript constantly checking to see if anything has been posted for the users closely interacting with each other (players in the same server, if you will). Each value in the HTML would have a linked javascript function which is linked to a value or function in the backend python code.

"Player" changes value on webpage -> JavaScript function call -> Python black box on "Server" -> JavaScript checking loop sees that there has been a change via simple Python variable on "Server" -> JavaScript gets all values via calls to functions in python code -> Update HTML for all "Players"

Finally, please note, I have no idea how to carry out my proposed solution, and I very much welcome a different, better approach. Thanks in advance.

So, for anyone who is trying to do the same thing as me, the answer is something called " websockets ".

I initially tried Kivy, but found that it was too cumbersome and just hard to get good styling in. Using HTML, CSS, and JavaScript, you can make pretty fancy looking stuff, and Kivy just isn't robust enough to make something attractive without having to write tons of excess code. Since what I'm trying to make is basically a styled data readout/hud, the HTML combination takes the cake.

What I'm currently using for the websockets is a module called " Tornado " which allows you to update any data on the client side without them reloading the page. I'm sending this data as a single JSON to update the whole page.

In conclusion, I decided on HTML/CSS/JavaScript for the GUI and Python/Tornado for fast server-side computation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM