Tôi muốn có thể truy cập một trang web và nó sẽ chạy một hàm python và hiển thị tiến trình trong trang web.Làm thế nào để liên tục hiển thị đầu ra Python trong một trang web?
Vì vậy, khi bạn truy cập trang web, bạn có thể thấy đầu ra của tập lệnh như thể bạn đã chạy nó từ dòng lệnh.
Dựa trên câu trả lời ở đây
How to continuously display python output in a webpage?
Tôi cố gắng để hiển thị đầu ra từ PYTHON
Tôi cố gắng để sử dụng mã Markus Unterwaditzer với một chức năng python.
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/>\n'
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)
Và nó chạy nhưng tôi không thấy gì trong trình duyệt.
Rất tiếc tôi đã thay đổi máy chủ để nó hoạt động trong môi trường âm hộ của tôi. –
OMG, tôi đã thử rất nhiều thứ để làm việc này và đây là điều duy nhất đã làm! – JeffThompson
Làm cách nào để chuyển Phản hồi cho một điểm cụ thể trong mẫu? – JeffThompson