-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
443 lines (334 loc) · 20.8 KB
/
main.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#imports
from choosing import *
from module import *
import streamlit as st
from PIL import Image
from datetime import date, timedelta
if 'sidebar_state' not in st.session_state:
st.session_state['sidebar_state'] = 'expanded'
if 'search_submission' not in st.session_state:
st.session_state['search_submission'] = False
st.set_page_config(page_title="Choosing: enjoy your best meal",
page_icon="🔍",
layout="wide",
initial_sidebar_state=st.session_state["sidebar_state"],
menu_items={
'Get Help': "mailto:[email protected]",
'Report a bug': 'mailto:[email protected]',
'About': """
## FAQ
##### 1) Perchè il filtro sul tipo di pasto o sulla spesa per persona non cambia il ristorante consigliato?
Nel caso di risultato non derivante da un match con altri utenti, quei due filtri non vengono applicati.
Il consiglio infatti prende in considerazione solo le recensioni Google per la posizione ricercata.
##### 2) Perchè non vedo più alcuni ristoranti che avevo nella mia lista ancora da valutare?
Hai 10 giorni di tempo dal momento in cui scegli un ristorante per lasciare una valutazione.
Dopodiché quel ristorante sarà rimosso dalla lista.
##### 3) Come faccio a modificare una valutazione a un ristorante nella mia lista?
Al momento, non è possibile eliminare una valutazione. Tuttavia, è possibile sovrascriverla. Choosing salva
la valutazione più recente per ristorante-tipo di pasto e scarta quella precedente
"""
}
)
image = Image.open('logo.png')
st.image(image, width=450)
#inputs
st.sidebar.title(':mag::mag_right:')
username = st.sidebar.text_input('Identificati con una tua mail (non sarà visibile agli altri utenti)', placeholder="e.g. [email protected]" ).strip().lower()
if not username:
st.markdown("""
<div style = "text-align: justify;">
**Per cominciare, identificati con una mail nel menù a sinistra e buon appetito :)**
# Cos'è Choosing?
La sua missione unica e imprescindibile è quella di generare un consiglio personalizzato per il tuo prossimo ristorante.
Finalmente, non dovrai più scrivere a Pippo *"Oh Pippo sono a Roma, dimmi dove posso andare a mangiare un'ottima pizza*", perchè la risposta te la da Choosing, che è molto più disponibile e preciso di Pippo.
E basta anche con le ore spese davanti ai siti di recensioni a contare le stelline e a confrontare prezzi e numero di commenti delle infinite alternative per poi
finire a mangiare nella solita osteria.
Dedica il tuo tempo alle cose migliori. A sceglierle ci pensa Choosing.
Enjoy your best meal.
## Come funziona?
Choosing usa due strategie alternative:
1. Ricerca per similarità tra gli utenti di Choosing in base ai ristoranti in comune e ai rating assegnati
2. Qualora 1. non restituisca un risultato, Choosing sceglie il ristorante migliore basandosi sulle recensioni di Google
Il consiglio che deriva da 1. è molto più personalizzato rispetto a quello di 2. ed è il vero orgoglio di Choosing. L'unica condizione affinché si verifichi è la partecipazione attiva di tanti utenti e il passaparola, se credi nei superpoteri di Choosing.
## Feedback
Per suggerimenti o per rimanere semplicemente in contatto scrivimi a [email protected]
## Support
Mantenere e migliorare Choosing ha qualche costo (APIs, server...).
Se credi che Choosing sia una bella e utile idea, puoi sostenerla con una piccola donazione qua: [Buy me a coffee](https://www.buymeacoffee.com/palealex)
## Author
- [Alessandro Ciocchetti](https://www.linkedin.com/in/ac-palealex/)
## 🚀 About Me
Gestisco il mio tempo tra tentativi di scrivere qualcosa che somigli a bel codice, di sviluppare idee che ricordino grandi progetti, di suonare l'armonica come la suonava Paul Butterfield.
Lo sviluppo web e lo studio delle proprietà delle Serie Temporali mi divertono particolarmente.
</div>
""", unsafe_allow_html=True)
elif not check(username): #and username != config.root:
st.warning('Email non valida', icon="⚠️")
#elif username == config.root:
# with open('final.csv') as f:
# st.download_button('final', f, mime='text/csv')
# with open('suggests.csv') as f:
# st.download_button('suggests', f, mime='text/csv')
# canc = st.checkbox("cancella file temporanei")
# mydir = "user_temps/"
# filelist = [ f for f in os.listdir(mydir) if f.endswith(".csv") ]
# st.write("numero file temporanei esistenti ", len(filelist))
# to_delete = st.multiselect('Delete', filelist, filelist)
# if canc:
# for f in to_delete:
# os.remove(os.path.join(mydir, f))
# st.write(f)
#mettere anche funzione per cancellare da final delle righe di utenti sbagliati per esempio (deve diventare dashboard di controllo)
else:
with st.sidebar:
st.markdown(f'''
<small>Benvenuto {username}!</small>
<small>Utilizza sempre questa mail per accedere al tuo account.</small>
<small>Inserisci le informazioni geografiche qui sotto per conoscere il miglior ristorante per te</small>
''', unsafe_allow_html=True)
with st.form(key='search'):
city = st.text_input('Città', placeholder="e.g. Ferrara", key = "field").capitalize()
province = st.text_input('Provincia (sigla)', placeholder="e.g. FE", max_chars=2).upper()
state = "Italia" #st.text_input('Stato', placeholder="e.g. Italia").capitalize()
border = st.selectbox("Confine di ricerca", ("comune", "provincia"))
c1,c2,c3 = st.columns(3)
with c2:
st.write("")
search_submission = st.form_submit_button("Cerca :mag:", type = 'primary')
st.write("* * * ")
st.write('*<small>(la modifica dei campi seguenti influenza il consiglio solo se esistono match con altri utenti)</small>*', unsafe_allow_html=True )
meal = st.multiselect('*Cosa vuoi mangiare?*', ("Primi piatti", "Pizza", "Street food", "Carne", "Pesce", "Vegetariano/Vegano", "Etnico", "Orientale", "Altro"), ("Primi piatti", "Pizza", "Street food", "Carne", "Pesce", "Vegetariano/Vegano", "Etnico", "Orientale", "Altro"), help="Utile solo nel caso di match con altri utenti")
epp = st.slider("*Range spesa per persona*", 1, 100, (15, 50), help="Utile solo nel caso di match con altri utenti")
st.write("\n")
back = st.button("Torna nella tua homepage")
st.write("\n")
if search_submission:
st.session_state['sidebar_state'] = 'collapsed' if st.session_state['sidebar_state'] == 'expanded' else 'expanded'
st.session_state['search_submission'] = True
st.experimental_rerun()
if not meal or not city or not province or back:
st.write("*Per conoscere il tuo prossimo miglior ristorante, compila i campi del menù laterale. Se invece devi ancora valutare un ristorante in cui sei stato, continua prima qui sotto*")
#reviewing
container1 = st.expander("VEDI I RISTORANTI A CUI POTRESTI LASCIARE UNA VALUTAZIONE")
with container1:
user_to_review = read_suggests()
user_to_review = user_to_review[user_to_review["user"] == username].drop_duplicates(keep= 'last').reset_index(drop= True)
if len(user_to_review) > 0:
st.dataframe(data=user_to_review.iloc[:, :-1])
with st.form(key="review"):
selected = st.selectbox("Selezionane uno dalla lista", set(user_to_review["name"]))
reviewed = user_to_review[user_to_review["name"] == selected].reset_index(drop = True)
NUMBER_OF_COLUMNS = 3
f, s, t = st.columns(NUMBER_OF_COLUMNS)
with f:
reviewed_meal = st.selectbox("Cos'hai mangiato? (se hai provato diverse portate, seleziona quella che ti ha convinto maggiormente)", ("Primi piatti", "Pizza", "Street food", "Carne", "Pesce", "Vegetariano/Vegano", "Etnico", "Orientale", "Altro"))
with s:
reviewed_epp = st.slider('Spesa per persona (circa)', 1, 100, 25)
with t:
reviewed_rate = st.slider('Valutazione complessiva', 1.0, 10.0, 8.0, 0.1)
ok = st.form_submit_button("Salva!")
if ok:
st.balloons()
st.success("Grazie per il tuo contributo. Continua a usare Choosing :) ", icon = "✅")
reviewed["added"] = datetime.date(datetime.now())
reviewed["rate"] = reviewed_rate
reviewed["what"] = reviewed_meal.capitalize()
reviewed["epp"] = reviewed_epp
final = read_final()
final = pd.concat([final, reviewed], axis=0, ignore_index=True)
final = final.drop_duplicates(subset=["name", "user", "what"], keep = 'last').reset_index(drop=True)
final.to_csv("final.csv")
filename = "final.csv"
upload(filename)
tendaysago = date.today() - timedelta(10)
suggests = read_suggests().reset_index(drop=True)
suggests["added"] = pd.to_datetime(suggests["added"], format='%Y-%m-%d').dt.date
try:
suggests.drop(suggests.index[(suggests['name'] == reviewed['name'][0]) & (suggests['user'] == reviewed['user'][0])], inplace=True)
except:
pass
suggests = suggests[suggests["added"] > tendaysago].reset_index(drop=True)
suggests.to_csv("suggests.csv")
filename = "suggests.csv"
upload(filename)
st.experimental_rerun()
else:
st.write("Non hai nessun ristorante salvato da valutare. Scegline prima uno e, dopo averlo provato, torna qua a valutarlo.")
container2 = st.expander("VEDI I RISTORANTI A CUI HAI LASCIATO UNA VALUTAZIONE")
with container2:
user_reviewed = read_final()
user_reviewed = user_reviewed[user_reviewed["user"] == username].drop_duplicates(keep= 'last').reset_index(drop= True)
if len(user_reviewed) > 0:
st.dataframe(data=user_reviewed.iloc[:, [0,1,3,5,6,7]])
else:
st.write("Non hai ancora lasciato nessuna valutazione.")
else:
#choosing object
ch = Choosing(username, meal, city, province, state, epp, border)
if st.session_state['search_submission'] == True:
df = (
ch.read_temp()[0:0]
.to_csv(f"user_temps/temp_{username}.csv", encoding='utf-8')
)
filename = f"user_temps/temp_{username}.csv"
upload(filename)
st.session_state['search_submission'] = False
#suggests
container = st.expander("TI SVELO IL TUO PROSSIMO MIGLIOR RISTORANTE", expanded=True)
with container:
#---------------------------------------------------------------------
def markdown_and_save(n_rist, type_of_choice):
if type_of_choice == 'matched':
chose = ch.matched_choice(choice = n_rist)
st.markdown('''
******Trovato :tada: ! In base al match con gli altri utenti:******
*(se sei già stato in tutte le proposte di Choosing prova a modificare i criteri di ricerca, oppure torna nel migliore e assaggia qualcosa di nuovo!)*
''')
else:
chose, latlon = ch.random_choice(choice = n_rist)
st.markdown('''
******Non ci sono suggerimenti per questa zona in base a similarità con altri utenti. Ecco uno dei migliori ristoranti selezionato secondo le recensioni di Google:******
''')
st.map(latlon, zoom = 13)
html_str = f"""
<div style ="text-align: center;" >
<h3>
{chose.iloc[:,0][0]}
</h3>
<h6>
{chose.iloc[:,1][0]}
<br>
</h6>
</div>
<br>
"""
return html_str, chose
#--------------------------------------------------------------------
try:
alt = len(ch.matched_suggests())
if alt == 1:
n_rist = 0
html, chose = markdown_and_save(n_rist, type_of_choice="matched")
st.markdown(html, unsafe_allow_html=True)
#form = st.form(key="case1", clear_on_submit = True)
#with form:
# ok = st.checkbox("Ok, scelgo questo!")
#submit = form.form_submit_button("Salva")
saving = st.button("Ok, scelgo questo!")
if saving:
st.balloons()
st.success("Ristorante registrato a tuo nome. Ottima scelta e buon appetito! Ricordati poi di tornare qua a dargli un voto", icon = "✅")
suggests = read_suggests()
suggests = pd.concat([suggests, chose], axis = 0, ignore_index=True)
suggests.to_csv("suggests.csv")
filename = "suggests.csv"
upload(filename)
df = (
ch.read_temp()[0:0]
.to_csv(f"user_temps/temp_{username}.csv", encoding='utf-8')
)
filename = f"user_temps/temp_{username}.csv"
upload(filename)
del st.session_state['search_submission']
elif alt > 1:
already = st.checkbox("Ci sono già stato, dimmene un altro")
if already:
disabled = False
else:
disabled = True
rist_slider = st.select_slider(
'Ecco delle alternative in ordine decrescente di punteggio di match',
options=np.arange(1, alt+1, 1), disabled = disabled)
n_rist = rist_slider - 1
html, chose = markdown_and_save(n_rist, type_of_choice="matched")
st.markdown(html, unsafe_allow_html=True)
#form = st.form(key="case2", clear_on_submit = True)
#with form:
# ok = st.checkbox("Ok, scelgo questo!")
#submit = form.form_submit_button("Salva")
saving = st.button("Ok, scelgo questo!")
if saving:
st.balloons()
st.success("Ristorante registrato a tuo nome. Ottima scelta e buon appetito! Ricordati poi di tornare qua a dargli un voto", icon = "✅")
suggests = read_suggests()
suggests = pd.concat([suggests, chose], axis=0, ignore_index=True)
suggests.to_csv("suggests.csv")
filename = "suggests.csv"
upload(filename)
df = (
ch.read_temp()[0:0]
.to_csv(f"user_temps/temp_{username}.csv", encoding='utf-8')
)
filename = f"user_temps/temp_{username}.csv"
upload(filename)
del st.session_state['search_submission']
else:
raise Exception("Nessun match trovato")
except:
try:
if border == 'provincia':
alt = 0
while alt == 0:
alt = len(ch.random_restaurants()) #se non trova niente nella provincia, rifa un altro giro
else:
alt = len(ch.random_restaurants())
if alt == 1:
n_rist = 0
html, chose = markdown_and_save(n_rist, type_of_choice="random")
st.markdown(html, unsafe_allow_html=True)
#form = st.form(key="case3", clear_on_submit = True)
#with form:
# ok = st.checkbox("Ok, scelgo questo!")
#submit = form.form_submit_button("Salva")
saving = st.button("Ok, scelgo questo!")
if saving:
st.balloons()
st.success("Ristorante registrato a tuo nome. Ottima scelta e buon appetito! Ricordati poi di tornare qua a dargli un voto", icon = "✅")
suggests = read_suggests()
suggests = pd.concat([suggests, chose], axis=0, ignore_index=True)
suggests.to_csv("suggests.csv")
filename = "suggests.csv"
upload(filename)
df = (
ch.read_temp()[0:0]
.to_csv(f"user_temps/temp_{username}.csv", encoding='utf-8')
)
filename = f"user_temps/temp_{username}.csv"
upload(filename)
del st.session_state['search_submission']
elif alt > 1:
already = st.checkbox("Ci sono già stato, dimmene un altro")
if already:
disabled = False
else:
disabled = True
rist_slider = st.select_slider(
'Ecco delle alternative in ordine decrescente di punteggio calcolato in base alle recensioni di Google',
options=np.arange(1, alt+1, 1), disabled = disabled)
n_rist = rist_slider - 1
html, chose = markdown_and_save(n_rist, type_of_choice="random")
st.markdown(html, unsafe_allow_html=True)
#form = st.form(key="case4", clear_on_submit = True)
#with form:
# ok = st.checkbox("Ok, scelgo questo!")
#submit = form.form_submit_button("Salva")
saving = st.button("Ok, scelgo questo!")
if saving:
st.balloons()
st.success("Ristorante registrato a tuo nome. Ottima scelta e buon appetito! Ricordati poi di tornare qua a dargli un voto", icon = "✅")
suggests = read_suggests()
suggests = pd.concat([suggests, chose], axis=0, ignore_index=True)
suggests.to_csv("suggests.csv")
filename = "suggests.csv"
upload(filename)
df = (
ch.read_temp()[0:0]
.to_csv(f"user_temps/temp_{username}.csv", encoding='utf-8')
)
filename = f"user_temps/temp_{username}.csv"
upload(filename)
del st.session_state['search_submission']
if alt == 0:
st.write("Nessun consiglio per i criteri ricercati.")
except:
st.write("La ricerca non è andata a buon fine... Verifica che le informazioni nei campi da te inserite siano coerenti, o prova a reimpostare la ricerca.")