From 2c262ad6aa558ce2ed1a28ebeb123e84f22c8dd4 Mon Sep 17 00:00:00 2001 From: Habib Date: Thu, 24 Oct 2024 01:33:08 +0200 Subject: [PATCH] Verification app.py pour PEP8 --- app.py | 82 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/app.py b/app.py index b950478..8b3d269 100644 --- a/app.py +++ b/app.py @@ -1,15 +1,18 @@ # pylint: disable = missing-module-docstring -import io -import ast +# import io +# import ast +# from encodings.mac_greek import encoding_table -import numpy as np 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 -from datetime import date, timedelta + if "data" not in os.listdir(): print("creating folder data") @@ -18,8 +21,10 @@ os.mkdir("data") if "exercises_sql_tables.duckdb" not in os.listdir("data"): - exec(open("init_db.py").read()) - # subprocess.run(["python", "init_db.py"] + # 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) @@ -28,7 +33,8 @@ select * from beverages cross join food_items""" -def check_users_solution(user_query: str) -> None: + +def check_users_solution(user_query: str, correct_solution_df: pd.DataFrame) -> None: """ 1: checking columns 2: checking the value @@ -38,14 +44,14 @@ def check_users_solution(user_query: str) -> None: st.dataframe(result) try: - result = result[solution_df.columns] - st.dataframe(result.compare(solution_df)) - if result.compare(solution_df).shape == (0, 0): + 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 as e: + except KeyError: st.write("Some columns are missing") - n_lines_difference = result.shape[0] - solution_df.shape[0] + 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 " @@ -53,8 +59,7 @@ def check_users_solution(user_query: str) -> None: ) - -with st.sidebar : +with st.sidebar: available_themes_df = con.execute("SELECT DISTINCT theme FROM memory_state").df() theme = st.selectbox( "What would you like to review ?", @@ -64,16 +69,19 @@ def check_users_solution(user_query: str) -> None: ) if theme: st.write(f"You selected {theme}") - select_exercise_query = f"SELECT * FROM memory_state WHERE theme = '{theme}'" + SELECT_EXERCISE_QUERY = f"SELECT * FROM memory_state WHERE theme = '{theme}'" else: - select_exercise_query = f"SELECT * FROM memory_state" + 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) + 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) @@ -82,22 +90,22 @@ def check_users_solution(user_query: str) -> None: exercise_name = exercices.loc[0, "exercise_name"] # Chemin du fichier SQL - file_path = f"answers/{exercise_name}.sql" + FILE_PATH = f"answers/{exercise_name}.sql" # Vérifier si le fichier existe - if os.path.exists(file_path): + if os.path.exists(FILE_PATH): # Lire et exécuter le fichier SQL si trouvé - with open(file_path, "r") as f: - answer = f.read() - solution_df = con.execute(answer).df() + 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 + st.error(f"File not found: {FILE_PATH}") + ANSWER = None else: st.write("No exercises found for this theme.") - answer = None + ANSWER = None # Champ de texte pour entrer une requête SQL st.header("Enter your code:") @@ -105,19 +113,25 @@ def check_users_solution(user_query: str) -> None: query = form.text_area(label="votre code SQL ici", key="user_input") form.form_submit_button("Submit") -if query: - check_users_solution(query) + +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}'" + f"UPDATE memory_state SET last_reviewed = '{next_review}' \ + WHERE exercise_name = '{exercise_name}'" ) + st.rerun() if st.button("Reset"): - con.execute(f"UPDATE memory_state SET last_reviewed = '1970-01-01'") + con.execute("UPDATE memory_state SET last_reviewed = '1970-01-01'") st.rerun() tab2, tab3 = st.tabs(["Tables", "Solution"]) @@ -125,7 +139,7 @@ def check_users_solution(user_query: str) -> None: 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"]) + # 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"] @@ -148,4 +162,4 @@ def check_users_solution(user_query: str) -> None: # with tab3: - st.write(answer) + st.write(ANSWER)