How to return a website and automatically redirect to another side which takes long to load
I ask the user to input some data, after that I redirect him to a 'result' website whi开发者_StackOverflow中文版ch takes several seconds to load.
I don't like this, because there is no user feedback telling him, that the server has not crashed and that his result is coming. So I was thinking about showing the user a 'loading' website stating 'please wait' and then automatically redirect him to the 'result' page. The user would see the "please-wait"-website the whole time the other one is loading, as Flask does not serve the site before it is finished with the calculations.
[Attempt to clarify] One of the pages takes so long, because it waits for a file upload. The other side takes so long because its functions wait for data from a third party api.
How would I do that in Flask?
I was going to suggest Celery for this, but someone just posted this on the Flask mailing list, and this might be a more simpler solution: Hello-Redis-Tasks. If you still want to use Celery though, this is what you should check out: Custom states in celery
Flask's documentation for file uploads includes a section on implementing progress bars for file uploads. Choose one of the many client-side solutions to display the progress of the upload and then redirect once the upload is complete.
For the long-running server side process you have several options:
- Pre-fetch (and cache) the data from the slow-to-respond API.
- Hijack your link's action with JavaScript and make an ajax request for the data - update the DOM with a status message to let the user know that everything is still working.
- Have a three step process. Page #1 has a link to the "Please wait ... " page (Page #2). Page #2 has a meta refresh tag pointing at Page #3 (
<meta http-equiv="refresh" content="0; url=/path/to/page3">
)
精彩评论