2

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.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • By the way: you really don't want to do real background work in a thread. Use a queue like Celery or RabbitMQ. – Jonathon Reinhart Jun 20 '18 at 09:59
  • @JonathonReinhart Yes, I just used this to have a simple minimal example, but in real life, I'll use something like Celery. – Basj Jun 20 '18 at 10:00

1 Answers1

0

I tried to write a solution (not 100% sure such a polling is really necessary). Of course the HTML+JS has to be moved into real HTML / JS files, but here I kept it small for a minimal example:

from bottle import get, run, request, static_file
import time
from threading import Thread
import os

def dothejob(i):
    time.sleep(5)  # the file takes 5 to 60 seconds to be prepared
    with open('test.txt', 'w') as f:
        f.write('Hello')

@get('/<filename>')
def static(filename):
    return static_file(filename, root='.', download=True)

@get('/isready')
def isready():
    return '1' if os.path.exists('test.txt') else '0'

@get('/')
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 5 seconds (do not reload the page)...
<script>
poll = function () {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/isready');
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            if (xhr.responseText == 1)
                document.write('Your file is now ready: <a href="/test.txt">download link</a>');
            else
                setTimeout(poll, 1000);
        }
    }
    xhr.send(null);
}

setTimeout(poll, 1000);
</script>
"""    

run(host='0.0.0.0', port=80, debug=True, quiet=True)
Basj
  • 41,386
  • 99
  • 383
  • 673