diff --git a/.gitignore b/.gitignore index eed6322..bb109f4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ venv/ *.csv +provisoire.py +test_db.py diff --git a/answers/beverages_and_food.sql b/answers/beverages_and_food.sql new file mode 100644 index 0000000..bcee673 --- /dev/null +++ b/answers/beverages_and_food.sql @@ -0,0 +1,2 @@ +SELECT * FROM beverages +CROSS JOIN coucou \ No newline at end of file diff --git a/app.py b/app.py index ce30179..5be97db 100644 --- a/app.py +++ b/app.py @@ -1,83 +1,69 @@ # pylint: disable = missing-module-docstring - import io - +import ast import duckdb import pandas as pd import streamlit as st -CSV = """ -beverage,price -orange juice,2.5 -Expresso,2 -Tea,3 -""" - -beverages = pd.read_csv(io.StringIO(CSV)) +#from init_db import beverages -CSV2 = """ -food_item,food_price -cookie juice,2.5 -chocolatine,2 -muffin,3 -""" +con = duckdb.connect(database="data/exercices_sql_tables.duckdb", read_only=False) -food_items = pd.read_csv(io.StringIO(CSV2)) - -ANSWER_STR = """ -SELECT * FROM beverages -CROSS JOIN food_items -""" - -solution_df = duckdb.sql(ANSWER_STR).df() +# solution_df = duckdb.sql(ANSWER_STR).df() with st.sidebar: - option = st.selectbox( + theme = st.selectbox( "What would you like to review?", - ("Joins", "GroupBy", "Windows Functions"), + ("cross_joins", "GroupBy", "windows_functions"), index=None, placeholder="Select a theme...", ) - st.write("You selected:", option) + st.write("You selected:", theme) + + exercice = con.execute(f"SELECT * FROM memory_state WHERE theme = '{theme}'").df() + st.write(exercice) st.header("enter your code") query = st.text_area(label="votre code SQL ici", key="user_input") if query: - result = duckdb.sql(query).df() + result = con.execute(query).df() st.dataframe(result) - - # if len(result.columns) != len( #comparaison du nombre de colonnes avec celui attendu - # solution_df.columns - # ): # replace with try result = result(solution_df.columns) - # st.write("Some columns are missing depuis if") - - try: - result = result[solution_df.columns] - st.dataframe(result.compare(solution_df)) - # comparaison deux dataframes, avec la méthode compare de pandas, mais cela ne fonctionne - # que sur des dataframes ayant le même nombre de colonnes et dans le même ordre - except KeyError as e: - st.write("Some columns are missing depuis except keyerror") - - n_line_difference = ( - result.shape[0] - solution_df.shape[0] - ) # comparaison du nombre de lignes avec celui attendu - if n_line_difference != 0: - st.write( - f"result has a {n_line_difference} lines difference with the solution_df" - ) - - -tab2, tab3 = st.tabs(["Tables", "Solutions"]) +# +# # if len(result.columns) != len( #comparaison du nombre de colonnes avec celui attendu +# # solution_df.columns +# # ): # replace with try result = result(solution_df.columns) +# # st.write("Some columns are missing depuis if") +# +# try: +# result = result[solution_df.columns] +# st.dataframe(result.compare(solution_df)) +# # comparaison deux dataframes, avec la méthode compare de pandas, mais cela ne fonctionne +# # que sur des dataframes ayant le même nombre de colonnes et dans le même ordre +# except KeyError as e: +# st.write("Some columns are missing depuis except keyerror") +# +# n_line_difference = ( +# result.shape[0] - solution_df.shape[0] +# ) # comparaison du nombre de lignes avec celui attendu +# if n_line_difference != 0: +# st.write( +# f"result has a {n_line_difference} lines difference with the solution_df" +# ) +# +# +tab2, tab3 = st.tabs(["Tables", "Solution"]) with tab2: - st.write("table : beverages") - st.dataframe(beverages) - st.write("table : food_items") - st.dataframe(food_items) - st.write("expected") - st.dataframe(solution_df) + exercice_tables = ast.literal_eval(exercice.loc[0,"tables"]) + for table in exercice_tables: + st.write(f"table: {table}") + df_table=con.execute(f"SELECT * FROM {table}").df() + st.dataframe(df_table) with tab3: - st.write(ANSWER_STR) + exercice_name = exercice.loc[0, "exercice_name"] + with open(f"answers/{exercice_name}.sql", "r") as f: + answer = f.read() + st.write(answer) + diff --git a/data/exercices_sql_tables.duckdb b/data/exercices_sql_tables.duckdb new file mode 100644 index 0000000..95fb3e1 Binary files /dev/null and b/data/exercices_sql_tables.duckdb differ diff --git a/init_db.py b/init_db.py new file mode 100644 index 0000000..b99e8cb --- /dev/null +++ b/init_db.py @@ -0,0 +1,48 @@ +# pylint: disable = missing-module-docstring + +import io + +import duckdb +import pandas as pd + +con = duckdb.connect(database = "data/exercices_sql_tables.duckdb", read_only=False) + + +# -------------------------------------------------------------- +# EXERCISES LIST +# -------------------------------------------------------------- + +data = { + "theme": ["cross_joins","windows_functions"], + "exercice_name": ["beverages_and_food","simple_window"], + "tables": [["beverages", "food_items"],"simple_window"], + "Last_review": ["1970-01-01", "1970-01-01"], +} +memory_state_df = pd.DataFrame(data) +con.execute("CREATE TABLE IF NOT EXISTS memory_state AS SELECT * FROM memory_state_df") + + +# -------------------------------------------------------------- +# CROSS JOIN EXERCISES +# -------------------------------------------------------------- + +CSV = """ +beverage,price +orange juice,2.5 +Expresso,2 +Tea,3 +""" + +beverages = pd.read_csv(io.StringIO(CSV)) +con.execute("CREATE TABLE IF NOT EXISTS beverages AS SELECT * FROM beverages") + +CSV2 = """ +food_item,food_price +cookie juice,2.5 +chocolatine,2 +muffin,3 +""" + +food_items = pd.read_csv(io.StringIO(CSV2)) +con.execute("CREATE TABLE IF NOT EXISTS food_items AS SELECT * FROM food_items") +