Spaces:
Sleeping
Sleeping
Victor Rivera
commited on
Commit
·
ec25389
1
Parent(s):
1aeeb14
Cambio de indentacion y agregar visualizaciones
Browse files- app.py +29 -0
- dataBaseSetup.py +107 -106
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import sqlite3
|
|
|
4 |
from dataBaseSetup import create_connection
|
5 |
|
6 |
# Funciones de conexión a la base de datos
|
@@ -93,6 +94,10 @@ def app():
|
|
93 |
if st.button("Execute Query 1", key='3'):
|
94 |
df = get_stocks_by_category_store(category_name_1, store_name_1)
|
95 |
st.write(df)
|
|
|
|
|
|
|
|
|
96 |
|
97 |
elif selected_query == "Query 2: Get Order Items":
|
98 |
st.write("### Query 2: Order Items by Category and Store")
|
@@ -101,6 +106,10 @@ def app():
|
|
101 |
if st.button("Execute Query 2", key='6'):
|
102 |
df = get_order_items_by_category_store(category_name_2, store_name_2)
|
103 |
st.write(df)
|
|
|
|
|
|
|
|
|
104 |
|
105 |
elif selected_query == "Query 3: Total Sales in Santa Cruz Bikes":
|
106 |
st.write("### Query 3: Total Sales in Santa Cruz Bikes")
|
@@ -108,6 +117,10 @@ def app():
|
|
108 |
if st.button("Execute Query 3", key='8'):
|
109 |
df = get_total_sales_by_store_year_month("Santa Cruz Bikes", year_month_3)
|
110 |
st.write(df)
|
|
|
|
|
|
|
|
|
111 |
|
112 |
elif selected_query == "Query 4: Total Sales in Baldwin Bikes":
|
113 |
st.write("### Query 4: Total Sales in Baldwin Bikes")
|
@@ -115,6 +128,10 @@ def app():
|
|
115 |
if st.button("Execute Query 4", key='10'):
|
116 |
df = get_total_sales_by_store_year_month("Baldwin Bikes", year_month_4)
|
117 |
st.write(df)
|
|
|
|
|
|
|
|
|
118 |
|
119 |
elif selected_query == "Query 5: Total Sales in Rowlett Bikes":
|
120 |
st.write("### Query 5: Total Sales in Rowlett Bikes")
|
@@ -122,18 +139,30 @@ def app():
|
|
122 |
if st.button("Execute Query 5", key='12'):
|
123 |
df = get_total_sales_by_store_year_month("Rowlett Bikes", year_month_5)
|
124 |
st.write(df)
|
|
|
|
|
|
|
|
|
125 |
|
126 |
elif selected_query == "Query 6: Staff with the Highest Number of Orders":
|
127 |
st.write("### Query 6: Staff with the Highest Number of Orders")
|
128 |
if st.button("Execute Query 6", key='13'):
|
129 |
df = get_staff_order_counts(desc=True)
|
130 |
st.write(df)
|
|
|
|
|
|
|
|
|
131 |
|
132 |
elif selected_query == "Query 7: Staff with the Lowest Number of Orders":
|
133 |
st.write("### Query 7: Staff with the Lowest Number of Orders")
|
134 |
if st.button("Execute Query 7", key='14'):
|
135 |
df = get_staff_order_counts(desc=False)
|
136 |
st.write(df)
|
|
|
|
|
|
|
|
|
137 |
|
138 |
if __name__ == "__main__":
|
139 |
app()
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import sqlite3
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
from dataBaseSetup import create_connection
|
6 |
|
7 |
# Funciones de conexión a la base de datos
|
|
|
94 |
if st.button("Execute Query 1", key='3'):
|
95 |
df = get_stocks_by_category_store(category_name_1, store_name_1)
|
96 |
st.write(df)
|
97 |
+
if not df.empty:
|
98 |
+
fig, ax = plt.subplots()
|
99 |
+
df.plot(kind='bar', x='category_name', y='total_stock', ax=ax)
|
100 |
+
st.pyplot(fig)
|
101 |
|
102 |
elif selected_query == "Query 2: Get Order Items":
|
103 |
st.write("### Query 2: Order Items by Category and Store")
|
|
|
106 |
if st.button("Execute Query 2", key='6'):
|
107 |
df = get_order_items_by_category_store(category_name_2, store_name_2)
|
108 |
st.write(df)
|
109 |
+
if not df.empty:
|
110 |
+
fig, ax = plt.subplots()
|
111 |
+
df.plot(kind='bar', x='category_name', y='total_items', ax=ax)
|
112 |
+
st.pyplot(fig)
|
113 |
|
114 |
elif selected_query == "Query 3: Total Sales in Santa Cruz Bikes":
|
115 |
st.write("### Query 3: Total Sales in Santa Cruz Bikes")
|
|
|
117 |
if st.button("Execute Query 3", key='8'):
|
118 |
df = get_total_sales_by_store_year_month("Santa Cruz Bikes", year_month_3)
|
119 |
st.write(df)
|
120 |
+
if not df.empty:
|
121 |
+
fig, ax = plt.subplots()
|
122 |
+
df.plot(kind='bar', x='year_month', y='total_sales', ax=ax)
|
123 |
+
st.pyplot(fig)
|
124 |
|
125 |
elif selected_query == "Query 4: Total Sales in Baldwin Bikes":
|
126 |
st.write("### Query 4: Total Sales in Baldwin Bikes")
|
|
|
128 |
if st.button("Execute Query 4", key='10'):
|
129 |
df = get_total_sales_by_store_year_month("Baldwin Bikes", year_month_4)
|
130 |
st.write(df)
|
131 |
+
if not df.empty:
|
132 |
+
fig, ax = plt.subplots()
|
133 |
+
df.plot(kind='bar', x='year_month', y='total_sales', ax=ax)
|
134 |
+
st.pyplot(fig)
|
135 |
|
136 |
elif selected_query == "Query 5: Total Sales in Rowlett Bikes":
|
137 |
st.write("### Query 5: Total Sales in Rowlett Bikes")
|
|
|
139 |
if st.button("Execute Query 5", key='12'):
|
140 |
df = get_total_sales_by_store_year_month("Rowlett Bikes", year_month_5)
|
141 |
st.write(df)
|
142 |
+
if not df.empty:
|
143 |
+
fig, ax = plt.subplots()
|
144 |
+
df.plot(kind='bar', x='year_month', y='total_sales', ax=ax)
|
145 |
+
st.pyplot(fig)
|
146 |
|
147 |
elif selected_query == "Query 6: Staff with the Highest Number of Orders":
|
148 |
st.write("### Query 6: Staff with the Highest Number of Orders")
|
149 |
if st.button("Execute Query 6", key='13'):
|
150 |
df = get_staff_order_counts(desc=True)
|
151 |
st.write(df)
|
152 |
+
if not df.empty:
|
153 |
+
fig, ax = plt.subplots()
|
154 |
+
df.plot(kind='bar', x='staff_name', y='order_count', ax=ax)
|
155 |
+
st.pyplot(fig)
|
156 |
|
157 |
elif selected_query == "Query 7: Staff with the Lowest Number of Orders":
|
158 |
st.write("### Query 7: Staff with the Lowest Number of Orders")
|
159 |
if st.button("Execute Query 7", key='14'):
|
160 |
df = get_staff_order_counts(desc=False)
|
161 |
st.write(df)
|
162 |
+
if not df.empty:
|
163 |
+
fig, ax = plt.subplots()
|
164 |
+
df.plot(kind='bar', x='staff_name', y='order_count', ax=ax)
|
165 |
+
st.pyplot(fig)
|
166 |
|
167 |
if __name__ == "__main__":
|
168 |
app()
|
dataBaseSetup.py
CHANGED
@@ -16,15 +16,17 @@ def create_table(conn, create_table_sql):
|
|
16 |
try:
|
17 |
c = conn.cursor()
|
18 |
c.execute(create_table_sql)
|
19 |
-
|
|
|
20 |
except Exception as e:
|
21 |
-
print(
|
22 |
|
23 |
def import_data_to_table(csv_file, table_name, conn):
|
24 |
""" Load data from a CSV file and insert it into the specified table """
|
25 |
try:
|
26 |
df = pd.read_csv(csv_file)
|
27 |
-
df
|
|
|
28 |
print(f"Data imported successfully into {table_name}.")
|
29 |
except Exception as e:
|
30 |
print(f"Error importing data into {table_name}: {e}")
|
@@ -32,120 +34,119 @@ def import_data_to_table(csv_file, table_name, conn):
|
|
32 |
def main():
|
33 |
# Create a database connection
|
34 |
conn = create_connection()
|
|
|
35 |
if conn is not None:
|
36 |
-
|
37 |
tables_sql = {
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
); """,
|
49 |
-
"staffs": """ CREATE TABLE IF NOT EXISTS staffs (
|
50 |
-
staff_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
51 |
-
first_name TEXT NOT NULL,
|
52 |
-
last_name TEXT NOT NULL,
|
53 |
-
email TEXT,
|
54 |
-
phone TEXT,
|
55 |
-
active INTEGER,
|
56 |
-
store_id INTEGER,
|
57 |
-
manager_id INTEGER,
|
58 |
-
FOREIGN KEY (store_id) REFERENCES stores(store_id),
|
59 |
-
FOREIGN KEY (manager_id) REFERENCES staffs(staff_id)
|
60 |
-
); """,
|
61 |
-
"stores": """ CREATE TABLE IF NOT EXISTS stores (
|
62 |
-
store_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
63 |
-
store_name TEXT NOT NULL,
|
64 |
-
phone TEXT,
|
65 |
-
email TEXT,
|
66 |
-
street TEXT,
|
67 |
-
city TEXT,
|
68 |
-
state TEXT,
|
69 |
-
zip_code TEXT
|
70 |
-
); """,
|
71 |
-
"categories": """ CREATE TABLE IF NOT EXISTS categories (
|
72 |
-
category_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
73 |
-
category_name TEXT NOT NULL
|
74 |
); """,
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
}
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
|
143 |
-
|
144 |
-
pass
|
145 |
conn.close()
|
146 |
else:
|
147 |
print("Failed to create database connection.")
|
148 |
|
149 |
-
|
150 |
if __name__ == '__main__':
|
151 |
main()
|
|
|
16 |
try:
|
17 |
c = conn.cursor()
|
18 |
c.execute(create_table_sql)
|
19 |
+
conn.commit()
|
20 |
+
print("Table created successfully or already exists.")
|
21 |
except Exception as e:
|
22 |
+
print("Error creating table:", e)
|
23 |
|
24 |
def import_data_to_table(csv_file, table_name, conn):
|
25 |
""" Load data from a CSV file and insert it into the specified table """
|
26 |
try:
|
27 |
df = pd.read_csv(csv_file)
|
28 |
+
print(df)
|
29 |
+
df.to_sql(table_name, conn, if_exists='replace', index=False)
|
30 |
print(f"Data imported successfully into {table_name}.")
|
31 |
except Exception as e:
|
32 |
print(f"Error importing data into {table_name}: {e}")
|
|
|
34 |
def main():
|
35 |
# Create a database connection
|
36 |
conn = create_connection()
|
37 |
+
print(conn)
|
38 |
if conn is not None:
|
39 |
+
# SQL table creation statements
|
40 |
tables_sql = {
|
41 |
+
"customers": """ CREATE TABLE IF NOT EXISTS customers (
|
42 |
+
customer_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
43 |
+
first_name TEXT NOT NULL,
|
44 |
+
last_name TEXT NOT NULL,
|
45 |
+
phone TEXT,
|
46 |
+
email TEXT,
|
47 |
+
street TEXT,
|
48 |
+
city TEXT,
|
49 |
+
state TEXT,
|
50 |
+
zip_code INTEGER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
); """,
|
52 |
+
"staffs": """ CREATE TABLE IF NOT EXISTS staffs (
|
53 |
+
staff_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
54 |
+
first_name TEXT NOT NULL,
|
55 |
+
last_name TEXT NOT NULL,
|
56 |
+
email TEXT,
|
57 |
+
phone TEXT,
|
58 |
+
active INTEGER,
|
59 |
+
store_id INTEGER,
|
60 |
+
manager_id INTEGER,
|
61 |
+
FOREIGN KEY (store_id) REFERENCES stores(store_id),
|
62 |
+
FOREIGN KEY (manager_id) REFERENCES staffs(staff_id)
|
63 |
+
); """,
|
64 |
+
"stores": """ CREATE TABLE IF NOT EXISTS stores (
|
65 |
+
store_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
66 |
+
store_name TEXT NOT NULL,
|
67 |
+
phone TEXT,
|
68 |
+
email TEXT,
|
69 |
+
street TEXT,
|
70 |
+
city TEXT,
|
71 |
+
state TEXT,
|
72 |
+
zip_code TEXT
|
73 |
+
); """,
|
74 |
+
"categories": """ CREATE TABLE IF NOT EXISTS categories (
|
75 |
+
category_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
76 |
+
category_name TEXT NOT NULL
|
77 |
+
); """,
|
78 |
+
"products": """ CREATE TABLE IF NOT EXISTS products (
|
79 |
+
product_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
80 |
+
product_name TEXT NOT NULL,
|
81 |
+
category_id INTEGER,
|
82 |
+
brand_id INTEGER,
|
83 |
+
model_year INTEGER,
|
84 |
+
list_price REAL,
|
85 |
+
FOREIGN KEY (category_id) REFERENCES categories(category_id),
|
86 |
+
FOREIGN KEY (brand_id) REFERENCES brands(brand_id)
|
87 |
+
); """,
|
88 |
+
"brands": """ CREATE TABLE IF NOT EXISTS brands (
|
89 |
+
brand_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
90 |
+
brand_name TEXT NOT NULL
|
91 |
+
); """,
|
92 |
+
"stocks": """ CREATE TABLE IF NOT EXISTS stocks (
|
93 |
+
store_id INTEGER,
|
94 |
+
product_id INTEGER,
|
95 |
+
quantity INTEGER,
|
96 |
+
PRIMARY KEY (store_id, product_id),
|
97 |
+
FOREIGN KEY (store_id) REFERENCES stores(store_id),
|
98 |
+
FOREIGN KEY (product_id) REFERENCES products(product_id)
|
99 |
+
); """,
|
100 |
+
"orders": """ CREATE TABLE IF NOT EXISTS orders (
|
101 |
+
order_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
102 |
+
customer_id INTEGER,
|
103 |
+
order_status TEXT,
|
104 |
+
order_date TEXT,
|
105 |
+
required_date TEXT,
|
106 |
+
shipped_date TEXT,
|
107 |
+
store_id INTEGER,
|
108 |
+
staff_id INTEGER,
|
109 |
+
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
|
110 |
+
FOREIGN KEY (store_id) REFERENCES stores(store_id),
|
111 |
+
FOREIGN KEY (staff_id) REFERENCES staffs(staff_id)
|
112 |
+
); """,
|
113 |
+
"order_items": """ CREATE TABLE IF NOT EXISTS order_items (
|
114 |
+
order_id INTEGER,
|
115 |
+
item_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
116 |
+
product_id INTEGER,
|
117 |
+
quantity INTEGER,
|
118 |
+
list_price REAL,
|
119 |
+
discount REAL,
|
120 |
+
FOREIGN KEY (order_id) REFERENCES orders(order_id),
|
121 |
+
FOREIGN KEY (product_id) REFERENCES products(product_id)
|
122 |
+
); """
|
123 |
}
|
124 |
|
125 |
+
# Create tables
|
126 |
+
for table_name, sql_command in tables_sql.items():
|
127 |
+
create_table(conn, sql_command)
|
128 |
|
129 |
+
# Data import paths
|
130 |
+
data_paths = {
|
131 |
+
"customers": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/customers.csv",
|
132 |
+
"staffs": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/staffs.csv",
|
133 |
+
"products": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/products.csv",
|
134 |
+
"categories": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/categories.csv",
|
135 |
+
"stores": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/stores.csv",
|
136 |
+
"brands": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/brands.csv",
|
137 |
+
"stocks": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/stocks.csv",
|
138 |
+
"orders": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/orders.csv",
|
139 |
+
"order_items": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/order_items.csv",
|
140 |
+
}
|
141 |
|
142 |
+
# Import data to tables
|
143 |
+
for table_name, csv_path in data_paths.items():
|
144 |
+
import_data_to_table(csv_path, table_name, conn)
|
145 |
|
146 |
+
# Close the connection
|
|
|
147 |
conn.close()
|
148 |
else:
|
149 |
print("Failed to create database connection.")
|
150 |
|
|
|
151 |
if __name__ == '__main__':
|
152 |
main()
|