import streamlit as st import json import yaml # Load logged-in user data from YAML file def load_logged_in_user(): user_file = "config/logged_in_user.yml" try: with open(user_file, "r") as file: user_data = yaml.safe_load(file) return user_data.get("username", "") except FileNotFoundError: return "" # Load cart data from JSON file, filtered by logged-in user # Function to log out user def logout(): st.session_state.cart = [] # Clear the cart st.session_state.logged_in_user = "" # Clear the logged-in user st.switch_page("pages/login_page.py") # Redirect to login page def load_cart_data(username): cart_file = "pages/config/cart_data.json" try: with open(cart_file, "r") as file: cart_data = json.load(file) return [item for item in cart_data if item.get("username") == username] except FileNotFoundError: return [] # Set page config st.set_page_config( page_title="Psychedelic Art Gallery", page_icon="๐ŸŒ€", layout="wide", initial_sidebar_state="expanded" ) # Get logged-in user logged_in_user = load_logged_in_user() # Initialize session state variables if 'cart' not in st.session_state: st.session_state.cart = load_cart_data(logged_in_user) if 'show_cart' not in st.session_state: st.session_state.show_cart = False # Function to add item to cart def add_to_cart(product_name, price, quantity): for item in st.session_state.cart: if item["product_name"] == product_name: item["quantity"] += quantity st.toast(f"Added {quantity} more {product_name} to cart!") return st.session_state.cart.append({ "product_name": product_name, "price": price, "quantity": quantity }) st.toast(f"Added {quantity} {product_name} to cart!") # Define your pages home_page = st.Page("pages/home.py", title="Home", icon="๐Ÿ ") cutting_boards_page = st.Page("pages/cutting_boards.py", title="Cutting Boards", icon="๐Ÿฅ ") charcuterie_page = st.Page("pages/charcuterie_boards.py", title="Charcuterie Boards", icon="๐Ÿด") accessories_page = st.Page("pages/accessories.py", title="Accessories", icon="๐Ÿงข") about_page = st.Page("pages/about.py", title="About", icon="โ„น๏ธ") cart_page = st.Page("pages/cart.py", title="Shopping Cart", icon="๐Ÿ›’") login_page = st.Page("pages/login_page.py", title="User Login Page", icon="๐Ÿ”’") wishlist_page = st.Page("pages/wishlist.py", title="Wishlist", icon="โค๏ธ") # Create navigation pg = st.navigation([home_page, cutting_boards_page, charcuterie_page, accessories_page, cart_page, about_page, login_page, wishlist_page]) # Cart in Sidebar with st.sidebar: cart_count = sum(item.get("quantity", 1) for item in st.session_state.cart) # Shopping Cart Header st.markdown( f"""
Shopping Cart {cart_count}
""", unsafe_allow_html=True ) # Empty Cart Message if not st.session_state.cart: st.markdown('
Your cart is empty
', unsafe_allow_html=True) else: # Loop through items in the cart for i, item in enumerate(st.session_state.cart): product_name = item['product_name'] price_display = item['price'] quantity = item.get('quantity', 1) # Create an expander for each cart item with st.expander(f"{product_name} - {price_display}", expanded=False): with st.container(border=True): st.markdown('
', unsafe_allow_html=True) col1, col2, col3 = st.columns([1, 2, 1]) # Decrease Quantity Button with col1: if st.button("โž–", key=f"decrease_{i}", help="Decrease quantity"): if item["quantity"] > 1: item["quantity"] -= 1 else: # Remove if quantity is 1 st.session_state.cart.pop(i) st.rerun() # Display Quantity with col2: st.markdown( f"
Qty: {quantity}
", unsafe_allow_html=True) # Increase Quantity Button with col3: if st.button("โž•", key=f"increase_{i}", help="Increase quantity"): item["quantity"] += 1 st.rerun() st.markdown('
', unsafe_allow_html=True) # Remove Item Button if st.button("๐Ÿ—‘๏ธ Remove", key=f"remove_{i}", help="Remove from cart", use_container_width=True): st.session_state.cart.pop(i) st.rerun() # Calculate Total Price total = sum(float(item["price"].replace("$", "")) * item.get("quantity", 1) for item in st.session_state.cart) # Display Total Price st.markdown( f"""
Total: ${total:.2f}
""", unsafe_allow_html=True ) # Add spacing before the button st.markdown("
", unsafe_allow_html=True) # Checkout Button if st.button("Proceed to Checkout", key="checkout", use_container_width=True): st.switch_page("pages/cart.py") # Sidebar - Display Logged-in User with st.sidebar: if logged_in_user: st.markdown( f"""
Logged in as:
{logged_in_user}
""", unsafe_allow_html=True ) # Add some spacing st.markdown("
", unsafe_allow_html=True) # Logout Button if st.button("๐Ÿšช Logout", key="logout", use_container_width=True): logout() # Run the current page pg.run()