Flask : Redirect after returning a Flask Response object

I'm working on a web app where I'm running a shell script and printing live output by returning Response object. Is there a way I can redirect to another page once the shell script is done running?

@app.route("/setup", methods=['POST', 'GET'])
def setup():
    def inner():
        command = ["./test.bash", "exam"]
        proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
        for line in iter(proc.stdout.readline,''):
            yield line.rstrip() + '<br/>\n'
    return Response(inner(), mimetype='text/html')
    # After this I want to redirect to another page say success.html

Or if there is a way, I can include a template within the Response object, I can include some buttons to navigate to the page I want to.

I also want to do something like:

for line in iter(proc.stdout.readline,''):
    if line == "Success":
        # Redirect to success.html after returning Response object with script output
    else:
        # Redirect to error.html after returning Response object with script 

output

Is this possible? Any help is appreciated. Thanks in advance.

Upvotes: 1

Views: 1932

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113930

yield '<script>document.location.href="http://redirect.me"</script>'

might work at the end... depending on if the browser is interpretting the streamed data or just dumping it. that said a better teqnique in general is to use something like celery for long running tasks and then use js to interogate the server to see if its done

Upvotes: 2

Related Questions