I'd like to do a "Your file is being prepared. Please wait 1 minute." page that auto-updates by making the client download the file when it's ready.
Obviously, this doesn't work:
from bottle import route, post, get, run, request, view, static_file, redirect
import time
from threading import Thread
def dothejob(i):
time.sleep(10) # the file takes 5 to 60 seconds to be prepared
with open('test.txt', 'w') as f:
f.write('Hello')
return static_file('test.txt', root='.', download=True) # error here
@get('/generatefile')
def generatefile():
i = request.params.get('id', '', type=str)
thread = Thread(target=dothejob, args=(i, ))
thread.start()
return "Your file is being prepared. Please wait 1 minute."
run(host='0.0.0.0', port=80, debug=True, quiet=True)
because the HTTP request doesn't exist anymore when dothejob
returns:
RuntimeError: Request context not initialized.
How to do properly such an auto-updating page when the file is ready on server?
Note:
I'd like to avoid
websockets
here (I already used it for more complex projects: chat, etc. and it sometimes does not work on certain connections that don't accept it + other reasons out of topic here) and go for the simplest solution possible.I'm wondering if it really needs an AJAX polling like Update and render a value from Flask periodically's answer, or if there is a simpler solution.