-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
200 lines (156 loc) · 5.67 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
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
# pyright: reportMissingModuleSource=false
# pyright: reportMissingImports=false
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from utils import (
get_country_coords,
save_csv,
)
# dropdown box for selecting country
st.title("Plots 🌎")
le_df = pd.read_csv("./edu_datasets/life_expectancy_years.csv")
countries = le_df["Country"].unique()
edu_indices = [
"Primary Education",
"Lower Secondary Education",
"Higher Secondary Education",
"College Completion",
]
health_indices = [
"Total Fertility Rate",
"Life Expectancy",
]
econ_indices = [
"GDP per Capita",
]
time_indices = [
"Years",
]
indices = edu_indices + health_indices + econ_indices + time_indices
cleaned_indices ={
"f_pri_edu" : "Female Primary Education",
"f_ls_edu" : "Female Lower Secondary Education",
"f_hs_edu" : "Female Higher Secondary Education",
"f_clg_comp" : "Female College Completion",
"m_pri_edu" : "Male Primary Education",
"m_ls_edu" : "Male Lower Secondary Education",
"m_hs_edu" : "Male Higher Secondary Education",
"m_clg_comp" : "Male College Completion",
"pri_edu" : "Primary Education",
"ls_edu" : "Lower Secondary Education",
"hs_edu" : "Higher Secondary Education",
"clg_comp" : "College Completion",
"gdp" : "GDP per Capita",
"le" : "Life Expectancy",
"tfr" : "Total Fertility Rate",
"time" : "Years",
}
cleaned_indices_reversed = {v: k for k, v in cleaned_indices.items()}
selected_options = []
selected_ys = []
params = st.experimental_get_query_params()
selected_countries = params.get("c", countries)
selected_countries = selected_countries[0].split(",")
selected_options = params.get("gender", [])
print("selected_options",selected_options)
selected_x, selected_y = indices[0], indices[1]
try:
selected_x = cleaned_indices[params.get("x", indices)[0]]
except:
pass
try:
selected_y = cleaned_indices[params.get("y", indices)[0]]
except:
pass
try:
start_year = params.get("sy", 1960)[0]
except:
start_year = 1960
try:
end_year = params.get("ey", 2020)[0]
except:
end_year = 2020
col1, col2 = st.columns(2)
selected_y = col1.selectbox("Select y axis", indices, index=indices.index(selected_y))
selected_x = col2.selectbox("Select x axis", indices, index=indices.index(selected_x))
if(selected_y in edu_indices):
options = ['Both', 'Male', 'Female']
print("selected_options",selected_options)
col1, col2, col3 = st.columns(3)
checkbox_state1 = col1.checkbox(options[0],value = options[0] in selected_options)
checkbox_state2 = col2.checkbox(options[1],value = options[1] in selected_options)
checkbox_state3 = col3.checkbox(options[2],value = options[2] in selected_options)
if checkbox_state1:
selected_options.append(options[0])
else:
selected_options = [option for option in selected_options if option != options[0]]
if checkbox_state2:
selected_options.append(options[1])
else:
selected_options = [option for option in selected_options if option != options[1]]
if checkbox_state3:
selected_options.append(options[2])
else:
selected_options = [option for option in selected_options if option != options[2]]
selected_options = list(set(selected_options))
for selected_option in selected_options:
selected_ys.append((selected_option + " " + selected_y,selected_option))
st.write(selected_y)
else:
selected_ys.append((selected_y,""))
if(selected_x in edu_indices):
selected_x = "Both " + selected_x
# Add a dropdown box to select a country
selected_countries = st.multiselect("Select Countries", countries, selected_countries)
selected_years = st.slider("Select years", 1960, 2020, (int(start_year), int(end_year)))
st.experimental_set_query_params(
c=",".join(selected_countries),
x=cleaned_indices_reversed[selected_x],
y=cleaned_indices_reversed[selected_y],
sy=selected_years[0],
ey=selected_years[1],
gender=selected_options
)
# plot the line chart using Matplotlib
fig, ax = plt.subplots()
country_coords = None
for selected_country in selected_countries:
for selected_y,gender in selected_ys:
country_coords = get_country_coords(selected_country, selected_x, selected_y,selected_years)
ax.plot(country_coords["x"], country_coords["y"], label=selected_country + " " + gender)
ax.set_xlabel(selected_x)
ax.set_ylabel(selected_y)
ax.set_title(f"{selected_y} vs {selected_x}")
ax.legend()
if country_coords is not None:
col1, col2 = st.columns(2)
file_name = f"{selected_x[:3]}_{selected_y[:3]}"
csv_snippet = save_csv(selected_countries, selected_x, selected_y, selected_years)
with open("chart.csv", "rb") as f:
data_bytes = f.read()
col1.download_button(
label = "Download Data 💿",
data = data_bytes,
file_name = f"{file_name}.csv",
mime = "text/csv",
)
fig.savefig("chart.png")
with open("chart.png", "rb") as f:
image_bytes = f.read()
col2.download_button(
label = "Download Graph 📈",
data = image_bytes,
file_name = f"{file_name}.png",
mime = "image/png",
)
# display the chart in Streamlit app
st.pyplot(fig)
st.markdown(
"""**_Note_** : the Education data used is only 20-25 year old age group and data and it is as follows:
- **Primary Education** : 6 years of education
- **Lower Secondary Education** : 9 years of education
- **Higher Secondary Education** : 12 years of education
- **College Completion** : 16 years of education
"""
)