1

I am working on a little personal project with the aim of using Python to obtain and process data from a running game and use HTML/CSS to output this data using Flask.

I am very new to Python, but have some basic exposure to HTML/CSS and JavaScript.

Using example code from the web and various tutorials I have been able to get two distinct sections of this working to a proof of concept stage:

  • My python code runs in an interuptible loop and will output a variable every X seconds to the console.

  • I have setup a Flask application that I can pass a variable to in a Python script and display in a browser.

My problem is I am stumped with how I go about joining these two together. In the Python script that runs the Flask app nothing else happens while the Flask app is running, I assume because it is executing synchronously. This means that I can't start the Flask app and then have my loop run after that, how can I get both to work simultaneously?

EDIT:

After more thinking about this and some further research I think I have framed the problem incorrectly due to the way I have setup my local environment.

My original thinking was that the Python script which runs the Flask server could also be used to recieve/process and output my game logic*. I now believe this to be incorrect because in typical usage Flask would be running on a web server somewhere serving the website while the data from the game would have to be collected and processed by an application local to the running of the game.

So, given the above the question could be better posed as:

How to dynamically display (say with a maximum refresh interval of 1s) a Python variable that is constantly changing using a Flask server? How do I get these two separate parts of my project to communicate with one-another?

*If this were possible (and I don't know its not) it would actually accomplish what I want from this project but is not likely to be a particularly useful skill so I would like to figure out how to do this the correct way in case this becomes something I would like to make available to others.

Pamellaaa
  • 29
  • 4
  • Possible duplicate of: https://stackoverflow.com/questions/32084321/reading-from-standard-input-using-flask-script-python – Charles Landau Nov 29 '18 at 15:12
  • 1
    Can you elaborate on this: `Ultimately I want to be able to use Ajax calls in javascript to show a constantly updating variable on a website`? – Silver Nov 29 '18 at 15:12
  • 1
    Also perhaps explain in more detail how your game needs to interact with Flask because you're trying to use a loop but there's likely a better way of doing what you want. – Silver Nov 29 '18 at 15:14
  • @SilverSlash I had edited the Ajax section to hopefully make it clearer. The interaction between game and flask is limited to the game data being read X times a second and processed by my app. The loop I have working is basically just the example code provided with the python implementation of the games API so I wanted to avoid messing with this too much. – Pamellaaa Nov 29 '18 at 15:41

0 Answers0