-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_helper.py
More file actions
212 lines (185 loc) · 6.87 KB
/
Copy pathapp_helper.py
File metadata and controls
212 lines (185 loc) · 6.87 KB
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
import os
import glob
import numpy as np
import pandas as pd
import streamlit as st
import plotly_express as px
from app_constants import PRODUCT_COLOR_SCHEME
@st.cache_data
def get_data(path, type='csv'):
li = []
extract_path = f'{os.getcwd()}{path}'
if type == 'csv':
files = glob.glob(os.path.join(extract_path, '*.csv'))
print(len(files))
for file in files:
df = pd.read_csv(file, dtype={'shop_id': str, 'sb_shop_id': str, 'ctime': str,})
li.append(df)
files.clear()
df = pd.concat(li, axis=0, ignore_index=True)
elif type == 'parquet':
files = glob.glob(os.path.join(extract_path, '*.parquet'))
print(len(files))
for file in files:
df = pd.read_parquet(file)
li.append(df)
files.clear()
df = pd.concat(li, axis=0, ignore_index=True)
df = df.sort_values('scraping_day')
return df
@st.cache_data
def get_recent_day_data(data, shop_id=None):
days = data['scraping_day'].unique().tolist()
recent_day = days[-1]
df_selection = data.loc[data['scraping_day'] == recent_day]
if shop_id:
df_selection = df_selection[df_selection['shop_id'] == shop_id]
return df_selection, recent_day
@st.cache_data
def get_recent_7_days_data(data, shop_id=None):
days = data['scraping_day'].unique().tolist()
recent_days = days[-7:]
df_selection = data.loc[data['scraping_day'].isin(recent_days)]
if shop_id:
df_selection = df_selection[df_selection['shop_id'] == shop_id]
return df_selection, recent_days
def converse_currency(num):
return '₫ {:0,}'.format(num).replace(',', '.')
def config_chart(fig, type, direction='v'):
fig.update_layout(paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
margin=dict(l=50, r=50, b=70, t=70,),)
fig.update_layout(
title={
'y': 0.94,
'x': 0.03,
'font_size': 18,
'xanchor': 'left',
'yanchor': 'top'}
)
if type == 'bar':
fig.update_traces(textfont_size=12,
textposition='outside', cliponaxis=False)
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
if direction == 'v':
fig.update_xaxes(
linecolor='#fff',
)
elif direction == 'h':
fig.update_yaxes(
linecolor='#fff'
)
# fig.update_yaxes(
# )
return fig
def plt_revenue_per_category(df_selection):
if 'sale' not in list(df_selection):
df_selection['sale'] = ((df_selection['price_max_before_discount'] +
df_selection['price_min_before_discount']) // 2 * df_selection['sold'])
revenue_per_category = df_selection.groupby(['category'])['sale'].sum(
).reset_index(name='count').sort_values('count', ascending=True)
revenue_per_category['currency'] = revenue_per_category['count'].apply(converse_currency)
fig = px.bar(
data_frame=revenue_per_category,
x='count',
y='category',
title='Revenue Per Category',
text='currency',
orientation='h',
labels={'count': 'Total Sales'},
)
x_range = int(revenue_per_category.tail(1)['count'].values[0] * 1.1)
fig = config_chart(fig=fig, type='bar', direction='h')
fig.update_traces(textfont_size=18)
fig.update_layout(
xaxis_range=[0, x_range],
)
st.plotly_chart(fig, use_container_width=True)
def plt_market_share_color(df_selection):
color_ms = df_selection['tag_color'].value_counts().reset_index()
color_ms.columns = ['tag_color', 'count']
cs = []
for color in color_ms['tag_color'].values:
if PRODUCT_COLOR_SCHEME.get(color):
cs.append(PRODUCT_COLOR_SCHEME.get(color))
fig = px.pie(data_frame=color_ms,
values='count',
names='tag_color',
title='Color',
color_discrete_sequence=cs)
fig.update_traces(textposition='inside', textinfo='percent+label')
fig = config_chart(fig=fig, type='pie')
st.plotly_chart(fig, use_container_width=True)
def plt_market_share_category(df_selection):
category_ms = df_selection['category'].value_counts().reset_index()
category_ms.columns = ['category', 'count']
fig = px.pie(data_frame=category_ms,
values='count',
names='category',
title='Category')
fig = config_chart(fig=fig, type='pie')
st.plotly_chart(fig, use_container_width=True)
def plt_sold_per_category(df_selection):
sold_per_category = df_selection.groupby(['category'], dropna=False)['sold'].sum(
).reset_index(name='sold').sort_values('category', ascending=False)
fig = px.bar(
data_frame=sold_per_category,
x='category',
y='sold',
title='Sold Per Category',
text='sold'
)
fig = config_chart(fig=fig, type='bar')
# fig.update_traces(marker_line_color = '#636EFA', marker_line_width = 4)
st.plotly_chart(fig, use_container_width=True)
def plt_sold_per_category_and_subcategory(df_selection):
sold_per_category_subcate = df_selection.groupby(['category', 'sub_category'])['sold'].sum(
).reset_index(name='count').sort_values(['category', 'count', 'sub_category'], ascending=False)
fig = px.bar(
data_frame=sold_per_category_subcate,
x='category',
y='count',
color='sub_category',
title='Sold Per Category And Sub Category',
text='count',
labels={'count': 'Sold', 'sub_category': 'Sub Category'},
)
fig = config_chart(fig=fig, type='bar')
st.plotly_chart(fig, use_container_width=True)
def plt_sold_per_subcategory(df_selection):
sold_per_category = df_selection.groupby(['sub_category'], dropna=False)['sold'].sum().reset_index(
name='count').sort_values('count', ascending=False)
size = len(sold_per_category)
if size > 10:
size = 10
fig = px.bar(
data_frame=sold_per_category.head(size),
x='sub_category',
y='count',
title='Sold Per Sub Category',
text='count',
labels={'count': 'Sold', 'sub_category': 'Sub Category'},
)
fig = config_chart(fig=fig, type='bar')
st.plotly_chart(fig, use_container_width=True)
def plt_best_shop_selling(df_selection):
fig = px.bar(
data_frame=df_selection,
x='shop_id',
y='sold',
title='Best Shop Selling',
text='sold'
)
fig = config_chart(fig, 'bar')
st.plotly_chart(fig, use_container_width=True)
def plt_best_product_selling(df_selection):
fig = px.bar(
data_frame=df_selection,
x='item_id',
y='sold',
color='shop_id',
title='Best Product Selling',
text='sold'
)
fig = config_chart(fig, 'bar')
st.plotly_chart(fig, use_container_width=True)