WIP: When selecting an exercise on new workout view, render a graph of exercise progress for the active user

This commit is contained in:
Peter Stockings
2023-12-07 20:34:26 +11:00
parent 5bf31d0cb9
commit 469054048e
5 changed files with 120 additions and 9 deletions

47
app.py
View File

@@ -424,14 +424,45 @@ def get_most_recent_topset_for_exercise(person_id, workout_id):
return render_template('partials/new_set_form.html', person_id=person_id, workout_id=workout_id, exercises=exercises, has_value=True, exercise_id=exercise_id, repetitions=repetitions, weight=weight)
# # TODO: Remove me, just for testing
# @ app.route("/sparkline", methods=['GET'])
# def get_sparkline():
# width = request.args.get('width', 400, type=int)
# height = request.args.get('height', 200, type=int)
# number_of_points = request.args.get('number_of_points', 50, type=int)
# points = [random.randint(1, 100) for _ in range(number_of_points)]
# return render_template('partials/sparkline.html', width=width, height=height, points=points)
def calculate_relative_positions(start_dates):
min_date = min(start_dates)
max_date = max(start_dates)
total_span = (max_date - min_date).days if max_date != min_date else 1
return [(date - min_date).days / total_span for date in start_dates]
@ app.route("/person/<int:person_id>/exercise/<int:exercise_id>/sparkline", methods=['GET'])
def get_exercise_progress_for_user(person_id, exercise_id):
width = request.args.get('width', 300, type=int)
height = request.args.get('height', 100, type=int)
(estimated_1rm, start_dates) = db.get_exercise_progress_for_user(person_id, exercise_id)
# Calculate vb_width
min_date = min(start_dates)
max_date = max(start_dates)
date_range = (max_date - min_date).days # e.g., 30 days
vb_width = date_range # This can be scaled if needed
# Calculate vb_height
min_value = min(estimated_1rm)
max_value = max(estimated_1rm)
value_range = max_value - min_value # e.g., 100
vb_height = value_range # This can be scaled if needed
# Scaling factors (optional, for design)
width_scaling_factor = 200 / vb_width # e.g., if you want 200px width
height_scaling_factor = 75 / vb_height # e.g., if you want 100px height
# Apply scaling
vb_width *= width_scaling_factor
vb_height *= height_scaling_factor
# Scale estimated_1rm between 0 and vb_height
estimated_1rm = [((value - min_value) / value_range) * vb_height for value in estimated_1rm]
relative_positions = calculate_relative_positions(start_dates)
data_points = list(zip(estimated_1rm, relative_positions))
return render_template('partials/sparkline.html', title="GHR", vb_width=vb_width, vb_height=vb_height, data_points=data_points)
@app.teardown_appcontext