-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathactivity_page.py
More file actions
177 lines (128 loc) Β· 5.54 KB
/
Copy pathactivity_page.py
File metadata and controls
177 lines (128 loc) Β· 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import streamlit as sl
from data_fetcher import get_user_workouts, insert_user_post,get_user_sensor_data
from modules import display_activity_summary, display_recent_workouts,manual_workout_box
from datetime import datetime
import pandas as pd
from google.cloud import bigquery
#Used GPT 4o to help with share logic
def display_activity_page(user_id="user1"):
# sl.title("πββοΈ Activities Dashboard")
fetcher = lambda: get_user_workouts(user_id)
workouts = fetcher()
# Sort by timestamp descending (most recent first)
sorted_workouts = sorted(
workouts,
key=lambda w: w['start_timestamp'][:w['start_timestamp'].index(' ')],
reverse=True
)
# Let the user choose how many workouts to display
view_mode = sl.radio(
"View Mode:",
["3 Most Recent", "All Workouts"],
key="workout_view_mode",
horizontal=True
)
if view_mode == "3 Most Recent":
recent_workouts = sorted_workouts[:3]
else:
recent_workouts = sorted_workouts
with sl.expander("β Add Workout Manually"):
manual_workout_box()
sl.markdown("---")
sl.caption(
"Showing only the 3 most recent workouts." if view_mode == "3 Most Recent"
else f"Showing all {len(recent_workouts)} workouts."
)
sl.markdown("---")
side_view(user_id, recent_workouts)
total_distance = sl.session_state.get("total_distance", 0)
total_steps = sl.session_state.get("total_steps", 0)
total_calories = sl.session_state.get("total_calories", 0)
handle_share_section(user_id, workouts, recent_workouts)
def side_view(user_id, workouts_list):
left_col, right_col = sl.columns(2)
if not workouts_list:
sl.info("No recent workouts to display.")
return
with left_col:
display_recent_workouts(
user_id,
workouts_func=lambda uid: workouts_list
)
with right_col:
display_activity_summary(workouts_list)
sl.markdown(" ")
sl.markdown("---")
handle_sensor_data(workouts_list, user_id)
def handle_sensor_data(workouts_list, user_id):
client = bigquery.Client()
sl.header("Sensor Data")
for workout in workouts_list:
sl.subheader(f"{workout['workout_id']}")
sensor_data = get_user_sensor_data(
client,
user_id=user_id,
workout_id=workout['workout_id']
)
if sensor_data:
with sl.expander("Sensor Data"):
sl.write("Sensor Data:")
df = pd.DataFrame(sensor_data)
sl.table(df) # or st.dataframe(df)
else:
sl.write("No sensor data found for this workout.")
def handle_share_section(user_id, workouts, recent_workouts):
sl.markdown("---")
sl.subheader("π£ Share with the Community")
data_source = sl.radio("Choose what to share from:", ["My Total Stats", "A Specific Workout"])
if data_source == "My Total Stats":
share_total_stats(user_id)
elif data_source == "A Specific Workout":
share_specific_workout(user_id, recent_workouts)
sl.markdown("---")
sl.markdown("When you press the sharing button, wait a few seconds until the shared notification appears!")
def share_total_stats(user_id):
steps = sl.session_state.get("total_steps", 0)
distance = sl.session_state.get("total_distance", 0)
calories = sl.session_state.get("total_calories", 0)
stat_type = sl.selectbox("Which stat would you like to share?", ["All", "Steps", "Distance", "Calories"])
if sl.button("π€ Share"):
if stat_type == "Steps":
message = f"π I walked {round(steps)} steps this week!"
elif stat_type == "Distance":
message = f"π I ran {round(distance)} miles this week!"
elif stat_type == "Calories":
message = f"π₯ I burned {calories} calories this week!"
elif stat_type == "All":
message = (
f"π This week I walked {steps} steps, "
f"ran {distance} miles, and burned {calories} calories!"
)
insert_user_post(user_id, message)
sl.success("β
Shared!")
def share_specific_workout(user_id,recent_workouts):
if not recent_workouts:
sl.info("No recent workouts to share.")
return
workout_options = {
f"{ w['workout_id']} - ( { w['start_timestamp']} | {w['distance']} mi | {w['calories_burned']} cal )": w
for w in reversed(recent_workouts)
}
selected_label = sl.selectbox("Choose a workout:", list(workout_options.keys()))
selected_workout = workout_options[selected_label]
date = selected_workout['start_timestamp'].split(' ')[0]
stat_type = sl.selectbox("Which stat would you like to share?", ["All", "Steps", "Distance", "Calories"])
if sl.button("π€ Share"):
if stat_type == "Steps":
message = f"π On {date}, I walked {selected_workout['steps']} steps!"
elif stat_type == "Distance":
message = f"π On {date}, I ran {selected_workout['distance']} miles!"
elif stat_type == "Calories":
message = f"π₯ On {date}, I burned {selected_workout['calories_burned']} calories!"
elif stat_type == "All":
message = (
f"ποΈ On {date}, I ran {selected_workout['distance']} miles, "
f"took {selected_workout['steps']} steps, and burned {selected_workout['calories_burned']} calories!"
)
insert_user_post(user_id, message)
sl.success("β
Shared!")