Brads_Store / app.py
scottie201
fsdfsd
fdf67c7
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"""
<style>
.cart-container {{
padding: 15px;
border-radius: 10px;
background-color: #2a2a2a;
margin-bottom: 15px;
}}
.cart-header {{
display: flex;
justify-content: space-between;
align-items: center;
font-size: 18px;
font-weight: bold;
padding: 12px;
border-bottom: 2px solid #007BFF;
}}
.cart-total {{
font-size: 18px;
font-weight: bold;
padding: 15px;
text-align: center;
background-color: #1E90FF;
color: white;
border-radius: 8px;
margin-top: 15px;
}}
.empty-cart {{
text-align: center;
padding: 15px;
font-size: 16px;
color: #bbb;
}}
.quantity-row {{
display: flex;
justify-content: center;
align-items: center;
padding: 8px;
}}
</style>
<div class="cart-container">
<div class="cart-header">
<span>Shopping Cart</span>
<span style="background-color: #1E90FF; color: white; padding: 4px 10px; border-radius: 12px; font-size: 14px;">{cart_count}</span>
</div>
</div>
""",
unsafe_allow_html=True
)
# Empty Cart Message
if not st.session_state.cart:
st.markdown('<div class="empty-cart">Your cart is empty</div>',
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('<div class="quantity-row">',
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"<div style='text-align: center; font-weight: bold;'>Qty: {quantity}</div>", 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('</div>', 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"""
<div class="cart-total" style="margin-bottom: 15px;">
Total: <span style="color: #FFF;">${total:.2f}</span>
</div>
""",
unsafe_allow_html=True
)
# Add spacing before the button
st.markdown("<div style='height: 15px;'></div>", 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"""
<div style="padding: 10px; border-radius: 8px; background-color: #2a2a2a; text-align: center; color: white;">
<span style="font-size: 16px;">Logged in as:</span>
<br>
<span style="font-size: 18px; font-weight: bold; color: #1E90FF;">{logged_in_user}</span>
</div>
""",
unsafe_allow_html=True
)
# Add some spacing
st.markdown("<div style='height: 10px;'></div>",
unsafe_allow_html=True)
# Logout Button
if st.button("πŸšͺ Logout", key="logout", use_container_width=True):
logout()
# Run the current page
pg.run()