-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart_app.py
More file actions
88 lines (64 loc) · 2.64 KB
/
cart_app.py
File metadata and controls
88 lines (64 loc) · 2.64 KB
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
import streamlit as st
# Session cart storage (persistent per user session)
if "cart" not in st.session_state:
st.session_state.cart = {}
# Page UI settings
st.set_page_config(page_title="Shopping Cart System", page_icon="🛒", layout="centered")
st.title("🛒 Online Cart System")
st.write("Add, update, remove or buy items with a clean interactive UI.")
# ---------- Functions ----------
def add_item(product, qty):
st.session_state.cart[product] = qty
def update_item(product, qty):
st.session_state.cart[product] = qty
def remove_item(product):
st.session_state.cart.pop(product, None)
# ---------- Sidebar Reset ----------
st.sidebar.header("⚙️ Options")
if st.sidebar.button("Reset Cart"):
st.session_state.cart = {}
st.sidebar.success("Cart cleared!")
# ---------- Add Section ----------
with st.expander("➕ Add Product"):
p_name = st.text_input("Product Name")
p_qty = st.number_input("Quantity", min_value=1, step=1, value=1)
if st.button("Add to Cart"):
if p_name.strip():
add_item(p_name.strip(), p_qty)
st.success(f"{p_qty}× {p_name} added.")
else:
st.warning("Enter a valid product name!")
# ---------- Update Section ----------
with st.expander("✏️ Update Product"):
u_name = st.text_input("Enter product to update")
u_qty = st.number_input("New Quantity", min_value=0, step=1, value=1)
if st.button("Update Item"):
if u_name.strip():
update_item(u_name.strip(), u_qty)
st.success(f"{u_name} updated to {u_qty}.")
else:
st.warning("Enter a product name to update!")
# ---------- Remove Section ----------
with st.expander("❌ Remove Product"):
r_name = st.text_input("Enter product to remove")
if st.button("Remove Item"):
if r_name.strip():
remove_item(r_name.strip())
st.success(f"{r_name} removed (if it existed).")
else:
st.warning("Enter a valid product to remove!")
# ---------- Display Cart ----------
st.subheader("🛍 Current Cart")
if st.session_state.cart:
items = [{"Product": p, "Quantity": q} for p, q in st.session_state.cart.items()]
st.table(items)
else:
st.info("Your cart is empty — add something 😄")
# ---------- Checkout ----------
if st.button("🛒 Buy & Checkout"):
if st.session_state.cart:
st.success("Purchase completed successfully 🎉 — Thank you!")
st.session_state.cart = {}
else:
st.warning("Cart is empty! Add items first.")
st.caption("Made by Sayed Hamza❤️")