generated from streamlit/gdp-dashboard-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
151 lines (117 loc) · 3.68 KB
/
streamlit_app.py
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
import streamlit as st
import pandas as pd
import math
from pathlib import Path
# Set the title and favicon that appear in the Browser's tab bar.
st.set_page_config(
page_title='GDP dashboard - testing GH edit',
page_icon=':earth_americas:', # This is an emoji shortcode. Could be a URL too.
)
# -----------------------------------------------------------------------------
# Declare some useful functions.
@st.cache_data
def get_gdp_data():
"""Grab GDP data from a CSV file.
This uses caching to avoid having to read the file every time. If we were
reading from an HTTP endpoint instead of a file, it's a good idea to set
a maximum age to the cache with the TTL argument: @st.cache_data(ttl='1d')
"""
# Instead of a CSV on disk, you could read from an HTTP endpoint here too.
DATA_FILENAME = Path(__file__).parent/'data/gdp_data.csv'
raw_gdp_df = pd.read_csv(DATA_FILENAME)
MIN_YEAR = 1960
MAX_YEAR = 2022
# The data above has columns like:
# - Country Name
# - Country Code
# - [Stuff I don't care about]
# - GDP for 1960
# - GDP for 1961
# - GDP for 1962
# - ...
# - GDP for 2022
#
# ...but I want this instead:
# - Country Name
# - Country Code
# - Year
# - GDP
#
# So let's pivot all those year-columns into two: Year and GDP
gdp_df = raw_gdp_df.melt(
['Country Code'],
[str(x) for x in range(MIN_YEAR, MAX_YEAR + 1)],
'Year',
'GDP',
)
# Convert years from string to integers
gdp_df['Year'] = pd.to_numeric(gdp_df['Year'])
return gdp_df
gdp_df = get_gdp_data()
# -----------------------------------------------------------------------------
# Draw the actual page
# Set the title that appears at the top of the page.
'''
# :earth_americas: GDP dashboard
Browse GDP data from the [World Bank Open Data](https://data.worldbank.org/) website. As you'll
notice, the data only goes to 2022 right now, and datapoints for certain years are often missing.
But it's otherwise a great (and did I mention _free_?) source of data.
'''
# Add some spacing
''
''
min_value = gdp_df['Year'].min()
max_value = gdp_df['Year'].max()
from_year, to_year = st.slider(
'Which years are you interested in?',
min_value=min_value,
max_value=max_value,
value=[min_value, max_value])
countries = gdp_df['Country Code'].unique()
if not len(countries):
st.warning("Select at least one country")
selected_countries = st.multiselect(
'Which countries would you like to view?',
countries,
['DEU', 'FRA', 'GBR', 'BRA', 'MEX', 'JPN'])
''
''
''
# Filter the data
filtered_gdp_df = gdp_df[
(gdp_df['Country Code'].isin(selected_countries))
& (gdp_df['Year'] <= to_year)
& (from_year <= gdp_df['Year'])
]
st.header('GDP over time', divider='gray')
''
st.line_chart(
filtered_gdp_df,
x='Year',
y='GDP',
color='Country Code',
)
''
''
first_year = gdp_df[gdp_df['Year'] == from_year]
last_year = gdp_df[gdp_df['Year'] == to_year]
st.header(f'GDP in {to_year}', divider='gray')
''
cols = st.columns(4)
for i, country in enumerate(selected_countries):
col = cols[i % len(cols)]
with col:
first_gdp = first_year[gdp_df['Country Code'] == country]['GDP'].iat[0] / 1000000000
last_gdp = last_year[gdp_df['Country Code'] == country]['GDP'].iat[0] / 1000000000
if math.isnan(first_gdp):
growth = 'n/a'
delta_color = 'off'
else:
growth = f'{last_gdp / first_gdp:,.2f}x'
delta_color = 'normal'
st.metric(
label=f'{country} GDP',
value=f'{last_gdp:,.0f}B',
delta=growth,
delta_color=delta_color
)