-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
161 lines (136 loc) · 5.25 KB
/
app.py
File metadata and controls
161 lines (136 loc) · 5.25 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
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import base64
from fpdf import FPDF
from scripts.data_loader import load_running_data
from scripts.visualization import (
plot_fastest_pace_per_shoe,
plot_monthly_trends,
plot_shoes_usage,
plot_elevation_gain,
plot_monthly_distance
)
from io import BytesIO
import tempfile
df = None
st.title("🏃🏻♀️ Running Data Dashboard")
# Load data from MongoDB
try:
df = load_running_data()
if df is None or df.empty:
st.warning("No running data found for 2024.")
else:
# Remove "Unknown"
df = df[df['shoes'] != 'Unknown']
# Sidebar selection
shoes_list = ["All"] + list(df['shoes'].unique())
selected_shoe = st.sidebar.selectbox("Select Shoes", shoes_list, index=0)
# Filter
if selected_shoe != "All":
df = df[df['shoes'] == selected_shoe]
st.write(f"✅ Filter applied: {selected_shoe}")
else:
st.write("✅ Showing all shoes")
# Plot common graphs
st.subheader("📊 Monthly Trends")
monthly_trends_fig = plot_monthly_trends(df)
st.pyplot(monthly_trends_fig)
st.subheader("⛰️ Monthly Elevation Gain")
elevation_gain_fig = plot_elevation_gain(df)
st.pyplot(elevation_gain_fig)
st.subheader("📍 Monthly Distance")
monthly_distance_fig = plot_monthly_distance(df)
st.pyplot(monthly_distance_fig)
st.subheader("🚀 Fastest Pace per Shoe")
fastest_pace_fig = plot_fastest_pace_per_shoe(df)
st.pyplot(fastest_pace_fig)
# Only show shoes usage for ALL
shoes_usage_fig = None
if selected_shoe == "All":
st.subheader("👟 Shoes Usage")
shoes_usage_fig = plot_shoes_usage(df)
st.pyplot(shoes_usage_fig)
except Exception as e:
st.error(f"An error occurred: {e}")
# Generate CSV for download
if df is not None and not df.empty:
csv = df.to_csv(index=False)
b64_csv = base64.b64encode(csv.encode()).decode()
href_csv = f'data:text/csv;base64,{b64_csv}'
# Generate PDF for download
pdf = FPDF()
def save_plot_to_bytes(fig):
"""Save a Matplotlib figure to a BytesIO object"""
img_stream = BytesIO()
fig.savefig(img_stream, format='png')
img_stream.seek(0)
return img_stream
def save_image_to_tempfile(image_stream):
"""Save image from BytesIO stream to a temporary file."""
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
temp_file.write(image_stream.read())
temp_file.close()
return temp_file.name
# Convert figures to images
monthly_trends_img = save_plot_to_bytes(monthly_trends_fig)
elevation_gain_img = save_plot_to_bytes(elevation_gain_fig)
monthly_distance_img = save_plot_to_bytes(monthly_distance_fig)
fastest_pace_img = save_plot_to_bytes(fastest_pace_fig)
monthly_trends_temp = save_image_to_tempfile(monthly_trends_img)
elevation_gain_temp = save_image_to_tempfile(elevation_gain_img)
monthly_distance_temp = save_image_to_tempfile(monthly_distance_img)
fastest_pace_temp = save_image_to_tempfile(fastest_pace_img)
# Only save shoes usage if it exists
shoes_usage_temp = None
if shoes_usage_fig:
shoes_usage_img = save_plot_to_bytes(shoes_usage_fig)
shoes_usage_temp = save_image_to_tempfile(shoes_usage_img)
# New page function
def add_image_to_new_page(pdf, image_path):
"""Add image to a new page in the PDF."""
pdf.add_page()
pdf.image(image_path, x=10, y=30, w=180)
# Add images to the PDF
add_image_to_new_page(pdf, monthly_trends_temp)
add_image_to_new_page(pdf, elevation_gain_temp)
add_image_to_new_page(pdf, monthly_distance_temp)
add_image_to_new_page(pdf, fastest_pace_temp)
if shoes_usage_temp:
add_image_to_new_page(pdf, shoes_usage_temp)
# Create PDF
pdf_output = pdf.output(dest='S').encode('latin1')
pdf_output_io = BytesIO(pdf_output)
pdf_output_io.seek(0)
# Convert PDF to base64
b64_pdf = base64.b64encode(pdf_output_io.read()).decode()
href_pdf = f"data:application/pdf;base64,{b64_pdf}"
# CSS for download buttons
st.markdown(f"""
<style>
.download-button-container {{
display: flex;
justify-content: center;
gap: 20px;
margin-top: 50px;
}}
.download-button {{
background-color: #FFD09B;
color: black;
border: none;
padding: 15px 25px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
text-decoration: none;
display: inline-block;
}}
.download-button:hover {{
background-color: #FFB0B0;
}}
</style>
<div class="download-button-container">
<a href="{href_csv}" download="running_data.csv" class="download-button">Download Running Data as CSV</a>
<a href="{href_pdf}" download="running_data.pdf" class="download-button">Download Charts as PDF</a>
</div>
""", unsafe_allow_html=True)