forked from ondata/dbc-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
379 lines (320 loc) · 14.2 KB
/
Copy pathapp.py
File metadata and controls
379 lines (320 loc) · 14.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import os
from datetime import datetime
# Plotly config to disable all zoom interactions (better for mobile)
PLOTLY_CONFIG = {
'scrollZoom': False,
'displayModeBar': True,
'displaylogo': False,
'modeBarButtonsToRemove': ['zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d']
}
# 1. PAGE CONFIGURATION
st.set_page_config(layout="wide", page_title="Italy Bluefin Tuna Landings - Enhanced", page_icon="🐟")
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
font-size: 3.5rem;
font-weight: bold;
color: #0066cc;
text-align: center;
margin-bottom: 0.2rem;
margin-top: 1rem;
}
.sub-header {
text-align: center;
color: #666;
font-size: 1.3rem;
margin-bottom: 2rem;
}
.metric-container {
background-color: #f0f2f6;
padding: 1rem;
border-radius: 0.5rem;
text-align: center;
}
</style>
""", unsafe_allow_html=True)
# Data path based on your repository structure
FILE_PATH = "data/pescaTonnoRosso/pescaTonnoRosso.csv"
# 2. DATA LOADING
@st.cache_data
def load_data(path):
if not os.path.exists(path):
st.error(f"Data file not found at: {path}")
st.info("💡 Tip: Make sure the data file is in the correct directory structure.")
st.stop()
with st.spinner("Loading data..."):
df = pd.read_csv(path)
df['data_cattura'] = pd.to_datetime(df['data_cattura'])
df['year'] = df['data_cattura'].dt.year
df['month'] = df['data_cattura'].dt.month
df['month_name'] = df['data_cattura'].dt.strftime('%B')
df['year_month'] = df['data_cattura'].dt.to_period('M').astype(str)
return df
# 3. DATA PROCESSING
try:
df = load_data(FILE_PATH)
# Sidebar filters
st.sidebar.title("🎛️ Filters")
years_available = sorted(df['year'].unique().tolist())
regions_available = sorted(df['regione'].unique().tolist())
selected_years = st.sidebar.multiselect(
"Select Years",
years_available,
default=years_available,
help="Filter data by year"
)
selected_regions = st.sidebar.multiselect(
"Select Regions",
regions_available,
default=regions_available,
help="Filter data by region"
)
# Apply filters
if not selected_years or not selected_regions:
st.warning("⚠️ Please select at least one year and one region to view the dashboard.")
st.stop()
df_filtered = df[df['year'].isin(selected_years) & df['regione'].isin(selected_regions)].copy()
if len(df_filtered) == 0:
st.warning("⚠️ No data available for the selected filters.")
st.stop()
# Calculate statistics
# FAO zone common names mapping
fao_zone_names = {
'37.1.3': 'Ligurian Sea & Tyrrhenian Sea',
'37.2.1': 'Adriatic Sea',
'37.2.2': 'Ionian Sea'
}
df_filtered['fao_common'] = df_filtered['zona_FAO'].map(fao_zone_names).fillna(df_filtered['zona_FAO'])
# All-time stats per region
reg_overall = df_filtered.groupby('regione')['peso_kg'].sum().reset_index()
total_all_time = reg_overall['peso_kg'].sum()
reg_overall['share_pct'] = (reg_overall['peso_kg'] / total_all_time) * 100
reg_overall = reg_overall.sort_values('peso_kg', ascending=False)
region_rank = reg_overall['regione'].tolist()
# Yearly stats per region
annual_totals = df_filtered.groupby('year')['peso_kg'].sum().reset_index(name='total_kg')
reg_annual = df_filtered.groupby(['year', 'regione']).agg(
weight=('peso_kg', 'sum'),
count=('peso_kg', 'count'),
avg_weight=('peso_kg', 'mean')
).reset_index()
# Merged stats for yearly shares
stats = reg_annual.merge(annual_totals, on='year')
stats['share_pct'] = (stats['weight'] / stats['total_kg']) * 100
stats['year_str'] = stats['year'].astype(str)
years_sorted = sorted(df_filtered['year'].unique().tolist())
years_sorted_str = [str(y) for y in years_sorted]
# Calculate YoY growth
annual_totals_sorted = annual_totals.sort_values('year')
annual_totals_sorted['yoy_growth'] = annual_totals_sorted['total_kg'].pct_change() * 100
# 4. DASHBOARD UI
st.markdown("<h1 style='text-align: center;'>Italy Bluefin Tuna Recreational Landings Dashboard</h1>", unsafe_allow_html=True)
st.markdown('<p class="sub-header">Comprehensive Analysis of Recreational Fishing Data</p>', unsafe_allow_html=True)
st.markdown("---")
# KPIs
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Weight", f"{total_all_time:,.0f} kg")
with col2:
st.metric("Total Landings", f"{len(df_filtered):,}")
with col3:
st.metric("Avg Weight/Specimen", f"{df_filtered['peso_kg'].mean():.1f} kg")
with col4:
st.metric("Active Regions", len(region_rank))
# INTERACTIVE TIPS
with st.expander("💡 Pro Tips for Chart Interaction"):
st.markdown("""
* **Single Click on Legend:** Hides/shows a specific year or region.
* **Double Click on Legend:** Isolates that specific year or region.
* **Click and Drag:** Zooms into an area. Double-click chart to reset.
* **Modebar (Top Right):** Hover to find icons for pan, zoom, and download as PNG.
* **Filters (Left Sidebar):** Use year and region filters to focus your analysis.
""")
st.markdown("---")
# === TABS FOR BETTER ORGANIZATION ===
tab1, tab2 = st.tabs([
"📊 Overview",
"📋 Data Explorer"
])
# ========== TAB 1: OVERVIEW ==========
with tab1:
st.subheader("Regional Performance Analysis")
col1, col2 = st.columns(2)
with col1:
metric_choice = st.radio(
"Select metric for chart:",
["Total Weight (kg)", "Number of Landings"],
horizontal=True
)
with col2:
chart_type = st.radio(
"Chart type:",
["Grouped", "Stacked"],
horizontal=True
)
y_col = 'weight' if metric_choice == "Total Weight (kg)" else 'count'
y_label = "Weight (kg)" if metric_choice == "Total Weight (kg)" else "Number of Landings"
barmode = "group" if chart_type == "Grouped" else "stack"
fig_perf = px.bar(
stats, x="regione", y=y_col, color="year_str", barmode=barmode,
category_orders={"regione": region_rank, "year_str": years_sorted_str},
labels={y_col: y_label, "regione": "Region", "year_str": "Year"},
title=f"{metric_choice} by Region and Year"
)
fig_perf.update_layout(height=500, dragmode=False)
st.plotly_chart(fig_perf, use_container_width=True, config=PLOTLY_CONFIG)
# Average Weight Chart
st.subheader("Average Landing Weight (kg) per Region")
fig_avg = px.bar(
stats, x="regione", y="avg_weight", color="year_str", barmode="group",
category_orders={"regione": region_rank, "year_str": years_sorted_str},
labels={"avg_weight": "Avg Weight (kg)", "regione": "Region", "year_str": "Year"}
)
nat_avg = df_filtered['peso_kg'].mean()
fig_avg.add_hline(
y=nat_avg, line_dash="dash", line_color="red",
annotation_text=f"Overall Avg: {nat_avg:.1f}kg"
)
fig_avg.update_layout(height=500, dragmode=False)
st.plotly_chart(fig_avg, use_container_width=True, config=PLOTLY_CONFIG)
# Distribution box plot
st.subheader("Weight Distribution by Region")
fig_box = px.box(
df_filtered, x="regione", y="peso_kg",
category_orders={"regione": region_rank},
labels={"peso_kg": "Weight (kg)", "regione": "Region"},
color="regione"
)
fig_box.update_layout(height=500, showlegend=False, dragmode=False)
st.plotly_chart(fig_box, use_container_width=True, config=PLOTLY_CONFIG)
# Pie chart
st.markdown("### Regional Distribution")
col1, col2 = st.columns(2)
with col1:
st.subheader("Regional Share (by Weight)")
# Add percentage column for proper display
reg_overall_display = reg_overall.copy()
reg_overall_display['percentage_text'] = reg_overall_display['share_pct'].round(1).astype(str) + '%'
# Create pie chart with go.Pie for better control
fig_pie = go.Figure(data=[go.Pie(
labels=reg_overall_display['regione'],
values=reg_overall_display['peso_kg'],
hole=0.4,
texttemplate='%{label}<br>%{text}',
text=reg_overall_display['percentage_text'],
textposition='inside',
hovertemplate='<b>%{label}</b><br>Weight: %{value:,.0f} kg<br>Percentage: %{text}<extra></extra>',
marker=dict(colors=px.colors.qualitative.Set3)
)])
fig_pie.update_layout(
height=500,
showlegend=True,
dragmode=False
)
st.plotly_chart(fig_pie, use_container_width=True, config=PLOTLY_CONFIG)
with col2:
st.subheader("Regional Share (by Count)")
reg_count = df_filtered.groupby('regione').size().reset_index(name='count')
total_count_val = reg_count['count'].sum()
reg_count['percentage_text'] = ((reg_count['count'] / total_count_val) * 100).round(1).astype(str) + '%'
fig_pie_count = go.Figure(data=[go.Pie(
labels=reg_count['regione'],
values=reg_count['count'],
hole=0.4,
texttemplate='%{label}<br>%{text}',
text=reg_count['percentage_text'],
textposition='inside',
hovertemplate='<b>%{label}</b><br>Count: %{value:,}<br>Percentage: %{text}<extra></extra>',
marker=dict(colors=px.colors.qualitative.Pastel)
)])
fig_pie_count.update_layout(
height=500,
showlegend=True,
dragmode=False
)
st.plotly_chart(fig_pie_count, use_container_width=True, config=PLOTLY_CONFIG)
# Yearly contribution share
st.markdown("### Yearly Contribution to National Total")
st.markdown("This chart shows how the 100% of each year's landings were split among regions.")
fig_share = px.bar(
stats, x="year", y="share_pct", color="regione", barmode="stack",
category_orders={"regione": region_rank},
labels={"share_pct": "Share of Yearly Total (%)", "year": "Year", "regione": "Region"}
)
fig_share.update_xaxes(type='category')
fig_share.update_layout(yaxis_range=[0, 100], height=500, dragmode=False)
st.plotly_chart(fig_share, use_container_width=True, config=PLOTLY_CONFIG)
# ========== TAB 2: DATA EXPLORER ==========
with tab2:
st.subheader("Data Explorer & Download")
# Summary statistics
st.markdown("### Summary Statistics")
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("**Weight Statistics**")
st.write(df_filtered['peso_kg'].describe().round(2))
with col2:
st.markdown("**Regional Breakdown**")
regional_counts = df_filtered['regione'].value_counts()
st.write(regional_counts)
with col3:
st.markdown("**Yearly Breakdown**")
yearly_summary = df_filtered.groupby('year').agg(
count=('peso_kg', 'count'),
total_kg=('peso_kg', 'sum')
).sort_index()
yearly_summary['total_kg'] = yearly_summary['total_kg'].apply(lambda x: f"{x:,.0f} kg")
st.write(yearly_summary)
# Top catches
st.markdown("### 🏆 Top 10 Largest Catches")
top_catches = df_filtered.nlargest(10, 'peso_kg')[
['data_cattura', 'peso_kg', 'regione', 'zona_FAO', 'identificativo_natante']
].reset_index(drop=True)
top_catches.index += 1
st.dataframe(top_catches, use_container_width=True)
# Raw data viewer
st.markdown("### 📋 Raw Data Viewer")
show_all = st.checkbox("Show all data (may be slow for large datasets)")
if show_all:
st.dataframe(df_filtered, use_container_width=True, height=400)
else:
st.dataframe(df_filtered.head(100), use_container_width=True, height=400)
st.caption(f"Showing first 100 rows of {len(df_filtered)} total records. Check the box above to see all.")
# Download section
st.markdown("### 💾 Download Data")
col1, col2 = st.columns(2)
with col1:
csv = df_filtered.to_csv(index=False).encode('utf-8')
st.download_button(
label="📥 Download Filtered Data (CSV)",
data=csv,
file_name=f"tuna_landings_{datetime.now().strftime('%Y%m%d')}.csv",
mime="text/csv"
)
with col2:
summary_csv = reg_overall.to_csv(index=False).encode('utf-8')
st.download_button(
label="📥 Download Regional Summary (CSV)",
data=summary_csv,
file_name=f"regional_summary_{datetime.now().strftime('%Y%m%d')}.csv",
mime="text/csv"
)
# FOOTER
st.markdown("---")
st.markdown(
"""
<div style='text-align: center; color: grey;'>
<b>Enhanced Dashboard</b> | Developed by: <b>Matteo Mannini</b> | Enhanced with ❤️ by Claude
</div>
""",
unsafe_allow_html=True
)
except Exception as e:
st.error(f"❌ Error encountered: {e}")
st.exception(e)