-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit.py
199 lines (129 loc) · 5.35 KB
/
streamlit.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 streamlit as st
st.set_page_config(layout="wide")
import pandas as pd
import altair as alt
import numpy as np
st.write("Psss, come here I've heard you want to buy some wine ... ")
st.title ("Wine Genie - Beta")
st.caption("\
\
Credit: Hugo Ruiz Verastegui - hugo.ruiz.verastegui@gmail.com ")
#st.header("this is the markdown")
#st.markdown("this is the header")
#st.subheader("this is the subheader")
#st.caption("this is the caption")
# load the dataframe
@st.cache_data
def get_data():
df = pd.read_csv("wine_diplo_2025_14Jan_updated.csv")
return df
#return df.set_index("country")
df = get_data()
#make sure the price and rating are floats
#remove commas in the price and - in vivino rating
df['price_usd'] = df['price_usd'].replace({',': ''}, regex=True)
df['vivino_rating'] = df['vivino_rating'].replace({'-': '0'}, regex=True)
df['price_usd'] = df['price_usd'].astype(float) #(or int)
df['vivino_rating'] = df['vivino_rating'].astype(float) #(or int)
#round price and vivino rating
df['price_usd']=df['price_usd'].round(2)
df['vivino_rating']=df['vivino_rating'].round(2)
#keep only where we have vivino ratings
df=df[df['vivino_rating']>0]
# choose countries
#choose the countries
countries = st.multiselect(
"Choose countries", list(df.country.unique())
)
#refine dataset
#data = df.loc[countries]
if len(countries)>0:
data=df[df['country'].isin(countries)]
else:
data=df
#choose the regions
regions = st.multiselect(
"Choose regions", list(data.vivino_region.unique())
)
if len(regions)>0:
data=data[data['vivino_region'].isin(regions)]
#choose the price range
values = st.slider(
'Choose the price range',
0, 500, (0, 500))
#confidence filter
min_confidence=st.slider('define mininum confidence', 0, 100, 60)
#filter confidence
data=data[data['confidence']>=min_confidence]
#data=data[data['price_usd'].isin(values)]
data=data[data['price_usd'].between(*values)]
#choose a wine directly
wine = st.multiselect(
"Choose a wine:", list(data.Name.unique())
)
if len(wine)>0:
data=data[data['Name'].isin(wine)]
# Search by keyword
data['name_lower']=data['Name'].str.lower()
keyword = st.text_input('Or search a keyword', value="")
if len(keyword)>0:
data=data[data['name_lower'].str.contains(keyword.lower())]
#sort by price asc
data=data.sort_values(by='price_usd', ascending=True)
# fit a log regression on the rating vs price
fit = np.polyfit(np.log(list(data['price_usd'])), list(data['vivino_rating']) , 1)
# calculate deviation vs fit in absolute and relatvie
data['log_fit_delta']=data['vivino_rating']-(fit[1] + fit[0] * np.log(data['price_usd']))
data['log_fit_delta_relative']=data['log_fit_delta'] / (fit[1] + fit[0] * np.log(data['price_usd']))
# color coding = relative delta vs log fit
chart_no_color = (
alt.Chart(data).mark_circle().encode(
x = alt.Y('price_usd' , scale=alt.Scale(type='log',domain=[data['price_usd'].min(), data['price_usd'].max()])),
y = alt.Y('vivino_rating' , scale=alt.Scale(domain=[data['vivino_rating'].min(), data['vivino_rating'].max()])),
tooltip=['price_usd','vivino_rating','Name','confidence',]
#'IDS link','vivino_url'],
))
chart_color = (
alt.Chart(data).mark_circle().encode(
x = alt.Y('price_usd' , scale=alt.Scale(type='log',domain=[data['price_usd'].min(), data['price_usd'].max()])),
y = alt.Y('vivino_rating' , scale=alt.Scale(domain=[data['vivino_rating'].min(), data['vivino_rating'].max()])),
#y='vivino_rating' ,
# color=alt.Color('confidence',legend=None).scale(scheme='redyellowgreen'),
color=alt.Color(
'confidence',
scale=alt.Scale(
domain=[60, 95], # Explicitly map 0 to 100
range=['red', 'yellow', 'green'] # Define the color scheme
),
legend=None),
tooltip=['price_usd','vivino_rating','Name','confidence','log_fit_delta_relative']
#'IDS link','vivino_url'],
)
)
chart=chart_color + chart_no_color.transform_regression('price_usd', 'vivino_rating', method='log').mark_line()
st.altair_chart(chart, use_container_width=True)
# order the dataset by delta relative and keep only top 10
data_top=data.sort_values('log_fit_delta_relative', ascending=False)
data_top=data_top[['Name','country','vivino_region','price_usd','vivino_rating','IDS link','vivino_url','confidence']]
data_top=data_top.head(100)
st.header("top 100 wines based on your criterias")
# define color coding for the confidence column
def color_confidence(val):
color = 'red' if val<=60 else 'orange' if val<=75 else 'green'
return f'background-color: {color}'
#st.dataframe(data_top.reset_index(drop=True).style.applymap(color_column, subset=['vivino_rating']))
def make_clickable_buy(link):
return f'<a target="_blank" href="{link}">"Buy Now"</a>'
def make_clickable_vivino(link):
return f'<a target="_blank" href="{link}">"Check it on Vivino"</a>'
data_top['IDS link'] = df['IDS link'].apply(make_clickable_buy)
data_top['vivino_url'] = df['vivino_url'].apply(make_clickable_vivino)
data_top=data_top.style\
.applymap(color_confidence, subset=['confidence'])\
.format({"price_usd": "{:.1f}","vivino_rating": "{:.1f}"})
#.format({"vivino_rating": "{:.1f}"})
data_top = data_top.to_html(escape=False)
st.write(data_top, unsafe_allow_html=True)
#st.dataframe(data_top.style\
# .background_gradient(axis=None, cmap='RdYlGn_r',subset=['price_usd'])
# .background_gradient(axis=None, cmap='RdYlGn',subset=['vivino_rating'])