1

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.

4
  • 1
    You can return an HTML page OR redirect to another page. If you try doing both, the user will not be able to see the original HTML because of immediate redirection. Commented May 5, 2017 at 15:55
  • Do you mean you want to run a continuous script and get live output from it on the page? Commented May 5, 2017 at 15:59
  • @AndrewCherevatkin: I am able to get live output from the script. But once the script runs I want to redirect to some page. Commented May 5, 2017 at 16:06
  • 2
    Maybe client-side (javascript) code will work better in this case? Like making an event listener or periodically checking if some output from the server has come, if yes - redirect by changing location.href Commented May 5, 2017 at 16:09

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.