-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
645 lines (550 loc) Β· 22.5 KB
/
app.py
File metadata and controls
645 lines (550 loc) Β· 22.5 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import streamlit as st
from pathlib import Path
from engine import QuizEngine
from datetime import datetime
from uuid import uuid4
from supabase import create_client
# ---------------------------------------------------------------------------
# CONFIGURAZIONE PAGINA
# Deve essere la PRIMA chiamata Streamlit in assoluto
# ---------------------------------------------------------------------------
st.set_page_config(
page_title="PyGym",
page_icon="ποΈ",
layout="wide"
)
# ---------------------------------------------------------------------------
# PERCORSI
# ---------------------------------------------------------------------------
BASE_DIR = Path(__file__).resolve().parent
ASSETS_DIR = BASE_DIR / "assets"
DATA_DIR = BASE_DIR / "data"
COVER_IMAGE = ASSETS_DIR / "cover_image.png"
QUESTIONS_FILE = DATA_DIR / "questions.json"
# ---------------------------------------------------------------------------
# CLIENT SUPABASE CON LE CREDENZIALI CARICATE DA .streamlit/secrets.toml
# ---------------------------------------------------------------------------
@st.cache_resource
def get_supabase():
return create_client(
st.secrets["SUPABASE_URL"],
st.secrets["SUPABASE_KEY"],
)
# ---------------------------------------------------------------------------
# CARICAMENTO CSS PER LA COVER IMG
# ---------------------------------------------------------------------------
def load_css(file_name: str):
css_path = BASE_DIR / file_name
with open(css_path, "r") as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
# ---------------------------------------------------------------------------
# INIZIALIZZAZIONE SESSION_STATE
# ---------------------------------------------------------------------------
def init_session_state():
"""
Chiavi e significati:
phase β quale pagina mostrare
engine β istanza di QuizEngine (caricata una sola volta)
category_id β categoria scelta dall'utente nella config
num_questions β numero domande scelto nella config
last_answer β UserAnswer dell'ultima risposta (per mostrare feedback)
answered β True se l'utente ha giΓ risposto alla domanda corrente
(serve per bloccare il tasto e mostrare spiegazione)
quiz_results β dizionario risultati finali (da engine.get_results())
"""
defaults = {
"phase": "home", # (home/config/quiz/result/leaderboard)
"engine": None,
"category_ids": [], # lista di category_id selezionati
"num_questions": 5,
"last_answer": None,
"answered": False,
"quiz_results": None,
"result_saved": False,
"save_celebration_pending": False,
}
for key, value in defaults.items():
if key not in st.session_state:
st.session_state[key] = value
def get_engine() -> QuizEngine:
"""
Restituisce l'istanza di QuizEngine, creandola se non esiste.
Salvandola nel session_state, il JSON viene letto una volta sola.
"""
if st.session_state.engine is None:
st.session_state.engine = QuizEngine(str(QUESTIONS_FILE))
return st.session_state.engine
@st.dialog("Conferma abbandono quiz")
def confirm_abandon_quiz_dialog():
st.write("Se abbandoni ora, i progressi del quiz in corso verranno persi.")
col_yes, col_no = st.columns(2)
with col_yes:
if st.button("β Abbandona Quiz", type="secondary", use_container_width=True):
engine = get_engine()
engine.reset()
st.session_state.quiz_results = None
st.session_state.last_answer = None
st.session_state.answered = False
st.session_state.result_saved = False
st.session_state.save_celebration_pending = False
st.session_state.phase = "home"
st.rerun()
with col_no:
if st.button("β
Continua", type="secondary", use_container_width=True):
st.rerun()
# ----------------------------------------------------------------------------
# PAGINE
# ----------------------------------------------------------------------------
def page_home():
"""
Pagina iniziale: immagine + messaggio di benvenuto.
"""
if COVER_IMAGE.exists():
# per rimuovere padding e margini attorno all'immagine
load_css("styles.css")
st.image(str(COVER_IMAGE), use_container_width=True)
else:
st.markdown(
"""
<div style='text-align:center; padding: 60px 0'>
<h1 style='font-size: 4rem'>ποΈ PyGym</h1>
<p style='font-size: 1.4rem; color: gray'>
Allenati ogni giorno con Python
</p>
</div>
""",
unsafe_allow_html=True,
)
with st.sidebar:
st.title("ποΈ PyGym")
st.markdown("<br><br>", unsafe_allow_html=True)
st.divider()
st.markdown("### Inizia il tuo allenamento")
if st.button("π Inizia il Quiz", use_container_width=True, type="primary"):
st.session_state.phase = "config"
st.session_state.result_saved = False
st.session_state.save_celebration_pending = False
st.rerun()
st.markdown("<br>", unsafe_allow_html=True)
st.markdown("### Visualizza la classifica")
if st.button("π Classifica", use_container_width=True, type="secondary"):
st.session_state.phase = "leaderboard"
st.rerun()
def page_config():
"""
Pagina configurazione: l'utente sceglie difficoltΓ , categorie
e il numero di domande totali da includere nel quiz.
"""
engine = get_engine()
with st.sidebar:
st.title("ποΈ PyGym")
st.divider()
# --- Info punteggio ---
st.info(
"π‘ **Sistema di punteggio** \n\n"
"β’ Domanda facile = 1 punto \n"
"β’ Domanda media = 2 punti \n"
"β’ Domanda difficile = 3 punti \n\n"
"β
Se rispondi correttamente, guadagni i punti della domanda \n"
"β Se sbagli, nessuna penalitΓ , lβobiettivo Γ¨ imparare.\n"
)
st.divider()
st.header("Configura il tuo quiz")
# --- Selezione difficoltΓ ---
difficulty_map = {
"Tutte le difficoltΓ ": None,
"Facile (1 pt)": 1,
"Media (2 pt)": 2,
"Difficile (3 pt)": 3,
}
diff_label = st.radio(
"π― DifficoltΓ ",
options=list(difficulty_map.keys()),
index=0,
)
selected_difficulty = difficulty_map[diff_label]
# --- Multiselect categorie (filtrate per difficoltΓ ) ---
categories = engine.get_categories(difficulty=selected_difficulty)
cat_labels = [
f"{c['name']} - ({c['question_count']})" for c in categories]
label_to_id = {
f"{c['name']} - ({c['question_count']})": c["id"]
for c in categories
}
selected_labels = st.multiselect(
"π Categorie",
options=cat_labels,
default=[],
placeholder="Scegli una o piΓΉ categorie...",
key=f"cats_{selected_difficulty}",
)
# Ricava la lista di category_id dalle etichette selezionate
selected_ids = [label_to_id[lbl] for lbl in selected_labels]
# --- Numero di domande con +/β ---
if selected_ids:
min_q = len(selected_ids)
max_q = engine.get_max_questions_for(
selected_ids, difficulty=selected_difficulty)
default_q = max(min_q, min(5, max_q))
num_q = st.slider(
"π’ Numero di domande",
min_value=min_q,
max_value=max_q,
value=default_q,
step=1,
)
st.info(
f"Minimo {min_q} domande (1 per categoria) Β· Massimo {max_q} domande")
else:
num_q = 0
st.number_input(
"π’ Numero di domande",
min_value=0,
max_value=1,
value=0,
step=1,
disabled=True,
help="Seleziona almeno una categoria per abilitare questo campo",
)
st.divider()
# --- Bottone avvia ---
avvia_disabled = len(selected_ids) == 0
if st.button("βΆ Avvia Quiz", use_container_width=True,
type="primary",
disabled=avvia_disabled):
st.session_state.category_ids = selected_ids
st.session_state.num_questions = num_q
st.session_state.quiz_results = None
st.session_state.result_saved = False
st.session_state.save_celebration_pending = False
# Avvia il quiz nell'engine con la lista di categorie
engine.start_quiz(selected_ids, num_q,
difficulty=selected_difficulty)
# Reset stato risposta per la pagina quiz
st.session_state.last_answer = None
st.session_state.answered = False
st.session_state.phase = "quiz"
st.rerun()
if st.button("β Home", use_container_width=True):
st.session_state.phase = "home"
st.rerun()
# --- Area principale: istruzioni / anteprima ---
st.title("ποΈ PyGym")
st.markdown("*Allenati ogni giorno con Python*")
st.divider()
if not selected_ids:
st.info("π Seleziona almeno una categoria dalla sidebar per iniziare.")
else:
# Mostra un riepilogo delle categorie scelte
st.markdown(
"### Stai Configurando il Quiz... Le singole categorie contengono varie domande:")
st.markdown("<br>", unsafe_allow_html=True)
max_cols_per_row = 4
cat_info_map = {c["id"]: c for c in categories}
for i in range(0, len(selected_ids), max_cols_per_row):
row_ids = selected_ids[i:i + max_cols_per_row]
cols = st.columns(len(row_ids), gap="xsmall")
for col, cat_id in zip(cols, row_ids):
cat = cat_info_map[cat_id]
with col:
st.metric(
label=cat["description"],
value=f"{cat['name']}",
delta=f"{cat['question_count']}",
border=False,
)
st.markdown("\n---\n")
def page_quiz():
engine = get_engine()
question = engine.current_question()
if question is None and not st.session_state.answered:
# Se il quiz Γ¨ finito e siamo finiti su page_quiz (es. reload), passa
# alla pagina risultati se possibile, altrimenti torna alla config.
if st.session_state.quiz_results is not None:
st.session_state.phase = "result"
elif engine.is_finished():
st.session_state.quiz_results = engine.get_results()
st.session_state.phase = "result"
else:
st.session_state.phase = "config"
st.rerun()
# --- Header con progresso ---
current, total = engine.progress()
st.markdown(f"### Domanda {current} di {total}")
st.progress(current / total)
difficulty_labels = {1: "π’ Facile", 2: "π‘ Medio", 3: "π΄ Difficile"}
display_question = question or (
st.session_state.last_answer.question
if st.session_state.last_answer else None
)
if display_question is None:
st.session_state.phase = "config"
st.rerun()
st.markdown(
f"**{difficulty_labels.get(display_question.difficulty, '')}** β "
f"vale **{display_question.difficulty} pt**"
)
st.divider()
# --- Testo della domanda ---
st.markdown(f"#### {display_question.text}")
st.markdown("<br>", unsafe_allow_html=True)
# --- Bottoni risposta ---
for i, ans in enumerate(display_question.answers):
if st.button(
ans.text,
key=f"answer_{i}",
disabled=st.session_state.answered,
use_container_width=True,
):
# salva risposta, NON avanza indice
ua = engine.answer(i)
st.session_state.last_answer = ua
st.session_state.answered = True
st.rerun()
# --- Feedback ---
if st.session_state.answered and st.session_state.last_answer:
ua = st.session_state.last_answer
if ua.is_correct:
st.success(f"β
Risposta corretta! Vale **+{ua.points} pt**")
else:
correct_text = next(
a.text for a in display_question.answers if a.is_correct
)
st.error(
f"π Hai risposto: `{ua.chosen_text}` \n"
f"β Risposta sbagliata. **{ua.points} pt** \n"
f"π‘ La risposta corretta era: `{correct_text}`"
)
with st.expander("π Spiegazione e esempio di codice"):
st.markdown(ua.question.explanation)
if ua.question.code_example:
st.code(ua.question.code_example, language="python")
st.divider()
# Label dinamico del bottone dipende se il quiz Γ¨ finito o no
# se siamo alla fine, mostriamo "Vedi risultati", altrimenti "Prossima domanda"
all_answered = engine.is_finished()
label = "π Vedi i risultati" if all_answered else "β‘οΈ Prossima domanda"
_, col_next, col_abandon, _ = st.columns([2, 1, 1, 2])
with col_next:
if st.button(label, type="primary"):
if all_answered:
# Raccoglie i risultati PRIMA di resettare lo stato
st.session_state.quiz_results = engine.get_results()
st.session_state.phase = "result"
else:
# avanza l'indice nell'engine
engine.next_question()
# Reset stato risposta per la prossima domanda (o per la pagina result)
st.session_state.last_answer = None
st.session_state.answered = False
st.rerun()
with col_abandon:
if st.button("β οΈ Abbandona", type="secondary"):
confirm_abandon_quiz_dialog()
def page_result():
"""
Pagina risultati: mostra il riepilogo finale e permette di salvare il punteggio.
"""
results = st.session_state.quiz_results
if results is None:
st.session_state.phase = "home"
st.rerun()
st.title("π Risultati")
st.divider()
# --- Metriche principali ---
col1, col2, col3, col4 = st.columns(4)
col1.metric("β
Corrette", results["correct"])
col2.metric("β Sbagliate", results["wrong"])
col3.metric("π Punteggio",
f"{results['score']} / {results['max_score']} pt")
col4.metric("π Percentuale", f"{results['percentage']}%")
st.divider()
# --- Riepilogo domanda per domanda ---
st.markdown("#### Dettaglio risposte")
for ua in results["answers"]:
icon = "β
" if ua.is_correct else "β"
delta = f"+{ua.points}" if ua.points > 0 else str(ua.points)
with st.expander(f"{icon} {ua.question.text[:60]}... ({delta} pt)"):
chosen = ua.chosen_text
correct = next(a.text for a in ua.question.answers if a.is_correct)
st.markdown(f"**La tua risposta:** {chosen}")
if not ua.is_correct:
st.markdown(f"**Risposta corretta:** {correct}")
st.markdown(f"**Spiegazione:** {ua.question.explanation}")
if ua.question.code_example:
st.code(ua.question.code_example, language="python")
st.divider()
# --- Salvataggio in classifica ---
st.markdown("#### πΎ Salva il tuo punteggio")
nome = st.text_input("Il tuo nome o nickname", placeholder="es. Mario")
col1, col2 = st.columns([1, 4])
with col1:
if st.button(
"πΎ Salva",
type="primary",
disabled=not nome.strip() or st.session_state.result_saved,
):
# Salva il risultato SOLO se non Γ¨ giΓ stato salvato in questa sessione
if not st.session_state.result_saved:
_save_result(nome.strip(), results)
st.session_state.result_saved = True
st.session_state.save_celebration_pending = True
st.rerun()
if st.session_state.result_saved:
if st.session_state.save_celebration_pending:
st.balloons()
st.session_state.save_celebration_pending = False
st.success("β
Punteggio salvato con successo! π")
st.markdown("<br>", unsafe_allow_html=True)
col3, col4 = st.columns(2)
with col3:
if st.button("π Nuovo Quiz", use_container_width=True):
engine = get_engine()
engine.reset()
st.session_state.quiz_results = None
st.session_state.result_saved = False
st.session_state.save_celebration_pending = False
st.session_state.phase = "config"
st.rerun()
with col4:
if st.button("π Classifica", use_container_width=True):
st.session_state.phase = "leaderboard"
st.rerun()
def page_leaderboard():
"""
Pagina classifica: legge results.json e mostra la top 10.
"""
st.title("π Classifica")
st.divider()
records = _load_results()
if not records:
st.info(
"Nessun punteggio salvato ancora. Completa un quiz per essere il primo!")
else:
# Ordina per punteggio decrescente, poi per percentuale
records_sorted = sorted(
records,
key=lambda r: (r["score"], r["percentage"]),
reverse=True,
)
# KPI sintetici per leggere la classifica a colpo d'occhio
total_quiz = len(records_sorted)
unique_players = len({r["name"] for r in records_sorted})
avg_score = round(sum(r["score"]
for r in records_sorted) / total_quiz, 2)
avg_percentage = round(
sum(float(r["percentage"]) for r in records_sorted) / total_quiz,
1,
)
best = records_sorted[0]
kpi1, kpi2, kpi3, kpi4 = st.columns(4)
kpi1.metric("π₯ Giocatori", unique_players)
kpi2.metric("π§© Quiz completati", total_quiz)
kpi3.metric("π Media punteggio", avg_score)
kpi4.metric("π― Media %", f"{avg_percentage}%")
st.divider()
st.success(
f"Miglior risultato: **{best['name']}** con "
f"**{best['score']} pt** ({best['percentage']}%)"
)
table_rows = []
for rank, r in enumerate(records_sorted, start=1):
medal = {1: "π₯",
2: "π₯",
3: "π₯",
4: "π",
5: "πΌ",
6: "π",
7: "π¦",
8: "π’",
9: "π",
10: "π£", }.get(rank, "π₯")
table_rows.append(
{
"Pos": rank,
"Podio": medal,
"Nome": r["name"],
"Punteggio": r["score"],
"Max": r["max_score"],
"%": float(r["percentage"]),
"Corrette": r["correct"],
"Sbagliate": r["wrong"],
"Categorie": r["categories"],
"Data": r["date"],
}
)
st.subheader("Classifica completa")
st.dataframe(
table_rows,
use_container_width=True,
hide_index=True,
column_config={
"Pos": st.column_config.NumberColumn(width="small"),
"Podio": st.column_config.TextColumn(width="small"),
"Nome": st.column_config.TextColumn(width="medium"),
"Punteggio": st.column_config.NumberColumn(format="%d pt"),
"Max": st.column_config.NumberColumn(format="%d pt"),
"%": st.column_config.NumberColumn(format="%.1f%%"),
"Corrette": st.column_config.NumberColumn(width="small"),
"Sbagliate": st.column_config.NumberColumn(width="small"),
"Categorie": st.column_config.TextColumn(width="large"),
"Data": st.column_config.TextColumn(width="medium"),
},
)
if st.button("β Torna alla Home"):
st.session_state.phase = "home"
st.rerun()
# ---------------------------------------------------------------------------
# FUNZIONI DI SUPPORTO PER LA CLASSIFICA - CON SALVATAGGIO SU SUPABASE
# ---------------------------------------------------------------------------
def _save_result(name: str, results: dict):
"""
Salva un risultato nella tabella Supabase 'results'.
"""
new_record = {
"id": uuid4().hex,
"name": name,
"score": results["score"],
"max_score": results["max_score"],
"percentage": results["percentage"],
"correct": results["correct"],
"wrong": results["wrong"],
"categories": ", ".join(
get_engine().categories.get(cid, {}).get("name", cid)
for cid in st.session_state.get("category_ids", [])
) or "N/A",
"date": datetime.now().strftime("%d/%m/%Y %H:%M"),
"saved_at": datetime.now().isoformat(timespec="seconds"),
}
get_supabase().table("results").insert(new_record).execute()
def _load_results() -> list[dict]:
"""
Legge tutti i risultati dalla tabella Supabase 'results'.
"""
response = get_supabase().table("results").select("*").execute()
return response.data or []
# ---------------------------------------------------------------------------
# ROUTER PRINCIPALE
# ---------------------------------------------------------------------------
def main():
"""
Punto di ingresso dell'app.
Inizializza lo stato e instrada verso la pagina corretta.
"""
init_session_state()
# Mappa fase -> funzione di pagina
pages = {
"home": page_home,
"config": page_config,
"quiz": page_quiz,
"result": page_result,
"leaderboard": page_leaderboard,
}
# Recupera e chiama la funzione della pagina corrente
current_phase = st.session_state.phase
page_fn = pages.get(current_phase, page_home)
page_fn()
# ---------------------------------------------------------------------------
# ENTRY POINT
# ---------------------------------------------------------------------------
main()