From a155613005c6d4f5b02e3809a4f683acd1415639 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Wed, 29 Mar 2023 21:39:46 +1100 Subject: [PATCH] Improve users and workouts overview page WIP --- app.py | 38 +++++++++- requirements.txt | 3 +- templates/users.html | 82 +------------------- templates/users_and_workouts.html | 7 +- templates/workouts_list.html | 120 ++++++++++++++++++++++++------ 5 files changed, 141 insertions(+), 109 deletions(-) diff --git a/app.py b/app.py index 0cf03f0..50d2631 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ from sqlalchemy import func -from datetime import timedelta +from datetime import datetime, timedelta import io from flask_sqlalchemy import SQLAlchemy from flask import Flask, make_response, render_template, request, jsonify @@ -8,6 +8,7 @@ from flask_htmx import HTMX import matplotlib.pyplot as plt import matplotlib.dates as mdates import os +import sparklines app = Flask(__name__) @@ -165,7 +166,7 @@ def view_workout(user_id, workout_id): @app.route('/user//workout//delete', methods=['DELETE']) -def delete_workout(user_io, workout_id): +def delete_workout(user_id, workout_id): # Delete the workout and its associated cadence readings CadenceReading.query.filter_by(workout_id=workout_id).delete() db.session.delete(workout) @@ -198,12 +199,41 @@ def render_users_and_workouts(): users_data = [] for user in users: workouts = get_workouts_for_user(user.id) + workout_counts_by_week = [] + duration_by_week = [] + # get start date from last workout + start_date = workouts[-1]['start_time_date'].date() + # get end date from first workout + end_date = workouts[0]['start_time_date'].date() + # calculate number of weeks between start and end date + num_weeks = (end_date - start_date).days // 7 + 1 + + for i in range(num_weeks): + # Calculate the start and end of the current week + start = start_date + timedelta(days=7*i) + end = start + timedelta(days=6) + + # Get the workouts for the current week + weekly_workouts = [w for w in workouts if start <= + w['start_time_date'].date() <= end] + + # Calculate the workout counts and duration for the current week + workout_counts_by_week.append(len(weekly_workouts)) + duration_by_week.append( + int(sum([int(w['duration_minutes']) for w in weekly_workouts]))/len(weekly_workouts)) + + workout_counts_sparkline = sparklines.sparklines( + workout_counts_by_week) + duration_sparkline = sparklines.sparklines(duration_by_week) + user_data = { 'id': user.id, 'name': user.name, 'bike_id': user.bike_id, 'workouts_count': len(workouts), - 'workouts': workouts + 'workouts': workouts, + 'workout_counts_by_week': workout_counts_by_week, + 'duration_by_week': duration_by_week } users_data.append(user_data) return render_template('users_and_workouts.html', users=users_data, bikes=Bike.query.all()) @@ -230,7 +260,9 @@ def get_workouts_for_user(user_id): 'id': workout.id, 'user_id': user_id, 'start_time': format_date_with_ordinal(start_time, '%#H:%M %B %dth %Y'), + 'start_time_date': start_time, 'duration': format_duration(duration), + 'duration_minutes': duration.total_seconds() / 60, 'average_rpm': int(average_rpm), 'calories': int(calories), 'distance': int(distance) diff --git a/requirements.txt b/requirements.txt index 2aa9ea6..65b13d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ flask-htmx==0.2.0 python-dateutil==2.8.2 bidict==0.22.1 Flask-SQLAlchemy==3.0.3 -matplotlib==3.5.2 \ No newline at end of file +matplotlib==3.5.2 +sparklines==0.4.2 \ No newline at end of file diff --git a/templates/users.html b/templates/users.html index dc61e76..fc5e9fd 100644 --- a/templates/users.html +++ b/templates/users.html @@ -1,83 +1,5 @@ - - - - - - - - - - {% for u in users %} - - - - - - {% endfor %} - -
NameBike
- -
- - - - - -
-

{{ u.name }}

-

({{ u.workouts_count }})

-
-
-
-
-
- -
- - - -
-
-
-
-
-
- -
- - - - -
-
- - - - -
-
-
-
- -
+
diff --git a/templates/users_and_workouts.html b/templates/users_and_workouts.html index 8e74913..74aeccc 100644 --- a/templates/users_and_workouts.html +++ b/templates/users_and_workouts.html @@ -8,6 +8,7 @@ + diff --git a/templates/workouts_list.html b/templates/workouts_list.html index 7b00068..e5270f0 100644 --- a/templates/workouts_list.html +++ b/templates/workouts_list.html @@ -1,13 +1,55 @@ -{% for w in workouts %} -
+ +
+
{{ u.name }}
+
+ + + + + +
+
+
+ +
+ +
+ + + +
+
+
+ +
+ +

-
-
- {% with workout=w, graph_types=['speed'] %} - {% include 'workout_view.html' %} - {% endwith %} -
- + +
+
+ {% with workout=w, graph_types=['speed'] %} + {% include 'workout_view.html' %} + {% endwith %} +
+ + +
+
+ {% endfor %} +
- Delete -
-{% endfor %} + +