I'm design a web application with python flask, the processing flow is like:
- User choose a specified a URL, and ask server site to do some time-consuming task;
- The time-consuming task is then running in a new thread;
- After #2 is done, server will update some info regarding the URL, I need to refresh the page in #1
I don't know how to notify the "completion" status to front-end so as to refresh the page.
Anyone can help me out? Thanks
Using flask-socketio would probably work for what you're asking. It establishes a connection between the front-end and back-end that lets the backend emit a message and the front-end registers a callback that fires when it receives the event.
app.py
templates/index.html
Then run the app with
python app.py
. Visit the app in the browser athttp://127.0.0.1:5000/
and click the button. The button will send the'long-running-event'
request to flask and flask processes it by sleeping for 5 seconds to simulate a long running process. Then flask emits the'processing-finished
message and javascript fires the callback registered to listen for that event by alerting the message that flask sent.