Spaces:
Build error
Build error
File size: 8,215 Bytes
03f3f1c 4d1f388 fdf67c7 4d1f388 d6ba17f 4d1f388 fdf67c7 4d1f388 03f3f1c 4d1f388 03f3f1c 4d1f388 03f3f1c 4d1f388 03f3f1c 4d1f388 03f3f1c d6ba17f 03f3f1c 4d1f388 d6ba17f 03f3f1c 4d1f388 d6ba17f 03f3f1c d6ba17f 03f3f1c d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 03f3f1c d6ba17f 03f3f1c d6ba17f 03f3f1c d6ba17f 03f3f1c 4d1f388 03f3f1c d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 4d1f388 d6ba17f 03f3f1c d6ba17f 03f3f1c 4d1f388 03f3f1c d6ba17f 03f3f1c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 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()
|