-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
201 lines (164 loc) · 4.28 KB
/
utils.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
import dash
import dash_mantine_components as dmc
from dash import Dash, Input, Output, State, dcc, html, callback
import plotly.express as px
import pandas as pd
from data_etl import df_raw
def bar_chart(
df: pd.DataFrame,
x_axis: str,
y_axis: str,
n_largest=10
):
# Aggregating data before plotting
dff = df[[
x_axis,
y_axis
]].groupby(
by=[x_axis],
as_index=False
).agg('sum')
dff = dff.nlargest(n=n_largest, columns=y_axis)
dff.sort_values(by=y_axis, inplace=True, ascending=False)
# Creating figure
fig = px.histogram(
dff,
x=x_axis,
y=y_axis,
template='simple_white'
)
fig.update_layout(
margin=dict(t=50, l=25, r=25, b=25),
yaxis_title=y_axis,
xaxis_title=x_axis
)
return fig
def bar_chart_grouped(
df: pd.DataFrame,
x_axis: str,
y_axis: str,
color: str,
barmode: str = 'group',
n_largest=10
):
# Aggregating data before plotting
dff = df[[
x_axis, y_axis, color
]].groupby(
by=[x_axis, color],
as_index=False
).sum()
dff = dff.nlargest(n=n_largest, columns=y_axis)
# Creating figure
fig = px.histogram(
dff,
x=x_axis,
y=y_axis,
color=color,
barmode=barmode,
template='simple_white'
)
fig.update_layout(
margin=dict(t=50, l=25, r=25, b=25),
yaxis_title=y_axis,
xaxis_title=x_axis
)
return fig
def create_metric_chooser():
return dmc.Select(
label="Select Metric",
placeholder="Please select metric",
id='metric-chooser',
value='volume_sold_gallons',
data=[
'volume_sold_gallons',
'volume_sold_liters',
'sale_dollars',
'bottles_sold',
'state_bottle_retail',
'state_bottle_cost',
'bottle_volume_ml',
'pack'],
style={"width": 200, "marginBottom": 10},
)
def create_top_n_filter():
return dmc.NumberInput(
label="Top N ",
value=5,
min=1,
step=1,
max=100,
style={"width": 200},
id='top-n-filter'
)
def create_city_filter():
return dmc.Select(
label="City",
placeholder="Select one",
id="city-filter",
clearable=True,
data=df_raw['city'].unique().astype(str).tolist(),
style={"width": 200, "marginBottom": 10},
)
def create_category_filter():
return dmc.Select(
label="Category",
placeholder="Select one",
id="category-filter",
clearable=True,
data=df_raw['category_name'].unique().astype(str).tolist(),
style={"width": 200, "marginBottom": 10},
)
def create_vendor_filter():
return dmc.Select(
label="Vendor",
placeholder="Select one",
id="vendor-filter",
clearable=True,
data=df_raw['vendor_name'].unique().astype(str).tolist(),
style={"width": 200, "marginBottom": 10},
)
def create_bottly_volume_filter():
return dmc.Select(
label="Bottle volume ml",
placeholder="Select one",
id="bottle-size-filter",
clearable=True,
data=df_raw['bottle_volume_ml'].unique().astype(str).tolist(),
style={"width": 200, "marginBottom": 10},
)
def create_simple_grid(component_list: list):
return dmc.Container([
dmc.SimpleGrid(
spacing='lg',
cols=2,
breakpoints=[
{"maxWidth": 980, "cols": 3, "spacing": "md"},
{"maxWidth": 755, "cols": 2, "spacing": "sm"},
{"maxWidth": 600, "cols": 1, "spacing": "sm"},
],
style={
"textAlign": "center",
},
children=component_list)
])
def line_chart(
df: pd.DataFrame,
x_axis: str,
y_axis: str,
color: str = None
):
dff = df.sort_values(by=x_axis)
fig = px.line(
data_frame=dff,
x=x_axis,
y=y_axis,
color=color,
template='simple_white'
)
fig.update_layout(
margin=dict(t=50, l=25, r=25, b=25),
yaxis_title='Price',
xaxis_title='Date'
)
return fig