Skip to content

Commit

Permalink
Merge pull request #12 from chrishardoc/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
chrishardoc authored Dec 22, 2023
2 parents 44eedd4 + 9dec4bf commit a3bfe84
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
venv/

*.csv
provisoire.py
test_db.py

2 changes: 2 additions & 0 deletions answers/beverages_and_food.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT * FROM beverages
CROSS JOIN coucou
104 changes: 45 additions & 59 deletions app.py
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 added data/exercices_sql_tables.duckdb
Binary file not shown.
48 changes: 48 additions & 0 deletions init_db.py
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")

0 comments on commit a3bfe84

Please sign in to comment.