0

So, here's what I have. A very simple Flask app, with one route that listens for POST and GET methods. I have a separate python script that is constantly outputting data. Right now, it is post'ing that data to the flask app, which takes that data, appends it to a list, then when someone visits or refreshed the webpage, the entire list is rendered into a template. I would like to not have to refresh the page to see this, and would rather it be just a live feed of data.

Currently I don't know how to use either SocketIO or AJAX, but I know that one of these is what I should be using. My ask here is which one? There will eventually be more scripts sending data to this app, which will ultimately turn into a dashboard of sorts. Starting small for now and getting just one working how I want. So my ask is, which would be a better solution, or are they both equal given what I want to do?

SkrillKill
  • 71
  • 5
  • 1
    `AJAX` can be simpler to create and it doesn't need to extra modules so it should work on all browsers (even older one). `SocketIO` (and `websockets`) needs extra module in `Flask`, and probably extra library in `JavaScript`. It can be more complex at start. Flask has to run two loops - one to work with standard requets, and other to work with websockets. But `websocket` was invented to send information from server to client - and update data on page. And it can be simpler when you will know how it work. – furas Oct 01 '22 at 21:02
  • From what I've gathered so far, AJAX is better suited for cases where the user does something on the page (like enter text or hit a button) and you want to dynamically update part of the page. Where as using a socket allows the server to initiate the request to reload the page; which in my case is probably a better option? – SkrillKill Oct 02 '22 at 02:25
  • try both and you will see which one is for you. `AJAX` should be simpler to use and you should find many examples on blogs and in questions on Stackoverflow. I saw many question where people sends image from webcam to server and get back image with modifications (ie. detect faces). [github / furas / python-examples / flask / take image and upload to server](https://github.com/furas/python-examples/tree/master/flask/web%20camera%20in%20browser%20-%20canvas%20-%20take%20image%20and%20upload%20to%20server) and more [examples](https://github.com/furas/python-examples/tree/master/flask) – furas Oct 02 '22 at 10:59

0 Answers0