-
Notifications
You must be signed in to change notification settings - Fork 0
/
brand_dash.py
206 lines (181 loc) · 6.15 KB
/
brand_dash.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
import streamlit as st
import plotly.graph_objects as go
from collections import Counter
import pandas as pd
def dataframe_lolist(csv):
df=pd.read_csv(csv)
return df.values.tolist()
# hmkidsl=dataframe_lolist('output_hmkids.csv')
# zarakidsl=dataframe_lolist('output_zarakids.csv')
# mothercarel=dataframe_lolist('output_mothercare.csv')
brandhmkidsl=dataframe_lolist('output_brandhmkids.csv')
brandzarakidsl=dataframe_lolist('output_brandzarakids.csv')
brandmothercarel=dataframe_lolist('output_brandmothercare.csv')
import json
def cleanup(nested_list):
final=[]
for row in nested_list:
try:
response=json.loads(row[1])
if response["classification"] != "others":
final.append(row)
except:
continue
return final
# finalhmkidsl=cleanup(hmkidsl)
# finalzarakidsl=cleanup(zarakidsl)
# finalmothercarel=cleanup(mothercarel)
finalbrandhmkidsl=cleanup(brandhmkidsl)
finalbrandzarakidsl=cleanup(brandzarakidsl)
finalbrandmothercarel=cleanup(brandmothercarel)
color_map = {
'Aqua': '#00FFFF',
'Beige': '#F5F5DC',
'Black': '#000000',
'Blue': '#0000FF',
'Brown': '#A52A2A',
'Charcoal': '#36454F',
'Coral': '#FF7F50',
'Cyan': '#00FFFF',
'Ecru': '#C2B280',
'Fuchsia': '#FF00FF',
'Gold': '#FFD700',
'Grey': '#808080',
'Green': '#008000',
'Ivory': '#FFFFF0',
'Khaki': '#F0E68C',
'Lavender': '#E6E6FA',
'Maroon': '#800000',
'Mint': '#98FF98',
'Mustard': '#FFDB58',
'Navy': '#000080',
'Olive': '#808000',
'Orange': '#FFA500',
'Peach': '#FFE5B4',
'Pink': '#FFC0CB',
'Purple': '#800080',
'Red': '#FF0000',
'Silver': '#C0C0C0',
'Teal': '#008080',
'Turquoise': '#40E0D0',
'White': '#FFFFFF',
'Yellow': '#FFFF00'
}
from collections import defaultdict, Counter
def product_color_counter(final_list):
product_colors=defaultdict(list)
for _,row in enumerate(final_list):
try:
x=json.loads(row[1])
products=x.get('fashion').keys()
for product in products:
colors=x.get('fashion').get(product).get('colors')
for color in colors:
product_colors[product].append(color)
except:
continue
product_color_count={}
for pc in product_colors:
product_color_count[pc]=Counter(product_colors[pc])
return product_color_count
# hmkids=product_color_counter(finalhmkidsl)
# zarakids=product_color_counter(finalzarakidsl)
# mothercare=product_color_counter(finalmothercarel)
hmkids=product_color_counter(finalbrandhmkidsl)
zarakids=product_color_counter(finalbrandzarakidsl)
mothercare=product_color_counter(finalbrandmothercarel)
# hm=set(hmkids.keys())
# za=set(zarakids.keys())
# mc=set(mothercare.keys())
# intersection=hm&za&mc
# common=list(intersection)
# common.sort()
hmb=set(hmkids.keys())
zab=set(zarakids.keys())
mcb=set(mothercare.keys())
common=hmb&zab&mcb
common=list(common)
common.sort()
def colors(datasets):
cols=set()
for dataset in datasets:
for product in dataset:
for color in dataset[product].keys():
cols.add(color)
return cols
#colours=colors([hmkids,zarakids,mothercare])
colours=colors([hmkids,zarakids,mothercare])
def reduce_labels(counter, max_labels=10):
total = sum(counter.values())
top_items = counter.most_common(max_labels)
others_count = total - sum(count for _, count in top_items)
if others_count > 0:
top_items.append(('Other', others_count))
return Counter(dict(top_items))
def create_pie_charts(products):
fig = go.Figure()
annotations = []
def create_pie(counter, name, domain_x):
reduced_counter = reduce_labels(counter)
total_count = sum(counter.values())
pie_chart = go.Pie(
labels=list(reduced_counter.keys()),
values=list(reduced_counter.values()),
name=name,
textinfo='label+percent',
hoverinfo='label+value+percent',
marker=dict(
colors=[color_map.get(color, '#CCCCCC') for color in reduced_counter.keys()],
line=dict(color='black', width=1)
),
hole=0.4,
domain={'x': domain_x, 'y': [0, 1]}
)
annotations.append(
dict(
x=sum(domain_x)/2, y=0.5,
text=f'{name}<br>{total_count}',
showarrow=False,
font=dict(size=14),
xanchor='center',
yanchor='middle'
)
)
return pie_chart
domains = [(i / len(products), (i + 1) / len(products)) for i in range(len(products))]
for i, product in enumerate(products):
if product in hmkids and product in zarakids and product in mothercare:
fig.add_trace(create_pie(hmkids[product], f'H&M Kids', [domains[i][0], domains[i][0] + 0.33]))
fig.add_trace(create_pie(zarakids[product], f'Zara Kids', [domains[i][0] + 0.33, domains[i][0] + 0.66]))
fig.add_trace(create_pie(mothercare[product], f'Mothercare', [domains[i][0] + 0.66, domains[i][1]]))
else:
annotations.append(
dict(
x=sum(domains[i])/2, y=0.5,
text=f"No data available for {product}.",
showarrow=False,
font=dict(size=14),
xanchor='center',
yanchor='middle'
)
)
fig.update_layout(
title='Color Distribution for Selected Products',
margin=dict(l=0, r=0, t=50, b=50),
showlegend=False,
uniformtext_minsize=12,
uniformtext_mode='hide',
paper_bgcolor='white',
plot_bgcolor='white',
height=600, # Adjust the height as needed
annotations=annotations
)
return fig
# Streamlit App
st.title("Distribution in brand posts")
selected_products = st.multiselect('Select products:', common, default=common[:1])
if selected_products:
fig = create_pie_charts(selected_products)
st.plotly_chart(fig, use_container_width=True)
else:
st.write("Please select at least one product.")