-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from chrishardoc/develop
Develop
- Loading branch information
Showing
5 changed files
with
97 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
venv/ | ||
|
||
*.csv | ||
provisoire.py | ||
test_db.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SELECT * FROM beverages | ||
CROSS JOIN coucou |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
|