forked from vizzu-streamlit/world-population-story
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_slim.py
361 lines (310 loc) · 8.73 KB
/
app_slim.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
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
from streamlit.components.v1 import html
import pandas as pd
from ipyvizzu import Data, Config, Style
from ipyvizzustory import Story, Slide, Step
import ssl
import streamlit as st
ssl._create_default_https_context = ssl._create_unverified_context
st.set_page_config(page_title='World Population Streamlit Story', layout='centered')
st.title('World Population Forecast')
st.header('An interactive ipyvizzu-story in Streamlit')
width=570
height=400
# initialize chart
data = Data()
df = pd.read_csv('Data/worldpop.csv', dtype={'Year': str})
data.add_data_frame(df)
#@title Create the story
regions = df['Region'].unique()
sel_region = st.selectbox(
'Select region',
list(regions))
skip_intro = st.checkbox(
'Skip intro slides', value=False
)
df_region = df[df['Region'] == sel_region]
pop_max = int(df_region[df_region['Category'] == 'Population'][['Medium','High','Low']].max().T.max()*1.1)
df_future = df_region[df_region['Period'] == 'Future']
df_futureCategories = df_future[df_future['Category']!='Population'][['Category','Medium','High','Low']];
df_future_sum = df_futureCategories.groupby('Category').sum().T
other_max = df_future_sum.max().max() * 1.1
other_min = df_future_sum.max().max() * -1.1
region_palette = ['#FE7B00FF','#FEBF25FF','#55A4F3FF','#91BF3BFF','#E73849FF','#948DEDFF']
region_palette_str = ' '.join(region_palette)
region_color = region_palette[list(regions).index(sel_region)]
category_palette = ['#FF8080FF', '#808080FF', region_color.replace('FF','20'), '#60A0FFFF', '#80A080FF']
category_palette_str = ' '.join(category_palette)
# Define the style of the charts in the story
style = {
'legend' : {'width' : '13em'},
'plot': {
'yAxis': {
'label': {
'fontSize': '1em',
'numberFormat' : 'prefixed',
'numberScale':'shortScaleSymbolUS'
},
'title': {'color': '#ffffff00'},
},
'marker' :{
'label' :{
'numberFormat' : 'prefixed',
'maxFractionDigits' : '1',
'numberScale':'shortScaleSymbolUS',
}
},
'xAxis': {
'label': {
'angle': '2.5',
'fontSize': '1em',
'paddingRight': '0em',
'paddingTop': '1em',
'numberFormat' : 'grouped',
},
'title': {'color': '#ffffff00'},
},
},
}
story = Story(data=data)
story.set_size(width, height)
# Add the first slide, containing a single animation step
# that sets the initial chart.
if skip_intro:
style['plot']['marker']['colorPalette'] = region_palette_str
else:
slide1 = Slide(
Step(
Data.filter("record.Period === 'Past' && record.Category === 'Population'"),
Config(
{
'x':'Year',
'y': 'Medium',
'label': 'Medium',
'title': 'The Population of the World 1950-2020',
}
),
Style(style)
)
)
# Add the slide to the story
story.add_slide(slide1)
# Show components side-by-side
slide2 = Slide(
Step(
Config(
{
'y': ['Medium','Region'],
'color': 'Region',
'label': None,
'title': 'The Population of Regions 1950-2020',
}
),
Style({ 'plot.marker.colorPalette': region_palette_str })
)
)
story.add_slide(slide2)
# Show components side-by-side
slide3 = Slide()
slide3.add_step(
Step(
Data.filter("record.Category === 'Population'"),
Config(
{
'y': ['Medium','Region'],
'color': 'Region',
# 'lightness': 'Period',
# 'x': ['Year','Period'],
'title': 'The Population of Regions 1950-2100',
}
)
))
slide3.add_step(
Step(
Config(
{
'geometry':'area'
}
)
))
story.add_slide(slide3)
slide4 = Slide(
Step(
Config(
{
'split': True
},
),
Style({'plot' : {'yAxis' :{ 'label' :{ 'color' : '#99999900'}}}})
)
)
story.add_slide(slide4)
slide5 = Slide(
Step(
Config.percentageArea(
{
'x':'Year',
'y':'Medium',
'stackedBy':'Region',
'title': 'The Population of Regions 1950-2100 (%)'
}
),
Style({'plot' : {'yAxis' :{ 'label' :{ 'color' : '#999999FF'}}}})
)
)
story.add_slide(slide5)
slide6 = Slide()
slide6.add_step(
Step(
Config.stackedArea(
{
'x':'Year',
'y':'Medium',
'stackedBy':'Region',
}
),
Style(style) #,{'plot.marker.colorPalette': region_palette_str}
))
slide6.add_step(
Step(
Data.filter(f'record.Category === "Population" && record.Region === "{sel_region}"'),
Config({
'title': 'The Population of '+sel_region+' 1950-2100',
'channels':{'y':{
'range':{'max':pop_max}
}}
}),
))
story.add_slide(slide6)
slide7 = Slide(
Step(
Config(
{
'y':'High',
'title': 'High prediction for '+sel_region
}
)
)
)
story.add_slide(slide7)
slide8 = Slide(
Step(
Config(
{
'y':'Low',
'title': 'Low prediction for '+sel_region
}
)
)
)
story.add_slide(slide8)
slide9 = Slide(
Step(
Config(
{
'y':'Medium',
'title': 'Medium prediction for '+sel_region
}
)
)
)
story.add_slide(slide9)
slide10 = Slide()
slide10.add_step(
Step(
Config({
'y':['Medium','Category'],
'title': 'Adding Sources of Gain and Loss to the Mix '
}),
)
)
slide10.add_step(
Step(
Data.filter(f'record.Region === "{sel_region}" && (record.Category === "Population" || record.Category === "Migration+" || record.Category === "Births")'),
Config(
{
'color': ['Category']
}),
Style({ 'plot.marker.colorPalette': category_palette_str })
)
)
slide10.add_step(
Step(
Data.filter(f'record.Region === "{sel_region}"'),
)
)
story.add_slide(slide10)
slide11 = Slide()
slide11.add_step(
Step(
Config(
{
'geometry':'rectangle',
}
)
)
)
slide11.add_step(
Step(
Data.filter(f'record.Period === "Future" && record.Region === "{sel_region}"'),
Config(
{
'title': 'Zoom to the future'
}
)
)
)
slide11.add_step(
Step(
Data.filter(f'record.Period === "Future" && record.Region === "{sel_region}" && record.Category !== "Population"'),
Config(
{
'channels':{
'x':{'set':['Medium','Year'],'range':{'max':other_max,'min':other_min}},
'y':{'set': 'Category', 'range':{'max':'auto'}},
},
'title': 'Sources of Population Gain and Loss - Medium Scenario'
},
),
Style({'plot' : {'marker' :{ 'label' :{ 'maxFractionDigits' : '1'}}}})
)
)
slide11.add_step(
Step(
Config(
{
'x':'Medium',
'label':'Medium',
}
)
)
)
story.add_slide(slide11)
slide12 = Slide(
Step(
Config(
{
'x':'High',
'label': 'High',
'title': 'Sources of Population Gain and Loss - High Scenario'
}
)
)
)
story.add_slide(slide12)
slide13 = Slide(
Step(
Config(
{
'x':'Low',
'label': 'Low',
'title': 'Sources of Population Gain and Loss - Low Scenario'
}
)
)
)
story.add_slide(slide13)
# Switch on the tooltip that appears when the user hovers the mouse over a chart element.
story.set_feature('tooltip', True)
html(story._repr_html_(), width=width, height=height)
st.download_button('Download HTML export', story.to_html(), file_name=f'world-population-story-{sel_region}.html', mime='text/html')
#st.markdown('''T.B.D''')