0

I am using Python 3.6.1 with Flask, with the goal being to load a webpage, display live loading information via websockets, then once the loading is complete redirect or otherwise allow the user to do something.

I have the first two steps working- I can load the page fine, and the websocket interface (running on a separate thread using SocketIO) updates properly with the needed data. But how can I make something happen once the functions that need to load are finished? To my understanding once I return a webpage in Flask it is simply static, and there's no easy way to change it.

Specific code examples aren't necessary, I'm just looking for ideas or resources. Thanks.

rxbots
  • 87
  • 1
  • 8
  • 1
    Flask just sends a single response to a single request. It cannot redirect after that. The redirection has to be implemented in the client side, i.e. in JavaScript. – zvone Oct 26 '20 at 14:58
  • @zvone Is there a way to trigger a JavaScript redirect via Flask? – rxbots Oct 26 '20 at 15:00
  • Well you should definitely use client-side scripting, like JavaScript, for this purpose. If you need to redirect the user after the page is done loading, add "window.onload = function() { ... }"; you know the deal – Captain Trojan Oct 26 '20 at 15:02
  • Does this answer your question? [Redirecting to a relative URL in JavaScript](https://stackoverflow.com/questions/1655065/redirecting-to-a-relative-url-in-javascript) – simkusr Oct 26 '20 at 15:04
  • @simkus Thanks for the link. It sorta does, but I am still not sure how to TRIGGER the redirect. I need it to be activated once the backend python code is finished processing- the front end loads instantly and uses a websocket chat to display status. Once the processing is complete it should redirect. – rxbots Oct 26 '20 at 15:27
  • Add an if statement that shecks when status is ok and then redirect to where you need it – simkusr Oct 26 '20 at 15:44
  • You cannot trigger anything from the backend directly after the fact. You have two options: either poll the server constantly from JavaScript to ask "are you ready?" until it says yes and then redirect, or implement a way to "push" messages from the server (which is way more complex) – zvone Oct 27 '20 at 16:06

0 Answers0