I want to be able to visit a webpage and it will run a python function and display the progress in the webpage.
So when you visit the webpage you can see the output of the script as if you ran it from the command line.
Based on the answer here
How to continuously display python output in a webpage?
I am trying to display output from PYTHON
I am trying to use Markus Unterwaditzer's code with a python function.
import flask
import subprocess
app = flask.Flask(__name__)
def test():
print "Test"
@app.route('/yield')
def index():
def inner():
proc = subprocess.Popen(
test(),
shell=True,
stdout=subprocess.PIPE
)
while proc.poll() is None:
yield proc.stdout.readline() + '<br/>
'
return flask.Response(inner(), mimetype='text/html') # text/html is required for most browsers to show the partial page immediately
app.run(debug=True, port=5005)
And it runs but I don't see anything in the browser.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…