-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
465 lines (382 loc) Β· 17.6 KB
/
app.py
File metadata and controls
465 lines (382 loc) Β· 17.6 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
"""
Main Streamlit application for Afrobarometer Data Explorer.
"""
import streamlit as st
import pandas as pd
from pathlib import Path
# Import our custom modules
from app.utils.preprocessed_data_loader import (
load_preprocessed_data,
get_country_list,
get_variable_info,
filter_data_by_country,
get_sample_data,
get_data_summary
)
from app.utils.codebook_handler import display_codebook_info
from app.components.sidebar import render_sidebar
from app.pages.overview import render_overview_page
from app.pages.visualizations import render_visualizations_page
from app.utils.export import export_to_csv, export_to_excel, export_to_json, generate_summary_report, get_export_filename
from config.settings import STREAMLIT_CONFIG, DATA_FILE
# Configure page
st.set_page_config(**STREAMLIT_CONFIG)
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
font-size: 3rem;
color: #1f77b4;
text-align: center;
margin-bottom: 2rem;
}
.metric-card {
background-color: #f0f2f6;
padding: 1rem;
border-radius: 0.5rem;
border-left: 4px solid #1f77b4;
}
.sidebar .sidebar-content {
background-color: #f8f9fa;
}
.stSelectbox > div > div {
background-color: white;
}
</style>
""", unsafe_allow_html=True)
def main():
"""Main application function."""
# Header
st.markdown('<h1 class="main-header">π Afrobarometer Data Explorer</h1>', unsafe_allow_html=True)
st.markdown("### Interactive Dashboard for Afrobarometer Round 9 (39 Countries)")
# Load preprocessed data
with st.spinner("Loading preprocessed Afrobarometer data..."):
df, metadata = load_preprocessed_data()
if df is None:
st.error("Failed to load preprocessed data. Please run the preprocessing script first.")
st.info("π‘ **To create the preprocessed dataset, run:**")
st.code("python preprocess_data.py", language="bash")
st.stop()
# Render sidebar and get selections
selected_country_groups, selected_countries, selected_variable, variable_type = render_sidebar(df, metadata)
# Filter data by groupings and countries
df_filtered = df.copy()
if selected_country_groups or selected_countries:
from app.utils.preprocessed_data_loader import filter_data_by_groupings_and_countries
df_filtered = filter_data_by_groupings_and_countries(df, selected_country_groups, selected_countries)
# Main content area with tabs
tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs(["π Overview", "π Visualizations", "π Data Explorer", "π Summary Stats", "π Codebook", "πΎ Export Data"])
with tab1:
render_overview_page(df_filtered, metadata)
with tab2:
render_visualizations_page(df_filtered, metadata, selected_variable)
with tab3:
render_data_explorer_page(df_filtered, metadata)
with tab4:
render_summary_stats_page(df_filtered, metadata)
with tab5:
display_codebook_info(metadata)
with tab6:
# Create a label for the export page
if selected_countries:
country_label = f"{len(selected_countries)} selected countries"
elif selected_country_groups:
country_label = f"Countries in {len(selected_country_groups)} grouping(s)"
else:
country_label = "All Countries"
render_export_page(df_filtered, metadata, country_label)
# Footer
st.markdown("---")
st.markdown("**Afrobarometer Data Explorer** | Built with Streamlit | Data: Round 9 (39 Countries)")
def render_data_explorer_page(df: pd.DataFrame, metadata: dict):
"""Render the data explorer page."""
st.header("Interactive Data Explorer")
# Variable comparison
st.subheader("Variable Comparison")
from app.utils.data_loader import get_variable_types
var_types = get_variable_types(df)
# Filter variables to show only Q3 to Q116 (same as sidebar)
filtered_vars = []
for var in var_types['all']:
# Extract the Q number from the variable name (e.g., "Q3. Overall direction of the country (Q3)" -> "Q3")
if 'Q' in var and '(' in var:
# Look for pattern like "Q3)" or "Q116)"
q_part = var.split('(')[-1].replace(')', '').strip()
if q_part.startswith('Q'):
try:
q_num = int(q_part[1:]) # Extract number after Q
if 3 <= q_num <= 116:
filtered_vars.append(var)
except ValueError:
pass
if not filtered_vars:
st.warning("No Q3-Q116 variables found in the dataset.")
return
col1, col2 = st.columns(2)
with col1:
# Variable 1 selection - use same approach as sidebar
selected_var1 = st.selectbox(
"Variable 1:",
options=filtered_vars,
key="data_explorer_var1"
)
with col2:
if selected_var1:
# Variable 2 selection (exclude var1) - use same approach as sidebar
var2_options = [var for var in filtered_vars if var != selected_var1]
selected_var2 = st.selectbox(
"Variable 2:",
options=var2_options,
key="data_explorer_var2"
)
else:
selected_var2 = None
# Create comparison plot
if selected_var1 and selected_var2:
from app.pages.visualizations import render_comparison_plot
render_comparison_plot(df, metadata, selected_var1, selected_var2)
def render_summary_stats_page(df: pd.DataFrame, metadata: dict):
"""Render the summary statistics page."""
st.header("Summary Statistics")
# Overall statistics
st.subheader("Dataset Summary")
col1, col2 = st.columns(2)
with col1:
st.write("**Data Types:**")
dtype_counts = df.dtypes.value_counts()
st.dataframe(dtype_counts.to_frame('Count'), use_container_width=True)
with col2:
st.write("**Missing Values by Variable (Top 10):**")
from app.utils.data_loader import get_missing_data_summary
missing_summary = get_missing_data_summary(df)
st.dataframe(missing_summary.head(10), use_container_width=True)
# Numeric variables summary
numeric_vars = df.select_dtypes(include=['number']).columns.tolist()
if len(numeric_vars) > 0:
st.subheader("Numeric Variables Summary")
numeric_summary = df[numeric_vars].describe()
st.dataframe(numeric_summary, use_container_width=True)
# Categorical variables summary (now with labels)
categorical_vars = df.select_dtypes(include=['object', 'category']).columns.tolist()
if len(categorical_vars) > 0:
st.subheader("Categorical Variables Summary (with Labels)")
cat_summary = []
for var in categorical_vars[:10]: # Limit to first 10
# var is now the labeled column name (e.g., "Country (COUNTRY)")
unique_count = df[var].nunique()
most_common = df[var].mode().iloc[0] if len(df[var].mode()) > 0 else "N/A"
cat_summary.append({
'Variable': var, # Already labeled
'Unique Values': unique_count,
'Most Common': most_common
})
cat_df = pd.DataFrame(cat_summary)
st.dataframe(cat_df, use_container_width=True)
# Show detailed breakdown for a few categorical variables
st.subheader("π Detailed Categorical Breakdown")
# Select a few interesting categorical variables
interesting_vars = []
for var in categorical_vars[:5]:
if df[var].nunique() <= 20: # Only show variables with reasonable number of categories
interesting_vars.append(var)
for var in interesting_vars[:3]: # Show max 3 variables
value_counts = df[var].value_counts().head(10)
with st.expander(f"{var} - Value Distribution"):
st.write("**Top 10 values:**")
for value, count in value_counts.items():
percentage = (count / len(df)) * 100
st.write(f"β’ **{value}**: {count:,} responses ({percentage:.1f}%)")
def render_export_page(df: pd.DataFrame, metadata: dict, country_label: str):
"""Render the enhanced export data page with country and variable selection."""
st.header("Export Data")
# Load the full dataset for selection (not just filtered)
from app.utils.preprocessed_data_loader import load_preprocessed_data
df_full, _ = load_preprocessed_data()
if df_full is None:
st.error("Could not load full dataset for export selection.")
return
st.subheader("π Custom Data Export")
st.write("Select specific countries and variables to create a custom dataset for download.")
# Create two columns for selection
col1, col2 = st.columns(2)
with col1:
st.markdown("### π Country Selection")
# Get all available countries
from app.utils.preprocessed_data_loader import get_country_list
all_countries = get_country_list(df_full)
if all_countries:
selected_countries = st.multiselect(
"Select Countries:",
options=all_countries,
default=all_countries[:5] if len(all_countries) > 5 else all_countries,
key="export_countries",
help="Select one or more countries to include in the export"
)
else:
st.warning("No countries found in the dataset.")
selected_countries = []
with col2:
st.markdown("### π Variable Selection")
# Get all available variables
from app.utils.data_loader import get_variable_types
var_types = get_variable_types(df_full)
all_variables = var_types['all']
# Filter to Q3-Q116 variables (survey questions)
survey_vars = []
for var in all_variables:
if 'Q' in var and '(' in var:
q_part = var.split('(')[-1].replace(')', '').strip()
if q_part.startswith('Q'):
try:
q_num = int(q_part[1:])
if 3 <= q_num <= 116:
survey_vars.append(var)
except ValueError:
pass
# Add demographic and other important variables
demo_vars = [var for var in all_variables if any(demo in var for demo in ['Country', 'GENDER', 'AGE', 'EDUC', 'URBRUR', 'REGION'])]
# Combine and sort variables
available_vars = sorted(survey_vars + demo_vars)
if available_vars:
selected_variables = st.multiselect(
"Select Variables:",
options=available_vars,
default=available_vars[:10] if len(available_vars) > 10 else available_vars,
key="export_variables",
help="Select variables to include in the export. Start typing to search."
)
else:
st.warning("No variables found in the dataset.")
selected_variables = []
# Show selection summary
if selected_countries and selected_variables:
st.markdown("---")
st.subheader("π Export Summary")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Selected Countries", len(selected_countries))
with col2:
st.metric("Selected Variables", len(selected_variables))
# Calculate expected observations
if selected_countries:
country_data = df_full[df_full['Country (COUNTRY)'].isin(selected_countries)]
expected_obs = len(country_data)
with col3:
st.metric("Expected Observations", f"{expected_obs:,}")
# Show selected countries
with st.expander("Selected Countries"):
for country in selected_countries:
st.write(f"β’ {country}")
# Show selected variables
with st.expander("Selected Variables"):
for var in selected_variables:
var_label = metadata.get('var_labels', {}).get(var, var)
st.write(f"β’ {var_label} ({var})")
# Export options
st.markdown("---")
st.subheader("πΎ Download Options")
export_format = st.radio("Export Format:", ["CSV", "Excel", "JSON"], horizontal=True)
# Create custom dataset
if st.button("π Generate Custom Dataset", type="primary"):
with st.spinner("Creating custom dataset..."):
# Filter data by selected countries
if selected_countries:
export_df = df_full[df_full['Country (COUNTRY)'].isin(selected_countries)].copy()
else:
export_df = df_full.copy()
# Select only chosen variables
if selected_variables:
export_df = export_df[selected_variables]
# Create country label for filename
if len(selected_countries) == 1:
country_label = selected_countries[0]
elif len(selected_countries) <= 3:
country_label = f"{len(selected_countries)}_countries"
else:
country_label = f"{len(selected_countries)}_countries"
# Generate filename
filename = get_export_filename("afrobarometer_custom", export_format.lower(), country_label)
# Export based on format
if export_format == "CSV":
csv_data = export_to_csv(export_df)
st.download_button(
label="π₯ Download CSV",
data=csv_data,
file_name=filename,
mime="text/csv",
type="primary"
)
elif export_format == "Excel":
excel_data = export_to_excel(export_df, metadata)
st.download_button(
label="π₯ Download Excel",
data=excel_data,
file_name=filename,
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
type="primary"
)
elif export_format == "JSON":
json_data = export_to_json(export_df)
st.download_button(
label="π₯ Download JSON",
data=json_data,
file_name=filename,
mime="application/json",
type="primary"
)
# Show dataset info
st.success(f"β
Custom dataset created with {len(export_df):,} observations and {len(export_df.columns)} variables!")
# Show preview
with st.expander("π Dataset Preview"):
st.dataframe(export_df.head(10), use_container_width=True)
elif not selected_countries:
st.warning("β οΈ Please select at least one country to export.")
elif not selected_variables:
st.warning("β οΈ Please select at least one variable to export.")
# Legacy export section (for current filtered data)
st.markdown("---")
st.subheader("π₯ Quick Export (Current Filtered Data)")
st.write("Export the data currently filtered by the sidebar selections.")
export_format_quick = st.radio("Export Format:", ["CSV", "Excel", "JSON"], key="quick_export", horizontal=True)
if st.button("Generate Quick Download Link", key="quick_export_btn"):
if export_format_quick == "CSV":
csv_data = export_to_csv(df)
filename = get_export_filename("afrobarometer_filtered", "csv", country_label)
st.download_button(
label="Download CSV",
data=csv_data,
file_name=filename,
mime="text/csv"
)
elif export_format_quick == "Excel":
excel_data = export_to_excel(df, metadata)
filename = get_export_filename("afrobarometer_filtered", "xlsx", country_label)
st.download_button(
label="Download Excel",
data=excel_data,
file_name=filename,
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
elif export_format_quick == "JSON":
json_data = export_to_json(df)
filename = get_export_filename("afrobarometer_filtered", "json", country_label)
st.download_button(
label="Download JSON",
data=json_data,
file_name=filename,
mime="application/json"
)
# Export summary report
st.markdown("---")
st.subheader("π Export Summary Report")
if st.button("Generate Summary Report", key="summary_report_btn"):
report = generate_summary_report(df, metadata, country_label)
filename = get_export_filename("afrobarometer_summary", "md", country_label)
st.download_button(
label="Download Summary Report",
data=report,
file_name=filename,
mime="text/markdown"
)
if __name__ == "__main__":
main()