-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
165 lines (133 loc) · 5.1 KB
/
app.py
File metadata and controls
165 lines (133 loc) · 5.1 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
# pylint: disable = missing-module-docstring
# import io
# import ast
# from encodings.mac_greek import encoding_table
import os
import logging
from datetime import date, timedelta
import subprocess
import duckdb
import pandas as pd
import numpy as np
import streamlit as st
if "data" not in os.listdir():
print("creating folder data")
logging.error(os.listdir())
logging.error("creating folder data")
os.mkdir("data")
if "exercises_sql_tables.duckdb" not in os.listdir("data"):
# with open("init_db.py", encoding="utf-8") as f:
# code = f.read()
# exec(code, {"__name__": "__main__"}) # Limiter le contexte d'exécution
subprocess.run(["python", "init_db.py"], check=True)
con = duckdb.connect(database="data/exercises_sql_tables.duckdb", read_only=False)
ANSWER_STR = """
select * from beverages
cross join food_items"""
def check_users_solution(user_query: str, correct_solution_df: pd.DataFrame) -> None:
"""
1: checking columns
2: checking the value
:param user_query: a string containing the query inserted by the user
"""
result = con.execute(user_query).df()
st.dataframe(result)
try:
result = result[correct_solution_df.columns]
st.dataframe(result.compare(correct_solution_df))
if result.compare(correct_solution_df).shape == (0, 0):
st.write("Correct !")
st.balloons()
except KeyError:
st.write("Some columns are missing")
n_lines_difference = result.shape[0] - correct_solution_df.shape[0]
if n_lines_difference != 0:
st.write(
f"result has a {n_lines_difference} lines difference with "
f"the solution_df"
)
with st.sidebar:
available_themes_df = con.execute("SELECT DISTINCT theme FROM memory_state").df()
theme = st.selectbox(
"What would you like to review ?",
available_themes_df["theme"].unique(),
index=None,
placeholder="Select a theme...",
)
if theme:
st.write(f"You selected {theme}")
SELECT_EXERCISE_QUERY = f"SELECT * FROM memory_state WHERE theme = '{theme}'"
else:
SELECT_EXERCISE_QUERY = f"SELECT * FROM memory_state"
# Récupérer les exercices correspondant au thème sélectionné
exercices = (
con.execute(f"select * from memory_state where theme = '{theme}'")
.df()
.sort_values("last_reviewed")
.reset_index(drop=True)
)
solution_df = pd.DataFrame() # Initialisation à un DataFrame vide
# Vérifier si le DataFrame n'est pas vide avant d'accéder aux données
if not exercices.empty:
st.write(exercices)
# Récupérer le nom de l'exercice
exercise_name = exercices.loc[0, "exercise_name"]
# Chemin du fichier SQL
FILE_PATH = f"answers/{exercise_name}.sql"
# Vérifier si le fichier existe
if os.path.exists(FILE_PATH):
# Lire et exécuter le fichier SQL si trouvé
with open(FILE_PATH, "r", encoding="utf-8") as f:
ANSWER = f.read()
solution_df = con.execute(ANSWER).df()
else:
# Message d'erreur si le fichier est introuvable
st.error(f"File not found: {FILE_PATH}")
ANSWER = None
else:
st.write("No exercises found for this theme.")
ANSWER = None
# Champ de texte pour entrer une requête SQL
st.header("Enter your code:")
form = st.form("my_form")
query = form.text_area(label="votre code SQL ici", key="user_input")
form.form_submit_button("Submit")
if query and not solution_df.empty:
check_users_solution(query, solution_df)
else:
st.error("Solution DataFrame not found.")
for n_days in [2, 7, 21]:
if st.button(f"Revoir dans {n_days} jours"):
next_review = date.today() + timedelta(days=n_days)
con.execute(
f"UPDATE memory_state SET last_reviewed = '{next_review}' \
WHERE exercise_name = '{exercise_name}'"
)
st.rerun()
if st.button("Reset"):
con.execute("UPDATE memory_state SET last_reviewed = '1970-01-01'")
st.rerun()
tab2, tab3 = st.tabs(["Tables", "Solution"])
with tab2:
if not exercices.empty:
try:
# Vérifier que l'index existe avant d'y accéder
# exercice_tables = ast.literal_eval(exercices.loc[0, "tables"])
# Utiliser directement la valeur sans passer par ast.literal_eval
exercice_tables = exercices.loc[0, "tables"]
# Si exercice_tables est un tableau numpy ou une liste
if isinstance(exercice_tables, (list, pd.Series, np.ndarray)):
# Afficher chaque table
for table in exercice_tables:
st.write(f"table: {table}")
df_table = con.execute(f"select * from {table}").df()
st.dataframe(df_table)
else:
st.write(f"Unexpected format for tables: {exercice_tables}")
except KeyError:
st.write("The 'tables' column or index 0 was not found in the data.")
else:
st.write("No tables found to display.")
#
with tab3:
st.write(ANSWER)