Show formatted name for attribute label on graphs
This commit is contained in:
27
app.py
27
app.py
@@ -216,6 +216,11 @@ def create_workout(user_id):
|
||||
|
||||
@app.route('/user/<int:user_id>/workouts/graph', methods=['GET'])
|
||||
def graph_user_workouts(user_id):
|
||||
attributes = request.args.getlist(
|
||||
'attributes', type=str)
|
||||
if not attributes:
|
||||
return ""
|
||||
|
||||
if htmx:
|
||||
return f"""
|
||||
<img src="{request.full_path}"
|
||||
@@ -234,17 +239,9 @@ def graph_user_workouts(user_id):
|
||||
end_date = request.args.get(
|
||||
'end_date', default=most_recent_workout_date, type=toDate)
|
||||
|
||||
attributes = request.args.getlist(
|
||||
'attributes', type=str)
|
||||
if not attributes:
|
||||
return ""
|
||||
|
||||
period = request.args.get('period', default='day', type=str)
|
||||
|
||||
user_data = generate_user_data(user)
|
||||
title = f'{format_key_values(user_data["attributes"], attributes)} over {get_value_from_key(user_data["periods"], period)} ({start_date.strftime("%d/%m/%y")} - {end_date.strftime("%d/%m/%y")})'
|
||||
|
||||
return plot_averaged_attributes(workouts, start_date, end_date, period, attributes, title)
|
||||
return plot_averaged_attributes(workouts, user, start_date, end_date, period, attributes)
|
||||
|
||||
|
||||
def daterange(start_date, end_date, delta=timedelta(days=1)):
|
||||
@@ -345,7 +342,7 @@ def create_user_graph(x_values, y_data, filename, x_label='Time', title=None):
|
||||
return response
|
||||
|
||||
|
||||
def plot_averaged_attributes(workouts_list, start_date, end_date, period, attributes, title):
|
||||
def plot_averaged_attributes(workouts_list, user, start_date, end_date, period, attributes):
|
||||
"""Creates a graph for averaged attributes over a period.
|
||||
|
||||
Parameters:
|
||||
@@ -356,6 +353,9 @@ def plot_averaged_attributes(workouts_list, start_date, end_date, period, attrib
|
||||
Returns:
|
||||
- Flask Response object containing the image of the graph.
|
||||
"""
|
||||
user_data = generate_user_data(user)
|
||||
title = f'{format_key_values(user_data["attributes"], attributes)} over {get_value_from_key(user_data["periods"], period)} ({start_date.strftime("%d/%m/%y")} - {end_date.strftime("%d/%m/%y")})'
|
||||
|
||||
# Fetching the data
|
||||
averaged_attributes = average_workout_attributes_per_period(
|
||||
workouts_list, start_date, end_date, period, attributes)
|
||||
@@ -365,8 +365,8 @@ def plot_averaged_attributes(workouts_list, start_date, end_date, period, attrib
|
||||
|
||||
y_data = {}
|
||||
for attribute in attributes:
|
||||
y_data[attribute] = [averaged_attributes[date][attribute]
|
||||
for date in x_values]
|
||||
y_data[get_value_from_key(user_data["attributes"], attribute)] = [averaged_attributes[date][attribute]
|
||||
for date in x_values]
|
||||
|
||||
# Creating the graph
|
||||
return create_user_graph(x_values, y_data, filename=f"average_attributes_over_{period}", title=title)
|
||||
@@ -632,7 +632,8 @@ def generate_calendar_monthly_view(workouts, selected_date):
|
||||
start_date, end_date = get_month_bounds(selected_date)
|
||||
|
||||
# Build a lookup dictionary for faster access
|
||||
workout_lookup = {w['start_time_date']: w for w in workouts if start_date <= w['start_time_date'] <= end_date}
|
||||
workout_lookup = {w['start_time_date']
|
||||
: w for w in workouts if start_date <= w['start_time_date'] <= end_date}
|
||||
|
||||
current_date = datetime.now().date()
|
||||
days_of_month = [
|
||||
|
||||
Reference in New Issue
Block a user