-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdashboard.py
392 lines (337 loc) · 13.1 KB
/
dashboard.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
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
# -*- coding: utf-8 -*-
import base64
import functools
import io
import os
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import plotly.graph_objs as go
import plotly.figure_factory as ff
import iso3166
import pandas as pd
from wordcloud import WordCloud, STOPWORDS
app = dash.Dash()
app.css.append_css({
"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"
})
##############################################################
# #
# D A T A L O A D I N G #
# #
##############################################################
def get_alpha3(alpha2):
"""Get alpha3 code from alpha2 code."""
country = iso3166.countries_by_alpha2.get(alpha2)
if country is None:
return 'UNKNOWN'
return country.alpha3
kickstarter_df = pd.read_csv('kickstarter-cleaned.csv', parse_dates=True)
kickstarter_df_sub = kickstarter_df.sample(10000)
kickstarter_df['created_at'] = pd.to_datetime(kickstarter_df['created_at'])
kickstarter_country = kickstarter_df.groupby('country').count().reset_index()
kickstarter_country['country'] = kickstarter_country['country'].map(get_alpha3)
kickstarter_df['broader_category'] = kickstarter_df['category_slug'].str.split('/').str.get(0)
COLUMNS = ['launched_at', 'deadline', 'blurb', 'usd_pledged', 'state', 'spotlight', 'staff_pick', 'category_slug', 'backers_count', 'country']
COLORS = ['#C7583F', '#D4752E', '#C7B815', '#7DFB6D']
##############################################################
# #
# D A T A F U N C T I O N S #
# #
##############################################################
def get_state_trace(x, state, color, df):
"""Generate a bar trace from x, state, color and a dataframe."""
# Count number of rows under state `name`
trace_data = [
(df[(~df.staff_pick)].state == state).sum(),
(df[(df.staff_pick)].state == state).sum(),
]
return go.Bar(
x=x,
y=trace_data,
name=state,
marker=dict(
color=color
)
)
def generate_grouped_bar_chart_vs_spotlight_and_staff_pick(df):
"""Generate a figure with success rate box plot according to combinations of spotlight and staff pick values."""
x = [
'Not picked by staff',
'Picked by staff'
]
states = ['failed', 'suspended', 'canceled', 'successful']
colors = ['#C7583F', '#D4752E', '#C7B815', '#7DFB6D']
data = [
get_state_trace(x, state, color, df)
for (state, color) in zip(states, colors)
]
layout = go.Layout(
barmode='group',
title='Number of projects in a given state relative to spotlight and staff pick'
)
fig = go.Figure(data=data, layout=layout)
return fig
def generate_usd_pledged_hist_vs_spotlight_and_staff_pick(df):
"""Generate a figure with USD pledged histogram according to combinations of spotlight and staff pick values."""
bin_size = 2000
df_successful = df[(df.state == 'successful') & (df.usd_pledged < 100000)]
hist_data = [
df_successful[(~df_successful.staff_pick)].usd_pledged.values,
df_successful[(df_successful.staff_pick)].usd_pledged.values,
]
group_labels = [
'Not picked by staff',
'Picked by staff'
]
colors = ['#03241F', '#68D35A']
# Create distplot with curve_type set to 'normal'
fig = ff.create_distplot(hist_data, group_labels, colors=colors,
bin_size=bin_size, show_rug=False)
# Add title
fig['layout'].update(
title='Successful projects with under $100k pledged',
xaxis={'title': 'Money pledged in USD'},
yaxis={'title': 'Proportion'},
)
return fig
def generate_table(dataframe, max_rows=10):
"""Generate an HTML table from a dataframe."""
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr([
html.Td(str(dataframe.iloc[i][col])) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))]
)
def grey_color_func(word, font_size, position, orientation, random_state=None,
**kwargs):
"""Color function for wordcloud."""
return 'hsl(0, 0%, {0}%)'.format(20)
def generate_map():
data = [
dict(
type='choropleth',
locations=kickstarter_country.country,
z=kickstarter_country.id,
text=kickstarter_country.country,
autocolorscale=True,
colorbar=dict(
autotick=True,
title='Number of projects'
),
marker=dict(
line=dict(
color='rgb(180,180,180)',
width=0.5
)
),
)
]
layout = dict(
title='Project counts by country',
geo=dict(
showland=True,
landcolor="#DDDDDD",
projection=dict(
type='Mercator'
)
)
)
figure = dict(data=data, layout=layout)
return figure
##############################################################
# #
# L A Y O U T #
# #
##############################################################
app.layout = html.Div(children=[
html.H1(children='Kickstarter Dashboard', style={
'textAlign': 'center',
}),
dcc.Dropdown(
id='category',
options=[{'label': i, 'value': i} for i in kickstarter_df['broader_category'].unique()],
value=kickstarter_df['broader_category'].unique()[0],
),
dcc.Graph(
id='money-vs-date'
),
dcc.Checklist(
id='states',
options=[{'label': i, 'value': i} for i in ['canceled', 'failed', 'successful', 'suspended']],
values=['canceled', 'failed', 'successful', 'suspended'],
labelStyle={'display': 'inline-block'}
),
dcc.Graph(
id='generate-usd-pledged-hist-vs-spotlight-and-staff-pick',
figure=generate_usd_pledged_hist_vs_spotlight_and_staff_pick(kickstarter_df)
),
dcc.Graph(
id='generate-success-rate-boxed-plot-vs-spotlight-and-staff-pick',
figure=generate_grouped_bar_chart_vs_spotlight_and_staff_pick(kickstarter_df)
),
dcc.RadioItems(
id='kickstarter-barchart-type',
options=[{'label': i, 'value': i} for i in ['cumulative', 'normalized']],
value='cumulative',
labelStyle={'display': 'inline-block'}
),
dcc.RadioItems(
id='kickstarter-barchart-aggregation',
options=[{'label': i, 'value': i} for i in ['count', 'usd_pledged']],
value='count',
labelStyle={'display': 'inline-block'}
),
dcc.Graph(id='kickstarter-barchart'),
html.Div(children=[
dcc.Slider(
id='kickstarter-barchart-year-slider',
min=2011,
max=2017,
value=kickstarter_df['created_at'].dt.year.max(),
step=None,
marks={str(year): str(year) for year in range(2011, 2018)}
),
], style={
'marginBottom': '50px',
}),
html.Div(
children=[html.Img(id='wordcloud')],
style={
'textAlign': 'center',
}
),
dt.DataTable(
# Using astype(str) to show booleans
rows=kickstarter_df[COLUMNS].sample(100).astype(str).to_dict('records'),
columns=COLUMNS,
editable=False,
filterable=True,
sortable=True,
id='kickstarter-datatable'
),
dcc.Graph(id='map', figure=generate_map()),
])
##############################################################
# #
# E V E N T S #
# #
##############################################################
@app.callback(
dash.dependencies.Output('kickstarter-barchart', 'figure'),
[
dash.dependencies.Input('kickstarter-barchart-type', 'value'),
dash.dependencies.Input('kickstarter-barchart-aggregation', 'value'),
dash.dependencies.Input('kickstarter-barchart-year-slider', 'value'),
])
def update_bar_chart(kickstarter_barchart_type, kickstarter_barchart_aggregation, kickstarter_barchart_year_slider):
"""Update bar chart."""
if 'usd_pledged' == kickstarter_barchart_aggregation:
stacked_barchart_df = (
kickstarter_df[
(kickstarter_df['created_at'].dt.year == kickstarter_barchart_year_slider)
]
.groupby(['broader_category', 'state'])[[kickstarter_barchart_aggregation]]
.sum()
.reset_index('state')
.pivot(columns='state')
)
if kickstarter_barchart_type == 'normalized':
stacked_barchart_df = stacked_barchart_df.div(stacked_barchart_df.sum(axis=1), axis=0)
stacked_barchart_df.reset_index(lace=True)
else:
stacked_barchart_df = (
kickstarter_df[
(kickstarter_df['created_at'].dt.year == kickstarter_barchart_year_slider)
]['state'].groupby(kickstarter_df['broader_category'])
.value_counts(normalize=kickstarter_barchart_type == 'normalized')
.rename(kickstarter_barchart_aggregation)
.to_frame()
.reset_index('state')
.pivot(columns='state')
.reset_index()
)
return {
'data': [
go.Bar(
x=stacked_barchart_df['broader_category'],
y=stacked_barchart_df[kickstarter_barchart_aggregation][state],
name=state,
) for state in ['canceled', 'failed', 'successful', 'suspended']
],
'layout': go.Layout(
xaxis={'title': 'Date'},
yaxis={'title': 'USD pledged'},
barmode='stack',
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
@app.callback(
dash.dependencies.Output('money-vs-date', 'figure'),
[
dash.dependencies.Input('category', 'value'),
])
@functools.lru_cache(maxsize=50)
def update_usd_pledged_vs_time(category):
"""Update the graph."""
return {
'data': [
go.Scatter(
x=kickstarter_df_sub[(kickstarter_df.broader_category == category) & (kickstarter_df_sub.state == state)]['created_at'],
y=kickstarter_df_sub[(kickstarter_df.broader_category == category) & (kickstarter_df_sub.state == state)]['usd_pledged'],
text=kickstarter_df_sub[(kickstarter_df.broader_category == category) & (kickstarter_df_sub.state == state)]['name'],
mode='markers',
opacity=0.7,
marker={
'size': 15,
'color': color,
'line': {'width': 0.5, 'color': 'white'}
},
name=state,
) for (state, color) in zip(kickstarter_df.state.unique(), COLORS[::-1])
],
'layout': go.Layout(
xaxis={'title': 'Date'},
yaxis={'title': 'USD pledged', 'type': 'log'},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
@app.callback(
dash.dependencies.Output('wordcloud', 'src'),
[
dash.dependencies.Input('states', 'values'),
dash.dependencies.Input('category', 'value'),
])
def update_wordcloud(states, category):
"""Update the wordcloud."""
if states == []:
return ''
states = frozenset(states)
return _update_wordcloud_from_set(states, category)
@functools.lru_cache(maxsize=50)
def _update_wordcloud_from_set(states, category):
"""Update the wordcloud."""
text = ' '.join(kickstarter_df_sub[kickstarter_df_sub.state.isin(states) & (kickstarter_df_sub.broader_category == category)].blurb).lower()
wordcloud_buffer = io.BytesIO()
wordcloud_image = (
WordCloud(stopwords=set(STOPWORDS), background_color='white', max_words=500, width=800, height=400, color_func=grey_color_func)
.generate(text).to_image()
)
wordcloud_image.save(wordcloud_buffer, format="JPEG")
encoded_wordcloud = base64.b64encode(wordcloud_buffer.getvalue()).decode('utf-8')
return 'data:image/png;base64,{}'.format(encoded_wordcloud)
##############################################################
# #
# M A I N #
# #
##############################################################
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run_server(debug=True, host='0.0.0.0', port=port)