Spaces:
Sleeping
Sleeping
Victor Rivera commited on
Commit ·
f61f637
1
Parent(s): 6218b8c
Cambios
Browse filesFiles changed (3)
- app.py+127-2
- bike_store.dbAdded
- dataBaseSetup.pyAdded+151
12343456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129import streamlit as stimport pandas as pdfrom dataBaseSetup import create_connection
x = st.slider('Select a value')st.write(x, 'squared is', x * x)def get_stocks_by_category_store(category_name, store_name): conn = create_connection() sql = ''' SELECT categories.category_name, stores.store_name, SUM(stocks.quantity) as total_stock FROM stocks JOIN products ON stocks.product_id = products.product_id JOIN categories ON products.category_id = categories.category_id JOIN stores ON stocks.store_id = stores.store_id WHERE categories.category_name = ? AND stores.store_name = ? GROUP BY categories.category_name, stores.store_name; ''' df = pd.read_sql_query(sql, conn, params=(category_name, store_name)) conn.close() return df
def get_order_items_by_category_store(category_name, store_name): conn = create_connection() sql = ''' SELECT c.category_name, s.store_name, COUNT(oi.item_id) as total_items FROM order_items oi JOIN orders o ON oi.order_id = o.order_id JOIN products p ON oi.product_id = p.product_id JOIN categories c ON p.category_id = c.category_id JOIN stores s ON o.store_id = s.store_id WHERE c.category_name = ? AND s.store_name = ? GROUP BY c.category_name, s.store_name; ''' df = pd.read_sql_query(sql, conn, params=(category_name, store_name)) conn.close() return df
def get_total_sales_by_store_year_month(store_name, year_month): conn = create_connection() sql = ''' SELECT strftime('%Y-%m', o.order_date) as year_month, SUM(oi.quantity * oi.list_price) as total_sales FROM orders o JOIN order_items oi ON o.order_id = oi.order_id WHERE strftime('%Y-%m', o.order_date) = ? AND o.store_id IN ( SELECT store_id FROM stores WHERE store_name = ? ) GROUP BY year_month; ''' df = pd.read_sql_query(sql, conn, params=(year_month, store_name)) conn.close() return df
def get_staff_order_counts(desc=True): conn = create_connection() sql = ''' SELECT s.staff_id, s.first_name || ' ' || s.last_name AS staff_name, COUNT(o.order_id) as order_count FROM orders o JOIN staffs s ON o.staff_id = s.staff_id GROUP BY s.staff_id ORDER BY order_count {} LIMIT 1; '''.format('DESC' if desc else 'ASC') df = pd.read_sql_query(sql, conn) conn.close() return df
# STREAMLIT
import streamlit as st
def app(): st.title("Bike Store Management System")
# Query 1: Stocks by Category and Store st.sidebar.header("Query 1: Get Stocks") category_name_1 = st.text_input("Category Name for Stocks", key='1') store_name_1 = st.text_input("Store Name for Stocks", key='2') if st.sidebar.button("Execute Query 1", key='3'): df = get_stocks_by_category_store(category_name_1, store_name_1) st.write("### Query 1: Stocks by Category and Store") st.write(df)
# Query 2: Order Items by Category and Store st.sidebar.header("Query 2: Get Order Items") category_name_2 = st.text_input("Category Name for Order Items", key='4') store_name_2 = st.text_input("Store Name for Order Items", key='5') if st.sidebar.button("Execute Query 2", key='6'): df = get_order_items_by_category_store(category_name_2, store_name_2) st.write("### Query 2: Order Items by Category and Store") st.write(df)
# Query 3: Total Sales in Santa Cruz Bikes st.sidebar.header("Query 3: Total Sales in Santa Cruz Bikes") year_month_3 = st.text_input("Year-Month (YYYY-MM) for Santa Cruz Bikes", key='7') if st.sidebar.button("Execute Query 3", key='8'): df = get_total_sales_by_store_year_month("Santa Cruz Bikes", year_month_3) st.write("### Query 3: Total Sales in Santa Cruz Bikes") st.write(df)
# Query 4: Total Sales in Baldwin Bikes st.sidebar.header("Query 4: Total Sales in Baldwin Bikes") year_month_4 = st.text_input("Year-Month (YYYY-MM) for Baldwin Bikes", key='9') if st.sidebar.button("Execute Query 4", key='10'): df = get_total_sales_by_store_year_month("Baldwin Bikes", year_month_4) st.write("### Query 4: Total Sales in Baldwin Bikes") st.write(df)
# Query 5: Total Sales in Rowlett Bikes st.sidebar.header("Query 5: Total Sales in Rowlett Bikes") year_month_5 = st.text_input("Year-Month (YYYY-MM) for Rowlett Bikes", key='11') if st.sidebar.button("Execute Query 5", key='12'): df = get_total_sales_by_store_year_month("Rowlett Bikes", year_month_5) st.write("### Query 5: Total Sales in Rowlett Bikes") st.write(df)
# Query 6: Staff with the Highest Number of Orders st.sidebar.header("Query 6: Staff with the Highest Number of Orders") if st.sidebar.button("Execute Query 6", key='13'): df = get_staff_order_counts(desc=True) st.write("### Query 6: Staff with the Highest Number of Orders") st.write(df)
# Query 7: Staff with the Lowest Number of Orders st.sidebar.header("Query 7: Staff with the Lowest Number of Orders") if st.sidebar.button("Execute Query 7", key='14'): df = get_staff_order_counts(desc=False) st.write("### Query 7: Staff with the Lowest Number of Orders") st.write(df)
if __name__ == '__main__': app()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151import sqlite3import pandas as pd
def create_connection(): """ Create a database connection to the SQLite database """ try: conn = sqlite3.connect('bike_store.db') print("Connection established: Database is connected") return conn except Exception as e: print("Error connecting to database:", e) return None
def create_table(conn, create_table_sql): """ Create a table from the create_table_sql statement """ try: c = conn.cursor() c.execute(create_table_sql) print(f"Table created successfully or already exists.") except Exception as e: print(f"Error creating table: {e}")
def import_data_to_table(csv_file, table_name, conn): """ Load data from a CSV file and insert it into the specified table """ try: df = pd.read_csv(csv_file) df.to_sql(table_name, conn, if_exists='append', index=False) print(f"Data imported successfully into {table_name}.") except Exception as e: print(f"Error importing data into {table_name}: {e}")
def main(): # Create a database connection conn = create_connection() if conn is not None: # SQL table creation statements tables_sql = { "customers": """ CREATE TABLE IF NOT EXISTS customers ( customer_id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT NOT NULL, last_name TEXT NOT NULL, phone TEXT, email TEXT, street TEXT, city TEXT, state TEXT, zip_code TEXT ); """, "staffs": """ CREATE TABLE IF NOT EXISTS staffs ( staff_id INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT NOT NULL, last_name TEXT NOT NULL, email TEXT, phone TEXT, active INTEGER, store_id INTEGER, manager_id INTEGER, FOREIGN KEY (store_id) REFERENCES stores(store_id), FOREIGN KEY (manager_id) REFERENCES staffs(staff_id) ); """, "stores": """ CREATE TABLE IF NOT EXISTS stores ( store_id INTEGER PRIMARY KEY AUTOINCREMENT, store_name TEXT NOT NULL, phone TEXT, email TEXT, street TEXT, city TEXT, state TEXT, zip_code TEXT ); """, "categories": """ CREATE TABLE IF NOT EXISTS categories ( category_id INTEGER PRIMARY KEY AUTOINCREMENT, category_name TEXT NOT NULL ); """, "products": """ CREATE TABLE IF NOT EXISTS products ( product_id INTEGER PRIMARY KEY AUTOINCREMENT, product_name TEXT NOT NULL, category_id INTEGER, brand_id INTEGER, model_year INTEGER, list_price REAL, FOREIGN KEY (category_id) REFERENCES categories(category_id), FOREIGN KEY (brand_id) REFERENCES brands(brand_id) ); """, "brands": """ CREATE TABLE IF NOT EXISTS brands ( brand_id INTEGER PRIMARY KEY AUTOINCREMENT, brand_name TEXT NOT NULL ); """, "stocks": """ CREATE TABLE IF NOT EXISTS stocks ( store_id INTEGER, product_id INTEGER, quantity INTEGER, PRIMARY KEY (store_id, product_id), FOREIGN KEY (store_id) REFERENCES stores(store_id), FOREIGN KEY (product_id) REFERENCES products(product_id) ); """, "orders": """ CREATE TABLE IF NOT EXISTS orders ( order_id INTEGER PRIMARY KEY AUTOINCREMENT, customer_id INTEGER, order_status TEXT, order_date TEXT, required_date TEXT, shipped_date TEXT, store_id INTEGER, staff_id INTEGER, FOREIGN KEY (customer_id) REFERENCES customers(customer_id), FOREIGN KEY (store_id) REFERENCES stores(store_id), FOREIGN KEY (staff_id) REFERENCES staffs(staff_id) ); """, "order_items": """ CREATE TABLE IF NOT EXISTS order_items ( order_id INTEGER, item_id INTEGER PRIMARY KEY AUTOINCREMENT, product_id INTEGER, quantity INTEGER, list_price REAL, discount REAL, FOREIGN KEY (order_id) REFERENCES orders(order_id), FOREIGN KEY (product_id) REFERENCES products(product_id) ); """ }
# Create tables for table_name, sql_command in tables_sql.items(): create_table(conn, sql_command)
# Data import paths data_paths = { "customers": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/customers.csv", "staffs": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/staffs.csv", "products": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/products.csv", "categories": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/categories.csv", "stores": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/stores.csv", "brands": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/brands.csv", "stocks": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/stocks.csv", "orders": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/orders.csv", "order_items": "/Users/victorg/Documents/Streamlitapp/InteractiveDBApp/CSV/order_items.csv", }
# Import data to tables for table_name, csv_path in data_paths.items(): import_data_to_table(csv_path, table_name, conn)
# Close the connection pass conn.close() else: print("Failed to create database connection.")
if __name__ == '__main__': main()app.py+127-2
bike_store.dbAdded
dataBaseSetup.pyAdded+151