You can make a view in Flask, then GET
/ POST
to it from the frontend to retrieve or change the progress value. Here's a minimal example.
The first route is the root page which renders a template showing the progress bar itself and also a way to update the value. [Of course you could make the edit part a separate page, do a POST programmatically from JavaScript, etc.]
The second route is the /progress
endpoint which has a view that can either retrieve or set values for progress and max. This view then stores the value in Flask's Sessions storage which uses a cookie on the client side.
app.py
:
from flask import Flask, render_template, request, session
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/' # (use random bytes)
def get_progress():
DEFAULT_PROGRESS_VALUE = 0
DEFAULT_PROGRESS_MAX = 100
return {
"value": session.get("progress_value", DEFAULT_PROGRESS_VALUE),
"max": session.get("progress_max", DEFAULT_PROGRESS_MAX),
}
def set_progress_value(val):
if val is not None:
session["progress_value"] = int(val)
def set_progress_max(val):
if val is not None:
session["progress_max"] = int(val)
@app.route("/")
def root():
return render_template("index.html", **get_progress())
@app.route("/progress", methods=["GET", "POST"])
def progress():
if request.method == "POST":
set_progress_value(request.form.get("value"))
set_progress_max(request.form.get("max"))
return get_progress()
templates/index.html
:
<!doctype html>
<html>
<body>
<label for="result_bar">Progress:</label>
<progress id="result_bar" max="{{ max }}" value="{{ value }}"></progress>
<hr>
<p>Update progress:</p>
<form action="/progress" method="post">
<input type="number" name="value" value="{{ value }}">
<input type="number" name="max" value="{{ max }}">
<input type="submit" value="Update">
</form>
</body>
</html>
To run it:
flask run
You can then change the progress values using the form and reload /
to see the change.
For more info, the Flask Quickstart is a good reference on the components used in this example.