-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (58 loc) · 2.55 KB
/
app.py
File metadata and controls
79 lines (58 loc) · 2.55 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
import streamlit as st
import pandas as pd
from utils import preprocess_data
from tabs import general, running, swimming, cycling, time_weather
st.set_page_config(page_title="Strava Triathlon Dashboard", layout="wide", initial_sidebar_state="expanded")
from auth_strava import authenticate
import requests
with st.sidebar:
data_source = st.radio("Choose data source", ["🔗 Strava API", "📂 Upload CSV", "📊 Example Data"])
if data_source == "📂 Upload CSV":
uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
if uploaded_file:
df = pd.read_csv(uploaded_file, encoding='latin-1', on_bad_lines='skip')
else:
st.stop()
elif data_source == "📊 Example Data":
df = pd.read_csv("activities.csv", encoding='latin-1', on_bad_lines='skip')
else: # Strava API
access_token = authenticate()
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get(
"https://www.strava.com/api/v3/athlete/activities?per_page=200",
headers=headers
)
activities = response.json()
if not isinstance(activities, list) or len(activities) == 0:
st.error("Failed to fetch activities from Strava API.")
st.stop()
df = pd.DataFrame(activities)
df = preprocess_data(df)
# Date filter
if 'Activity Date' in df.columns:
df['Activity Date'] = pd.to_datetime(df['Activity Date'], errors='coerce')
df = df.dropna(subset=['Activity Date'])
min_date = df['Activity Date'].min().date()
max_date = df['Activity Date'].max().date()
default_start = max(pd.to_datetime("2020-01-01").date(), min_date)
default_end = max_date
start_date = st.date_input("Start date", min_value=min_date, max_value=max_date, value=default_start)
end_date = st.date_input("End date", min_value=min_date, max_value=max_date, value=default_end)
if start_date > end_date:
st.warning("Start date must be before end date")
st.stop()
mask = (df['Activity Date'].dt.date >= start_date) & (df['Activity Date'].dt.date <= end_date)
df = df.loc[mask]
# Tab navigation
tabs = {
"🏃🚴🏊 General": general,
"🏃 Running": running,
"🏊 Swimming": swimming,
"🚴 Cycling": cycling,
"🕒⛅ Time & Weather": time_weather,
}
tab_labels = list(tabs.keys())
tab_objects = st.tabs(tab_labels)
for tab_obj, tab_label in zip(tab_objects, tab_labels):
with tab_obj:
tabs[tab_label].render(df)