From a3d2fe3e333ab2f190fdff75d757051a2bf700fc Mon Sep 17 00:00:00 2001 From: chrishardoc Date: Thu, 21 Dec 2023 19:57:18 +0100 Subject: [PATCH 1/2] ajout d'une database avec les exercices et un memory state --- .gitignore | 2 + app.py | 118 ++++++++++++++----------------- data/exercices_sql_tables.duckdb | Bin 0 -> 536576 bytes init_db.py | 47 ++++++++++++ 4 files changed, 101 insertions(+), 66 deletions(-) create mode 100644 data/exercices_sql_tables.duckdb create mode 100644 init_db.py 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/app.py b/app.py index ce30179..2e3f4e7 100644 --- a/app.py +++ b/app.py @@ -1,83 +1,69 @@ # pylint: disable = missing-module-docstring -import io - 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)) - -CSV2 = """ -food_item,food_price -cookie juice,2.5 -chocolatine,2 -muffin,3 -""" - -food_items = pd.read_csv(io.StringIO(CSV2)) +import duckdb -ANSWER_STR = """ -SELECT * FROM beverages -CROSS JOIN food_items -""" +con = duckdb.connect(database = "data/exercices_sql_tables.duckdb", read_only=False) -solution_df = duckdb.sql(ANSWER_STR).df() +# ANSWER_STR = """ +# SELECT * FROM beverages +# CROSS JOIN food_items +# """ +# +# 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() - 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"]) - -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) - -with tab3: - st.write(ANSWER_STR) +# if query: +# result = duckdb.sql(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"]) +# +# 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) +# +# with tab3: +# st.write(ANSWER_STR) diff --git a/data/exercices_sql_tables.duckdb b/data/exercices_sql_tables.duckdb new file mode 100644 index 0000000000000000000000000000000000000000..89b36a7af6b40175422d75b74315ad3c04798fc5 GIT binary patch literal 536576 zcmeI*&u=4F9RTo`AKPZPi=a}g7PX)pKtZCrNDT}M&JVx+-HkBWc7OHe`pvJ6FZ=b{>+9dB-?~#>A74fe0RjXF z5FkK+009C72oNCf^aTF)ts7r{|M7pW&W-MJX?}FNR00GD5FkK+009C72oNAZV1@*) zym&a+-jVHau2%#I5FkK+009C72oNAZfWRyYy!pG2zW3XI{{H!L+FuWgwJ=`~bM-La z2#c+1v)v4#$gLG=#a9e3#0RlcYt~|3$$z$8=~nM-$NZr5hv9lFwzAIKmAqT+#>XKw zS`SycTd@^mdhz*QZ!?6gqS<=*%%H72R@&7O+0P6bAFB4;?P}hwbmIMH+&z+61Y0ES z?QbdI4;Fg8Mwm~%o^Bjh@5KADQ{9YtF^I&nR3;qhGYxO2lR%2(mkx$^G?c{{-o09c zvT*89t~Ii(UTJn?tBmF9$03<^&Y(Ull_uO39y>&@buSj+?n5FkK+009C72oNAJ zsX#H~kHw5XeJ`f3b6e>s#4y(gVKaOfmTH|W&ntJc$6;x=*{)~1dF9U`{3Xo&HSAwI zC?=t08xM;%<`1<|Ooa#AL9tYPb!2blEIaj<<4NtOtp1}rjW-6S)k!Q<7-g{go{moi;>+!K(dbo~av< zfCvyEK!5;&%OFtxiBgWG{8Od;zLYY`i`uXVCKuiVX=?L1|NFQ)xw+MnM)&YP{B z?N}L9yA$unPIWU@s_lBEk!AG|zLLs)GwrXWy*NHp{;id=M!xde@=8&o((J}ozVhQ2 zQrR!1{aV_Kr~sDary4O zW-X?ia$DTkKidDkwDE Date: Fri, 22 Dec 2023 18:25:30 +0100 Subject: [PATCH 2/2] use user input to display erxercise specifics --- answers/beverages_and_food.sql | 2 ++ app.py | 48 +++++++++++++++---------------- data/exercices_sql_tables.duckdb | Bin 536576 -> 536576 bytes init_db.py | 9 +++--- 4 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 answers/beverages_and_food.sql 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 2e3f4e7..5be97db 100644 --- a/app.py +++ b/app.py @@ -1,23 +1,20 @@ # pylint: disable = missing-module-docstring - +import io +import ast import duckdb +import pandas as pd import streamlit as st -import duckdb +#from init_db import beverages -con = duckdb.connect(database = "data/exercices_sql_tables.duckdb", read_only=False) +con = duckdb.connect(database="data/exercices_sql_tables.duckdb", read_only=False) -# ANSWER_STR = """ -# SELECT * FROM beverages -# CROSS JOIN food_items -# """ -# # solution_df = duckdb.sql(ANSWER_STR).df() with st.sidebar: theme = st.selectbox( "What would you like to review?", - ("cross_joins", "GroupBy", "Windows Functions"), + ("cross_joins", "GroupBy", "windows_functions"), index=None, placeholder="Select a theme...", ) @@ -29,9 +26,9 @@ st.header("enter your code") query = st.text_area(label="votre code SQL ici", key="user_input") -# if query: -# result = duckdb.sql(query).df() -# st.dataframe(result) +if query: + result = con.execute(query).df() + st.dataframe(result) # # # if len(result.columns) != len( #comparaison du nombre de colonnes avec celui attendu # # solution_df.columns @@ -55,15 +52,18 @@ # ) # # -# tab2, tab3 = st.tabs(["Tables", "Solutions"]) -# -# 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) -# -# with tab3: -# st.write(ANSWER_STR) +tab2, tab3 = st.tabs(["Tables", "Solution"]) + +with tab2: + 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: + 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 index 89b36a7af6b40175422d75b74315ad3c04798fc5..95fb3e12ef139f826b57211a437c21987919581c 100644 GIT binary patch delta 165 zcmZp8pwRF@VSxe1 zJs=hYVi3DHGq)foHNHGEFD1V`DYY!MC^0=XJ~1yPJ}o~#g`qv&fCY$IftYQ3x&iyl F{{ZlbIMx6F diff --git a/init_db.py b/init_db.py index 4a084c9..b99e8cb 100644 --- a/init_db.py +++ b/init_db.py @@ -7,22 +7,23 @@ con = duckdb.connect(database = "data/exercices_sql_tables.duckdb", read_only=False) + # -------------------------------------------------------------- -# EXERCICES LIST +# EXERCISES LIST # -------------------------------------------------------------- data = { "theme": ["cross_joins","windows_functions"], - "exercise_name": ["beverage_and_food","simple_window"], + "exercice_name": ["beverages_and_food","simple_window"], "tables": [["beverages", "food_items"],"simple_window"], - "Last_review": ["1970-01-01", "1970-01-01"] + "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 EXERCICES +# CROSS JOIN EXERCISES # -------------------------------------------------------------- CSV = """