Spaces:
Sleeping
Sleeping
Victor Rivera
commited on
Commit
·
f61f637
1
Parent(s):
6218b8c
Cambios
Browse files- app.py +127 -2
- bike_store.db +0 -0
- dataBaseSetup.py +151 -0
app.py
CHANGED
|
@@ -1,4 +1,129 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from dataBaseSetup import create_connection
|
| 4 |
|
| 5 |
+
def get_stocks_by_category_store(category_name, store_name):
|
| 6 |
+
conn = create_connection()
|
| 7 |
+
sql = '''
|
| 8 |
+
SELECT categories.category_name, stores.store_name, SUM(stocks.quantity) as total_stock
|
| 9 |
+
FROM stocks
|
| 10 |
+
JOIN products ON stocks.product_id = products.product_id
|
| 11 |
+
JOIN categories ON products.category_id = categories.category_id
|
| 12 |
+
JOIN stores ON stocks.store_id = stores.store_id
|
| 13 |
+
WHERE categories.category_name = ? AND stores.store_name = ?
|
| 14 |
+
GROUP BY categories.category_name, stores.store_name;
|
| 15 |
+
'''
|
| 16 |
+
df = pd.read_sql_query(sql, conn, params=(category_name, store_name))
|
| 17 |
+
conn.close()
|
| 18 |
+
return df
|
| 19 |
+
|
| 20 |
+
def get_order_items_by_category_store(category_name, store_name):
|
| 21 |
+
conn = create_connection()
|
| 22 |
+
sql = '''
|
| 23 |
+
SELECT c.category_name, s.store_name, COUNT(oi.item_id) as total_items
|
| 24 |
+
FROM order_items oi
|
| 25 |
+
JOIN orders o ON oi.order_id = o.order_id
|
| 26 |
+
JOIN products p ON oi.product_id = p.product_id
|
| 27 |
+
JOIN categories c ON p.category_id = c.category_id
|
| 28 |
+
JOIN stores s ON o.store_id = s.store_id
|
| 29 |
+
WHERE c.category_name = ? AND s.store_name = ?
|
| 30 |
+
GROUP BY c.category_name, s.store_name;
|
| 31 |
+
'''
|
| 32 |
+
df = pd.read_sql_query(sql, conn, params=(category_name, store_name))
|
| 33 |
+
conn.close()
|
| 34 |
+
return df
|
| 35 |
+
|
| 36 |
+
def get_total_sales_by_store_year_month(store_name, year_month):
|
| 37 |
+
conn = create_connection()
|
| 38 |
+
sql = '''
|
| 39 |
+
SELECT strftime('%Y-%m', o.order_date) as year_month, SUM(oi.quantity * oi.list_price) as total_sales
|
| 40 |
+
FROM orders o
|
| 41 |
+
JOIN order_items oi ON o.order_id = oi.order_id
|
| 42 |
+
WHERE strftime('%Y-%m', o.order_date) = ? AND o.store_id IN (
|
| 43 |
+
SELECT store_id FROM stores WHERE store_name = ?
|
| 44 |
+
)
|
| 45 |
+
GROUP BY year_month;
|
| 46 |
+
'''
|
| 47 |
+
df = pd.read_sql_query(sql, conn, params=(year_month, store_name))
|
| 48 |
+
conn.close()
|
| 49 |
+
return df
|
| 50 |
+
|
| 51 |
+
def get_staff_order_counts(desc=True):
|
| 52 |
+
conn = create_connection()
|
| 53 |
+
sql = '''
|
| 54 |
+
SELECT s.staff_id, s.first_name || ' ' || s.last_name AS staff_name, COUNT(o.order_id) as order_count
|
| 55 |
+
FROM orders o
|
| 56 |
+
JOIN staffs s ON o.staff_id = s.staff_id
|
| 57 |
+
GROUP BY s.staff_id
|
| 58 |
+
ORDER BY order_count {}
|
| 59 |
+
LIMIT 1;
|
| 60 |
+
'''.format('DESC' if desc else 'ASC')
|
| 61 |
+
df = pd.read_sql_query(sql, conn)
|
| 62 |
+
conn.close()
|
| 63 |
+
return df
|
| 64 |
+
|
| 65 |
+
# STREAMLIT
|
| 66 |
+
|
| 67 |
+
import streamlit as st
|
| 68 |
+
|
| 69 |
+
def app():
|
| 70 |
+
st.title("Bike Store Management System")
|
| 71 |
+
|
| 72 |
+
# Query 1: Stocks by Category and Store
|
| 73 |
+
st.sidebar.header("Query 1: Get Stocks")
|
| 74 |
+
category_name_1 = st.text_input("Category Name for Stocks", key='1')
|
| 75 |
+
store_name_1 = st.text_input("Store Name for Stocks", key='2')
|
| 76 |
+
if st.sidebar.button("Execute Query 1", key='3'):
|
| 77 |
+
df = get_stocks_by_category_store(category_name_1, store_name_1)
|
| 78 |
+
st.write("### Query 1: Stocks by Category and Store")
|
| 79 |
+
st.write(df)
|
| 80 |
+
|
| 81 |
+
# Query 2: Order Items by Category and Store
|
| 82 |
+
st.sidebar.header("Query 2: Get Order Items")
|
| 83 |
+
category_name_2 = st.text_input("Category Name for Order Items", key='4')
|
| 84 |
+
store_name_2 = st.text_input("Store Name for Order Items", key='5')
|
| 85 |
+
if st.sidebar.button("Execute Query 2", key='6'):
|
| 86 |
+
df = get_order_items_by_category_store(category_name_2, store_name_2)
|
| 87 |
+
st.write("### Query 2: Order Items by Category and Store")
|
| 88 |
+
st.write(df)
|
| 89 |
+
|
| 90 |
+
# Query 3: Total Sales in Santa Cruz Bikes
|
| 91 |
+
st.sidebar.header("Query 3: Total Sales in Santa Cruz Bikes")
|
| 92 |
+
year_month_3 = st.text_input("Year-Month (YYYY-MM) for Santa Cruz Bikes", key='7')
|
| 93 |
+
if st.sidebar.button("Execute Query 3", key='8'):
|
| 94 |
+
df = get_total_sales_by_store_year_month("Santa Cruz Bikes", year_month_3)
|
| 95 |
+
st.write("### Query 3: Total Sales in Santa Cruz Bikes")
|
| 96 |
+
st.write(df)
|
| 97 |
+
|
| 98 |
+
# Query 4: Total Sales in Baldwin Bikes
|
| 99 |
+
st.sidebar.header("Query 4: Total Sales in Baldwin Bikes")
|
| 100 |
+
year_month_4 = st.text_input("Year-Month (YYYY-MM) for Baldwin Bikes", key='9')
|
| 101 |
+
if st.sidebar.button("Execute Query 4", key='10'):
|
| 102 |
+
df = get_total_sales_by_store_year_month("Baldwin Bikes", year_month_4)
|
| 103 |
+
st.write("### Query 4: Total Sales in Baldwin Bikes")
|
| 104 |
+
st.write(df)
|
| 105 |
+
|
| 106 |
+
# Query 5: Total Sales in Rowlett Bikes
|
| 107 |
+
st.sidebar.header("Query 5: Total Sales in Rowlett Bikes")
|
| 108 |
+
year_month_5 = st.text_input("Year-Month (YYYY-MM) for Rowlett Bikes", key='11')
|
| 109 |
+
if st.sidebar.button("Execute Query 5", key='12'):
|
| 110 |
+
df = get_total_sales_by_store_year_month("Rowlett Bikes", year_month_5)
|
| 111 |
+
st.write("### Query 5: Total Sales in Rowlett Bikes")
|
| 112 |
+
st.write(df)
|
| 113 |
+
|
| 114 |
+
# Query 6: Staff with the Highest Number of Orders
|
| 115 |
+
st.sidebar.header("Query 6: Staff with the Highest Number of Orders")
|
| 116 |
+
if st.sidebar.button("Execute Query 6", key='13'):
|
| 117 |
+
df = get_staff_order_counts(desc=True)
|
| 118 |
+
st.write("### Query 6: Staff with the Highest Number of Orders")
|
| 119 |
+
st.write(df)
|
| 120 |
+
|
| 121 |
+
# Query 7: Staff with the Lowest Number of Orders
|
| 122 |
+
st.sidebar.header("Query 7: Staff with the Lowest Number of Orders")
|
| 123 |
+
if st.sidebar.button("Execute Query 7", key='14'):
|
| 124 |
+
df = get_staff_order_counts(desc=False)
|
| 125 |
+
st.write("### Query 7: Staff with the Lowest Number of Orders")
|
| 126 |
+
st.write(df)
|
| 127 |
+
|
| 128 |
+
if __name__ == '__main__':
|
| 129 |
+
app()
|
bike_store.db
ADDED
|
Binary file (53.2 kB). View file
|
|
|
dataBaseSetup.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
def create_connection():
|
| 5 |
+
""" Create a database connection to the SQLite database """
|
| 6 |
+
try:
|
| 7 |
+
conn = sqlite3.connect('bike_store.db')
|
| 8 |
+
print("Connection established: Database is connected")
|
| 9 |
+
return conn
|
| 10 |
+
except Exception as e:
|
| 11 |
+
print("Error connecting to database:", e)
|
| 12 |
+
return None
|
| 13 |
+
|
| 14 |
+
def create_table(conn, create_table_sql):
|
| 15 |
+
""" Create a table from the create_table_sql statement """
|
| 16 |
+
try:
|
| 17 |
+
c = conn.cursor()
|
| 18 |
+
c.execute(create_table_sql)
|
| 19 |
+
print(f"Table created successfully or already exists.")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Error creating table: {e}")
|
| 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.to_sql(table_name, conn, if_exists='append', index=False)
|
| 28 |
+
print(f"Data imported successfully into {table_name}.")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error importing data into {table_name}: {e}")
|
| 31 |
+
|
| 32 |
+
def main():
|
| 33 |
+
# Create a database connection
|
| 34 |
+
conn = create_connection()
|
| 35 |
+
if conn is not None:
|
| 36 |
+
# SQL table creation statements
|
| 37 |
+
tables_sql = {
|
| 38 |
+
"customers": """ CREATE TABLE IF NOT EXISTS customers (
|
| 39 |
+
customer_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 40 |
+
first_name TEXT NOT NULL,
|
| 41 |
+
last_name TEXT NOT NULL,
|
| 42 |
+
phone TEXT,
|
| 43 |
+
email TEXT,
|
| 44 |
+
street TEXT,
|
| 45 |
+
city TEXT,
|
| 46 |
+
state TEXT,
|
| 47 |
+
zip_code TEXT
|
| 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 |
+
"products": """ CREATE TABLE IF NOT EXISTS products (
|
| 76 |
+
product_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 77 |
+
product_name TEXT NOT NULL,
|
| 78 |
+
category_id INTEGER,
|
| 79 |
+
brand_id INTEGER,
|
| 80 |
+
model_year INTEGER,
|
| 81 |
+
list_price REAL,
|
| 82 |
+
FOREIGN KEY (category_id) REFERENCES categories(category_id),
|
| 83 |
+
FOREIGN KEY (brand_id) REFERENCES brands(brand_id)
|
| 84 |
+
); """,
|
| 85 |
+
"brands": """ CREATE TABLE IF NOT EXISTS brands (
|
| 86 |
+
brand_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 87 |
+
brand_name TEXT NOT NULL
|
| 88 |
+
); """,
|
| 89 |
+
"stocks": """ CREATE TABLE IF NOT EXISTS stocks (
|
| 90 |
+
store_id INTEGER,
|
| 91 |
+
product_id INTEGER,
|
| 92 |
+
quantity INTEGER,
|
| 93 |
+
PRIMARY KEY (store_id, product_id),
|
| 94 |
+
FOREIGN KEY (store_id) REFERENCES stores(store_id),
|
| 95 |
+
FOREIGN KEY (product_id) REFERENCES products(product_id)
|
| 96 |
+
); """,
|
| 97 |
+
"orders": """ CREATE TABLE IF NOT EXISTS orders (
|
| 98 |
+
order_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 99 |
+
customer_id INTEGER,
|
| 100 |
+
order_status TEXT,
|
| 101 |
+
order_date TEXT,
|
| 102 |
+
required_date TEXT,
|
| 103 |
+
shipped_date TEXT,
|
| 104 |
+
store_id INTEGER,
|
| 105 |
+
staff_id INTEGER,
|
| 106 |
+
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
|
| 107 |
+
FOREIGN KEY (store_id) REFERENCES stores(store_id),
|
| 108 |
+
FOREIGN KEY (staff_id) REFERENCES staffs(staff_id)
|
| 109 |
+
); """,
|
| 110 |
+
"order_items": """ CREATE TABLE IF NOT EXISTS order_items (
|
| 111 |
+
order_id INTEGER,
|
| 112 |
+
item_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 113 |
+
product_id INTEGER,
|
| 114 |
+
quantity INTEGER,
|
| 115 |
+
list_price REAL,
|
| 116 |
+
discount REAL,
|
| 117 |
+
FOREIGN KEY (order_id) REFERENCES orders(order_id),
|
| 118 |
+
FOREIGN KEY (product_id) REFERENCES products(product_id)
|
| 119 |
+
); """
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
# Create tables
|
| 123 |
+
for table_name, sql_command in tables_sql.items():
|
| 124 |
+
create_table(conn, sql_command)
|
| 125 |
+
|
| 126 |
+
# Data import paths
|
| 127 |
+
data_paths = {
|
| 128 |
+
"customers": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/customers.csv",
|
| 129 |
+
"staffs": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/staffs.csv",
|
| 130 |
+
"products": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/products.csv",
|
| 131 |
+
"categories": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/categories.csv",
|
| 132 |
+
"stores": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/stores.csv",
|
| 133 |
+
"brands": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/brands.csv",
|
| 134 |
+
"stocks": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/stocks.csv",
|
| 135 |
+
"orders": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/orders.csv",
|
| 136 |
+
"order_items": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/order_items.csv",
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
# Import data to tables
|
| 140 |
+
for table_name, csv_path in data_paths.items():
|
| 141 |
+
import_data_to_table(csv_path, table_name, conn)
|
| 142 |
+
|
| 143 |
+
# Close the connection
|
| 144 |
+
pass
|
| 145 |
+
conn.close()
|
| 146 |
+
else:
|
| 147 |
+
print("Failed to create database connection.")
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
if __name__ == '__main__':
|
| 151 |
+
main()
|