我正在尝试更新进度条-我试图在下面的代码周围here处实现代码,但无法正常工作。运行代码时,进度条会出现,但不会更新。
我也检查了其他答案,但仍然不知道如何使它起作用。如果有人可以帮助解决这个问题,我将不胜感激。
server.py
def create_app():
app = Flask(__name__)
app.add_url_rule("/", view_func=views.home_page)
app.add_url_rule("/display", view_func=views.display_page)
app.add_url_rule("/display_progress", methods=['GET', 'POST'],
view_func=views.progress)
return app
views.py
def display_page():
return send_file("templates/display.html")
def progress():
def generate():
x = 0
while x <= 100:
print(x)
x = x + 10
time.sleep(0.2)
yield "data:" + str(x) +"\n\n"
return Response(generate(), mimetype='text/event-stream')
display.html
<html lang="en">
<head>
<title>Display Update</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/static/bootstrap.css"></link>
<script src="/static/jquery.js"></script>
<script src="/static/bootstrap.js"></script>
<script src="/static/websocket.js"></script>
<script>
var source = new EventSource("/display_progress");
source.onmessage = function(event) {
$('.progress-bar').css('width', event.data + '%').attr('aria-
valuenow', event.data);
}
</script>
</head>
<body>
<h3>Update DTS Displays</h3>
<div class="progress" style="width: 750px; height: 22px; margin:
10px;">
<div class="progress-bar progress-bar-striped active"
role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"
style="width: 0%">
<span class="progress-bar-label">0%</span>
</div>
</div>
<button type="submit" class="btn btn-default" formaction="{{
url_for('progress') }}">Progress Bar</button>
</body>
</html>
答案 0 :(得分:1)
在 display.html 中更新脚本标签:
<script>
var source = new EventSource("/display_progress");
source.onmessage = function (event) {
$('.progress-bar').css('width', event.data + '%').attr('aria-valuenow',event.data);
$('.progress-bar-label').text(event.data + '%');
if (event.data == 100) {
source.close()
}
}
</script>