generated from streamlit/gdp-dashboard-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
468 lines (413 loc) · 16.1 KB
/
streamlit_app.py
File metadata and controls
468 lines (413 loc) · 16.1 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
466
467
468
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import requests
from datetime import datetime
# Konfigurasi halaman
st.set_page_config(
page_title="PopuLens - Analisis Demografi Global",
page_icon="👥",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS
st.markdown("""
<style>
.main-header {
font-size: 3rem;
font-weight: bold;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-align: center;
margin-bottom: 1rem;
}
.stat-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 1.5rem;
border-radius: 10px;
color: white;
text-align: center;
}
.country-info {
background: #f8f9fa;
padding: 1rem;
border-radius: 8px;
border-left: 4px solid #667eea;
}
</style>
""", unsafe_allow_html=True)
# Fungsi cache untuk API World Bank
@st.cache_data(ttl=7200)
def get_demographic_data(indicator, start_year=2010, end_year=2022):
"""Fetch data dari World Bank API dengan range tahun"""
url = f"http://api.worldbank.org/v2/country/all/indicator/{indicator}"
params = {
'date': f'{start_year}:{end_year}',
'format': 'json',
'per_page': 20000
}
try:
response = requests.get(url, params=params, timeout=10)
if response.status_code == 200:
data = response.json()
if len(data) > 1 and data[1]:
records = []
for item in data[1]:
if item['value'] is not None and item['countryiso3code']:
records.append({
'country': item['country']['value'],
'code': item['countryiso3code'],
'value': float(item['value']),
'year': int(item['date'])
})
return pd.DataFrame(records)
except Exception as e:
st.error(f"Error: {e}")
return pd.DataFrame()
# Header
st.markdown('<h1 class="main-header">👥 PopuLens</h1>', unsafe_allow_html=True)
st.markdown('<p style="text-align:center; font-size:1.2rem; color:#666;">Platform Analisis Demografi Global - Real-time Data Kelahiran & Kematian</p>', unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.image("https://em-content.zobj.net/source/twitter/376/globe-showing-asia-australia_1f30f.png", width=80)
st.title("🎛️ Panel Kontrol")
# Time range selector
st.subheader("📅 Rentang Waktu")
col1, col2 = st.columns(2)
with col1:
start_year = st.selectbox("Dari", range(2010, 2023), index=0)
with col2:
end_year = st.selectbox("Sampai", range(2010, 2023), index=12)
if start_year > end_year:
st.error("Tahun awal harus lebih kecil dari tahun akhir!")
st.stop()
# Visualization mode
st.subheader("📊 Mode Visualisasi")
viz_mode = st.radio(
"Pilih Mode",
["🗺️ Peta Global", "📈 Tren Waktu", "🔍 Analisis Negara", "⚖️ Komparasi"]
)
# Country selector for specific analysis
if viz_mode in ["📈 Tren Waktu", "🔍 Analisis Negara"]:
country_list = ["Indonesia", "China", "India", "United States", "Brazil",
"Nigeria", "Japan", "Germany", "United Kingdom", "France"]
selected_countries = st.multiselect(
"Pilih Negara",
country_list,
default=["Indonesia", "China", "India"]
)
# Indicator selector
st.subheader("📍 Indikator")
indicator_type = st.selectbox(
"Pilih Data",
["Birth Rate", "Death Rate", "Natural Increase", "Population Growth"]
)
st.divider()
st.info("💡 **Data Source:** World Bank Open Data API")
st.caption("📊 Update terakhir: Setiap 6 jam")
# Load data
with st.spinner("🔄 Mengambil data dari World Bank..."):
birth_df = get_demographic_data('SP.DYN.CBRT.IN', start_year, end_year)
death_df = get_demographic_data('SP.DYN.CDRT.IN', start_year, end_year)
pop_growth_df = get_demographic_data('SP.POP.GROW', start_year, end_year)
if birth_df.empty or death_df.empty:
st.error("❌ Gagal memuat data. Silakan refresh halaman.")
st.stop()
# Merge datasets
df = birth_df.merge(death_df, on=['country', 'code', 'year'], suffixes=('_birth', '_death'))
if not pop_growth_df.empty:
df = df.merge(pop_growth_df[['code', 'year', 'value']], on=['code', 'year'], how='left')
df.rename(columns={'value': 'pop_growth'}, inplace=True)
df['natural_increase'] = df['value_birth'] - df['value_death']
df.columns = df.columns.str.replace('value_birth', 'birth_rate')
df.columns = df.columns.str.replace('value_death', 'death_rate')
# Clean invalid values
df = df.replace([float('inf'), float('-inf')], pd.NA)
df = df.dropna(subset=['birth_rate', 'death_rate'])
# Get latest year data for metrics
latest_year = df['year'].max()
latest_data = df[df['year'] == latest_year]
# Key Metrics
st.subheader("📊 Statistik Global Terbaru ({})".format(latest_year))
col1, col2, col3, col4 = st.columns(4)
with col1:
avg_birth = latest_data['birth_rate'].mean()
st.metric(
"🍼 Avg. Birth Rate",
f"{avg_birth:.2f}‰",
help="Rata-rata kelahiran per 1,000 penduduk"
)
with col2:
avg_death = latest_data['death_rate'].mean()
st.metric(
"⚰️ Avg. Death Rate",
f"{avg_death:.2f}‰",
help="Rata-rata kematian per 1,000 penduduk"
)
with col3:
avg_increase = latest_data['natural_increase'].mean()
delta_color = "normal" if avg_increase > 0 else "inverse"
st.metric(
"📈 Natural Increase",
f"{avg_increase:.2f}‰",
delta=f"{avg_increase:.2f}",
delta_color=delta_color
)
with col4:
total_countries = latest_data['country'].nunique()
st.metric(
"🌍 Total Countries",
f"{total_countries}",
help="Negara dengan data tersedia"
)
st.divider()
# Visualization berdasarkan mode
if viz_mode == "🗺️ Peta Global":
st.subheader(f"🗺️ Peta Global - {latest_year}")
tab1, tab2, tab3 = st.tabs(["🍼 Birth Rate", "⚰️ Death Rate", "📈 Natural Increase"])
with tab1:
fig_birth = px.choropleth(
latest_data,
locations="code",
color="birth_rate",
hover_name="country",
hover_data={'birth_rate': ':.2f', 'code': False},
color_continuous_scale="YlGnBu",
title=f"Birth Rate per 1,000 population ({latest_year})",
labels={'birth_rate': 'Birth Rate (‰)'}
)
fig_birth.update_geos(showcountries=True, countrycolor="lightgray")
fig_birth.update_layout(height=600, margin=dict(l=0, r=0, t=50, b=0))
st.plotly_chart(fig_birth, use_container_width=True)
with tab2:
fig_death = px.choropleth(
latest_data,
locations="code",
color="death_rate",
hover_name="country",
hover_data={'death_rate': ':.2f', 'code': False},
color_continuous_scale="OrRd",
title=f"Death Rate per 1,000 population ({latest_year})",
labels={'death_rate': 'Death Rate (‰)'}
)
fig_death.update_geos(showcountries=True, countrycolor="lightgray")
fig_death.update_layout(height=600, margin=dict(l=0, r=0, t=50, b=0))
st.plotly_chart(fig_death, use_container_width=True)
with tab3:
fig_increase = px.choropleth(
latest_data,
locations="code",
color="natural_increase",
hover_name="country",
hover_data={
'birth_rate': ':.2f',
'death_rate': ':.2f',
'natural_increase': ':.2f',
'code': False
},
color_continuous_scale="RdYlGn",
color_continuous_midpoint=0,
title=f"Natural Increase ({latest_year})",
labels={'natural_increase': 'Natural Increase (‰)'}
)
fig_increase.update_geos(showcountries=True, countrycolor="lightgray")
fig_increase.update_layout(height=600, margin=dict(l=0, r=0, t=50, b=0))
st.plotly_chart(fig_increase, use_container_width=True)
elif viz_mode == "📈 Tren Waktu":
st.subheader("📈 Analisis Tren Historis")
if not selected_countries:
st.warning("⚠️ Pilih minimal 1 negara di sidebar!")
else:
trend_data = df[df['country'].isin(selected_countries)]
# Line chart untuk birth rate
fig_trend = go.Figure()
for country in selected_countries:
country_data = trend_data[trend_data['country'] == country]
fig_trend.add_trace(go.Scatter(
x=country_data['year'],
y=country_data['birth_rate'],
mode='lines+markers',
name=f"{country} (Birth)",
line=dict(width=2)
))
fig_trend.add_trace(go.Scatter(
x=country_data['year'],
y=country_data['death_rate'],
mode='lines+markers',
name=f"{country} (Death)",
line=dict(dash='dash', width=2)
))
fig_trend.update_layout(
title="Birth Rate vs Death Rate Over Time",
xaxis_title="Year",
yaxis_title="Rate per 1,000 population",
hovermode='x unified',
height=500
)
st.plotly_chart(fig_trend, use_container_width=True)
# Natural increase trend
fig_ni = px.line(
trend_data,
x='year',
y='natural_increase',
color='country',
title="Natural Increase Trend",
labels={'natural_increase': 'Natural Increase (‰)', 'year': 'Year'}
)
fig_ni.update_traces(mode='lines+markers')
fig_ni.update_layout(height=400)
st.plotly_chart(fig_ni, use_container_width=True)
elif viz_mode == "🔍 Analisis Negara":
st.subheader("🔍 Analisis Detail per Negara")
if not selected_countries:
st.warning("⚠️ Pilih minimal 1 negara di sidebar!")
else:
for country in selected_countries:
country_data = df[df['country'] == country].sort_values('year')
with st.expander(f"🌍 {country}", expanded=True):
col1, col2, col3 = st.columns(3)
latest_country = country_data[country_data['year'] == latest_year].iloc[0]
with col1:
st.metric("🍼 Birth Rate", f"{latest_country['birth_rate']:.2f}‰")
with col2:
st.metric("⚰️ Death Rate", f"{latest_country['death_rate']:.2f}‰")
with col3:
st.metric("📈 Natural Increase", f"{latest_country['natural_increase']:.2f}‰")
# Mini sparkline
fig_spark = go.Figure()
fig_spark.add_trace(go.Scatter(
x=country_data['year'],
y=country_data['birth_rate'],
fill='tozeroy',
name='Birth',
line=dict(color='#667eea')
))
fig_spark.add_trace(go.Scatter(
x=country_data['year'],
y=country_data['death_rate'],
fill='tozeroy',
name='Death',
line=dict(color='#f093fb')
))
fig_spark.update_layout(
height=250,
margin=dict(l=0, r=0, t=20, b=0),
showlegend=True,
hovermode='x unified'
)
st.plotly_chart(fig_spark, use_container_width=True)
else: # Komparasi
st.subheader("⚖️ Komparasi Negara")
# Top 10 highest birth rate
col1, col2 = st.columns(2)
with col1:
st.markdown("#### 🔝 Top 10 Highest Birth Rate")
top_birth = latest_data.nlargest(10, 'birth_rate')[['country', 'birth_rate']]
fig_top_birth = px.bar(
top_birth,
x='birth_rate',
y='country',
orientation='h',
color='birth_rate',
color_continuous_scale='Greens',
labels={'birth_rate': 'Birth Rate (‰)'}
)
fig_top_birth.update_layout(showlegend=False, height=400)
st.plotly_chart(fig_top_birth, use_container_width=True)
with col2:
st.markdown("#### 🔝 Top 10 Highest Death Rate")
top_death = latest_data.nlargest(10, 'death_rate')[['country', 'death_rate']]
fig_top_death = px.bar(
top_death,
x='death_rate',
y='country',
orientation='h',
color='death_rate',
color_continuous_scale='Reds',
labels={'death_rate': 'Death Rate (‰)'}
)
fig_top_death.update_layout(showlegend=False, height=400)
st.plotly_chart(fig_top_death, use_container_width=True)
# Scatter plot
st.markdown("#### 📊 Birth Rate vs Death Rate Correlation")
# Clean data untuk scatter plot (remove NaN dan infinite values)
scatter_data = latest_data.dropna(subset=['birth_rate', 'death_rate', 'natural_increase'])
scatter_data = scatter_data[
(scatter_data['natural_increase'].notna()) &
(scatter_data['natural_increase'] != float('inf')) &
(scatter_data['natural_increase'] != float('-inf'))
]
# Gunakan absolute value untuk size agar selalu positif
scatter_data['size_value'] = scatter_data['natural_increase'].abs() + 1
fig_scatter = px.scatter(
scatter_data,
x='death_rate',
y='birth_rate',
size='size_value',
color='natural_increase',
hover_name='country',
hover_data={
'death_rate': ':.2f',
'birth_rate': ':.2f',
'natural_increase': ':.2f',
'size_value': False
},
color_continuous_scale='RdYlGn',
color_continuous_midpoint=0,
labels={
'death_rate': 'Death Rate (‰)',
'birth_rate': 'Birth Rate (‰)',
'natural_increase': 'Natural Increase (‰)'
}
)
fig_scatter.update_layout(height=500)
st.plotly_chart(fig_scatter, use_container_width=True)
# Data Table
st.divider()
st.subheader("📋 Data Explorer")
# Filters
col1, col2, col3 = st.columns(3)
with col1:
year_filter = st.selectbox("Filter Tahun", ["All"] + sorted(df['year'].unique().tolist(), reverse=True))
with col2:
sort_column = st.selectbox("Sort By", ['country', 'birth_rate', 'death_rate', 'natural_increase'])
with col3:
sort_order = st.radio("Order", ["Descending", "Ascending"], horizontal=True)
# Apply filters
display_df = df.copy()
if year_filter != "All":
display_df = display_df[display_df['year'] == year_filter]
display_df = display_df.sort_values(
by=sort_column,
ascending=(sort_order == "Ascending")
)
# Show table
st.dataframe(
display_df[['country', 'year', 'birth_rate', 'death_rate', 'natural_increase']].style.format({
'birth_rate': '{:.2f}',
'death_rate': '{:.2f}',
'natural_increase': '{:.2f}'
}).background_gradient(subset=['natural_increase'], cmap='RdYlGn'),
use_container_width=True,
height=400
)
# Download
csv_data = display_df.to_csv(index=False).encode('utf-8')
st.download_button(
label="⬇️ Download Data (CSV)",
data=csv_data,
file_name=f'populens_data_{datetime.now().strftime("%Y%m%d")}.csv',
mime='text/csv'
)
# Footer
st.divider()
st.markdown("""
<div style='text-align: center; color: #666; padding: 2rem;'>
<p><strong>PopuLens</strong> - Global Demographic Analysis Platform</p>
<p>Data Source: World Bank Open Data API | Updates: Every 6 hours</p>
<p>Birth Rate & Death Rate measured per 1,000 population</p>
<p>Natural Increase = Birth Rate - Death Rate</p>
</div>
""", unsafe_allow_html=True)