I am trying to get a progress bar to update - I tried to implement the code here here around my code below, but can't get it to work. When I run my code the progress bar appears but does not update.
I have also checked other answers but still can't figure out how to get this to work. If someone could help with the problem I would really appreciate it.
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>