-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
297 lines (272 loc) · 10.2 KB
/
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
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
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output
import plotly.graph_objs as go
from categoryplot import getPlot
from data import dfTips
import numpy as np
app = dash.Dash()
app.title = 'Dash Si Tampan'
color_set = {
'sex': ['#ff3fd8', '#4290ff'],
'smoker': ['#99ffff', '#ac7339'],
'time': ['#0033cc', '#ffff4d'],
'day': ['#80ced6', '#66ff66', '#ff66d9', '#993300']
}
estiFunc = {
'count': len,
'sum': sum,
'mean': np.mean,
'std': np.std
}
disabledEsti = {
'count': True,
'sum': False,
'mean': False,
'std': False
}
def generate_table(dataframe, max_rows=10):
return html.Table(
[html.Tr([html.Th(col, className='tableStyle') for col in dataframe.columns])] +
[html.Tr([
html.Td(dataframe.iloc[i][col], className='tableStyle') for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))]
,className='tableStyle'
)
app.layout = html.Div(children=[
dcc.Tabs(id='tabs', value= 'tab-4',
style = {
'fontFamily': 'system-ui'
},
content_style = {
'fontFamily': 'Arial',
'borderLeft': '1px solid #d6d6d6',
'borderRight': '1px solid #d6d6d6',
'borderBottom': '1px solid #d6d6d6',
'padding': '44px'
},
children=[
dcc.Tab(label='Tips data set', value= 'tab-1', children=[
html.Div([
html.H1(
children='Tips Data Set',
className='h1Tab'
),
generate_table(dfTips)
])
]),
dcc.Tab(label='Scatter Plot', value= 'tab-2', children=[
html.Div([
html.H1(
children='Scatter Plot Tips Data Set',
className='h1Tab'
),
html.Table([
html.Tr([
html.Td(html.P('Hue: ')),
html.Td(
dcc.Dropdown(
id='ddl-hue-scatter',
options=[{'label': 'Smoker', 'value': 'smoker'},
{'label': 'Sex', 'value': 'sex'},
{'label': 'Day', 'value': 'day'},
{'label': 'Time', 'value': 'time'}
],
value='sex'
)
)
])
], style={'width': '300px'}),
html.Div(html.P('', id='jml-data-scatter')),
dcc.Graph(
id='scatterPlot',
figure={
'data': []
}
),
dcc.Slider(
id='size-scatter-slider',
min=dfTips['size'].min(),
max=dfTips['size'].max(),
value=dfTips['size'].min(),
marks={str(size): str(size) for size in dfTips['size'].unique()}
)
])
]),
dcc.Tab(label='Categorical Plot', value= 'tab-3', children=[
html.Div([
html.H1(
children='Categorical Plot Tips Data Set',
className='h1Tab'
),
html.Table([
html.Tr([
html.Td([
html.P('Jenis: '),
dcc.Dropdown(
id='ddl-jenis-plot-category',
options=[{'label': 'Bar', 'value': 'bar'},
{'label': 'Violin', 'value': 'violin'},
{'label': 'Box', 'value': 'box'}
],
value='bar'
)
]),
html.Td([
html.P('X Axis: '),
dcc.Dropdown(
id='ddl-x-plot-category',
options=[{'label': 'Smoker', 'value': 'smoker'},
{'label': 'Sex', 'value': 'sex'},
{'label': 'Day', 'value': 'day'},
{'label': 'Time', 'value': 'time'}
],
value='sex'
)
])
])
], style={'width': '900px'}),
dcc.Graph(
id='categoricalPlot',
figure={
'data': []
}
)
])
]),
dcc.Tab(label='Pie Chart', value= 'tab-4', children=[
html.Div([
html.H1(
children='Pie Chart Tips Data Set',
className='h1Tab'
),
html.Table([
html.Tr([
html.Td(html.P('Hue: ')),
html.Td(
dcc.Dropdown(
id='ddl-hue-pie',
options=[{'label': 'Smoker', 'value': 'smoker'},
{'label': 'Sex', 'value': 'sex'},
{'label': 'Day', 'value': 'day'},
{'label': 'Time', 'value': 'time'}
],
value='sex'
)
),
html.Td(html.P('Estimator: ')),
html.Td(
dcc.Dropdown(
id='ddl-est-pie',
options=[{'label': 'Count', 'value': 'count'},
{'label': 'Sum', 'value': 'sum'},
{'label': 'Mean', 'value': 'mean'},
{'label': 'Std', 'value': 'std'}
],
value='count'
)
),
html.Td(html.P('Column: ')),
html.Td(
dcc.Dropdown(
id='ddl-col-pie',
options=[{'label': 'Total Bill', 'value': 'total_bill'},
{'label': 'Tip', 'value': 'tip'}
],
value='total_bill',
disabled=True
)
)
])
], style={'width': '900px'}),
dcc.Graph(
id='piePlot',
figure={
'data': []
}
)
])
])
])
],
style = {
'maxWidth': '1000px',
'margin': '0 auto'
}
)
# Categorical graph callback
@app.callback(
Output('categoricalPlot', 'figure'),
[Input('ddl-jenis-plot-category', 'value'), Input('ddl-x-plot-category', 'value')]
)
def update_category_graph(ddljeniscategory, ddlxcategory):
return {
'data': getPlot(ddljeniscategory, ddlxcategory),
'layout': go.Layout(
xaxis={'title': ddlxcategory.capitalize()}, yaxis={'title': 'US$'},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1}, hovermode='closest',
boxmode='group', violinmode='group'
# plot_bgcolor= 'black', paper_bgcolor= 'black',
)
}
# scatter plot callback
@app.callback(
Output('scatterPlot', 'figure'),
[Input('ddl-hue-scatter', 'value'), Input('size-scatter-slider', 'value')]
)
def update_scatter_hue(ddlhuescatter, size):
return {
'data': [
go.Scatter(
x=dfTips[(dfTips[ddlhuescatter] == col) & (dfTips['size'] == size)]['total_bill'],
y=dfTips[(dfTips[ddlhuescatter] == col) & (dfTips['size'] == size)]['tip'],
mode='markers',
# line=dict(color=color_set[i], width=1, dash='dash'),
marker=dict(color=color_set[ddlhuescatter][i], size=10, line={'width': 0.5, 'color': 'white'}), name=col)
for col,i in zip(dfTips[ddlhuescatter].unique(),range(len(color_set[ddlhuescatter])))
],
'layout': go.Layout(
xaxis={'title': 'Total Bill'},
yaxis={'title': 'Tip'},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
hovermode='closest'
)
}
# Perhitungan jumlah data
@app.callback(
Output('jml-data-scatter', 'children'),
[Input('size-scatter-slider', 'value')]
)
def update_scatter_jmlData(size):
return 'Jumlah Data: ' + str(len(dfTips[dfTips['size'] == size]))
# Pie Callback
@app.callback(
Output('ddl-col-pie', 'disabled'),
[Input('ddl-est-pie', 'value')]
)
def update_disabled(disabled):
return disabledEsti[disabled]
@app.callback(
Output('piePlot', 'figure'),
[Input('ddl-hue-pie', 'value'), Input('ddl-est-pie', 'value'), Input('ddl-col-pie', 'value')]
)
def update_pie_hue(ddlhuepie, est, col):
return {
'data': [
go.Pie(labels=list(dfTips[ddlhuepie].unique()),
values=[estiFunc[est](dfTips[dfTips[ddlhuepie]==item][col]) for item in dfTips[ddlhuepie].unique()],
hoverinfo='label+percent', textinfo='value',
textfont=dict(size=20),
marker=dict(colors=color_set[ddlhuepie],
line=dict(color='#000000', width=2)))
],
'layout': go.Layout(
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1}
)
}
if __name__ == '__main__':
app.run_server(debug=True, port=1996)