eaglelandsonce commited on
Commit
b271788
·
verified ·
1 Parent(s): 82a5dc2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # --- Data Structures ---
4
+ Inventory = [
5
+ ("Apples", 250),
6
+ ("Cherry", 650),
7
+ ("Chickoo", 50),
8
+ ("Oranges", 80),
9
+ ("Mangoes", 150),
10
+ ]
11
+
12
+ # Convert Inventory into a dictionary for easier lookup: {"Apples": 250, "Cherry": 650, ...}
13
+ price_dict = {item[0]: item[1] for item in Inventory}
14
+
15
+ # Initialize the cart in session state if not already present
16
+ if "cart" not in st.session_state:
17
+ st.session_state.cart = [] # Each element will be a tuple (product_name, quantity)
18
+
19
+ # --- Streamlit Layout ---
20
+ st.title("Shopping Cart Management System")
21
+
22
+ # 1. Add Products to the Shopping Cart
23
+ st.subheader("Add Items to Your Cart")
24
+ product_list = [item[0] for item in Inventory] # ["Apples", "Cherry", "Chickoo", "Oranges", "Mangoes"]
25
+ selected_product = st.selectbox("Select a product", product_list)
26
+ selected_quantity = st.number_input("Quantity", min_value=1, value=1, step=1)
27
+
28
+ if st.button("Add to Cart"):
29
+ # Append (product, quantity) to the cart
30
+ st.session_state.cart.append((selected_product, selected_quantity))
31
+ st.success(f"Added {selected_quantity} {selected_product}(s) to your cart!")
32
+
33
+ st.write("---")
34
+
35
+ # 2. Display and Remove Items from the Shopping Cart
36
+ st.subheader("Items in Your Cart")
37
+ if len(st.session_state.cart) == 0:
38
+ st.info("Your cart is empty. Add some items!")
39
+ else:
40
+ # List each item along with remove button
41
+ for i, (product, quantity) in enumerate(st.session_state.cart):
42
+ col1, col2, col3 = st.columns([3, 2, 1])
43
+ with col1:
44
+ st.write(f"**{product}** x {quantity} @ {price_dict[product]} each")
45
+ with col2:
46
+ st.write(f"Subtotal: {price_dict[product] * quantity}")
47
+ with col3:
48
+ # Create a dynamic remove button
49
+ remove_button_label = f"Remove-{i}"
50
+ if st.button("Remove", key=remove_button_label):
51
+ st.session_state.cart.pop(i)
52
+ st.experimental_rerun() # Refresh the app so the item disappears immediately
53
+
54
+ # 3. Calculate the Total Bill
55
+ total_bill = sum(price_dict[p] * q for p, q in st.session_state.cart)
56
+ st.write("---")
57
+ st.subheader(f"**Total Bill: {total_bill}**")