-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
207 lines (164 loc) · 6.25 KB
/
Copy pathapp.py
File metadata and controls
207 lines (164 loc) · 6.25 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
"""
app.py - Point d'entrée de SQLingo.
"""
import streamlit as st
from db import get_connection, init_db
from utils import (
check_users_solution,
get_current_exercise,
init_session,
load_exercises,
load_solution,
record_result,
show_question,
show_tables,
)
# ------------------------------------------------------------
# CONFIG
# ------------------------------------------------------------
st.set_page_config(
page_title="SQLingo",
page_icon="😎",
layout="wide",
)
# ------------------------------------------------------------
# INITIALISATION DB
# ------------------------------------------------------------
init_db()
con = get_connection()
# ------------------------------------------------------------
# ÉCRAN D'ACCUEIL
# ------------------------------------------------------------
def render_home() -> None:
st.title("😎 SQLingo")
st.markdown("Entraîne-toi au SQL avec un système de répétition espacée.")
st.divider()
exercises = load_exercises()
all_themes = sorted({e["theme"] for e in exercises})
st.subheader("Thèmes")
selected_themes = []
cols = st.columns(3)
for i, theme in enumerate(all_themes):
with cols[i % 3]:
if st.checkbox(
theme.replace("_", " ").title(), value=True, key=f"theme_{theme}"
):
selected_themes.append(theme)
st.divider()
n_selected = sum(1 for e in exercises if e["theme"] in selected_themes)
st.caption(f"{n_selected} exercice(s) sélectionné(s)")
if st.button("🚀 Démarrer", disabled=not selected_themes, type="primary"):
init_session(themes=selected_themes)
st.rerun()
# ------------------------------------------------------------
# ÉCRAN EXERCICE
# ------------------------------------------------------------
def render_exercise() -> None:
exercise = get_current_exercise()
if exercise is None:
st.session_state["session_started"] = False
st.session_state["session_finished"] = True
st.rerun()
return
score = st.session_state["score"]
queue = st.session_state["queue"]
total = score["correct"] + score["incorrect"] + len(queue)
done = score["correct"] + score["incorrect"]
# --- Sidebar ---
with st.sidebar:
st.markdown("## 😎 SQLingo")
st.divider()
st.metric("✅ Correct", score["correct"])
st.metric("❌ À revoir", score["incorrect"])
st.progress(done / total if total else 0)
st.caption(f"{done} / {total} exercices")
st.divider()
if st.button("🏠 Accueil", use_container_width=True):
st.session_state["session_started"] = False
st.rerun()
st.divider()
st.markdown(
"Made with 🍜 by "
"<a href='https://www.linkedin.com/in/fabien-hos/' target='_blank'>Fabien</a>"
"<br><a href='https://github.com/surybang/SQLingo' target='_blank'>"
"<img src='https://cdn.simpleicons.org/github' width='16' "
"style='vertical-align:middle; margin-right:4px;'>Voir sur GitHub</a>",
unsafe_allow_html=True,
)
# --- Contenu principal ---
theme_label = exercise["theme"].replace("_", " ").title()
st.markdown(f"**Thème :** {theme_label} · **Exercice :** `{exercise['name']}`")
st.divider()
col_question, col_tables = st.columns([3, 2])
with col_question:
st.subheader("Question")
show_question(exercise)
st.subheader("Ta requête")
user_query = st.text_area(
label="Saisir ta requête SQL :",
height=200,
key=f"query_{exercise['name']}",
label_visibility="collapsed",
)
if st.button("✅ Valider", type="primary", disabled=not user_query):
try:
solution_df = load_solution(con, exercise)
except FileNotFoundError:
st.error(f"Fichier solution introuvable pour `{exercise['name']}`.")
return
correct = check_users_solution(con, solution_df, user_query)
record_result(exercise, correct)
if correct:
st.success("Bonne réponse ! Exercice suivant →")
if st.button("Continuer →"):
st.rerun()
else:
st.error("Pas tout à fait - l'exercice reviendra un peu plus tard.")
if st.button("Continuer →"):
st.rerun()
with col_tables:
st.subheader("Tables disponibles")
show_tables(con, exercise)
# --- Onglet solution ---
with st.expander("Voir la solution"):
try:
solution_df = load_solution(con, exercise)
from config import ANSWERS_PATH
sql = (ANSWERS_PATH / exercise["theme"] / exercise["answer"]).read_text()
st.code(sql, language="sql")
st.dataframe(solution_df)
except FileNotFoundError:
st.warning("Fichier solution introuvable.")
# ------------------------------------------------------------
# ÉCRAN DE FIN
# ------------------------------------------------------------
def render_end() -> None:
score = st.session_state["score"]
history = st.session_state["history"]
total = score["correct"] + score["incorrect"]
st.title("🎉 Session terminée !")
st.divider()
col1, col2, col3 = st.columns(3)
col1.metric("Total", total)
col2.metric("✅ Corrects", score["correct"])
col3.metric("❌ À revoir", score["incorrect"])
errors = [(ex, r) for ex, r in history if r == "incorrect"]
if errors:
st.divider()
st.subheader("Exercices à retravailler")
for ex, _ in errors:
st.markdown(f"- **{ex['theme']}** · `{ex['name']}`")
st.divider()
if st.button("🔄 Recommencer", type="primary"):
for key in ["session_started", "session_finished", "queue", "score", "history"]:
st.session_state.pop(key, None)
st.rerun()
# ------------------------------------------------------------
# ROUTEUR
# ------------------------------------------------------------
if st.session_state.get("session_finished"):
render_end()
elif st.session_state.get("session_started"):
render_exercise()
else:
render_home()