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