ksnkumar commited on
Commit
86679b0
·
verified ·
1 Parent(s): 94ed00b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # -----------------------------
4
+ # Inventory Data
5
+ # -----------------------------
6
+ inventory = [
7
+ (101, "Hammer", 120),
8
+ (102, "Screwdriver Set", 350),
9
+ (103, "Drill Machine", 2500),
10
+ (104, "Pliers", 180),
11
+ (105, "Wrench", 220)
12
+ ]
13
+
14
+ # Convert inventory into dictionary for quick lookup
15
+ inventory_dict = {item[0]: {"name": item[1], "price": item[2]} for item in inventory}
16
+
17
+ # Cart
18
+ my_cart = []
19
+
20
+ # -----------------------------
21
+ # Functions
22
+ # -----------------------------
23
+
24
+ def add_to_cart(item_id):
25
+ if item_id is None:
26
+ return my_cart, "Please select an item"
27
+
28
+ item = inventory_dict[item_id]
29
+ my_cart.append(item)
30
+
31
+ return my_cart, f"{item['name']} added to cart"
32
+
33
+ def remove_from_cart(index):
34
+ if index is None or index >= len(my_cart):
35
+ return my_cart, "Invalid selection"
36
+
37
+ removed_item = my_cart.pop(index)
38
+ return my_cart, f"{removed_item['name']} removed from cart"
39
+
40
+ def display_cart():
41
+ if not my_cart:
42
+ return "Cart is empty"
43
+
44
+ cart_text = ""
45
+ for i, item in enumerate(my_cart):
46
+ cart_text += f"{i}. {item['name']} - ${item['price']}\n"
47
+
48
+ return cart_text
49
+
50
+ def calculate_total():
51
+ total = sum(item["price"] for item in my_cart)
52
+ return f"Total Bill: ${total}"
53
+
54
+ # -----------------------------
55
+ # Gradio Interface
56
+ # -----------------------------
57
+
58
+ with gr.Blocks() as demo:
59
+ gr.Markdown("# 🧾 Hardware Store Billing System")
60
+
61
+ # Dropdown to select inventory item
62
+ item_dropdown = gr.Dropdown(
63
+ choices=[(f"{item[1]} (${item[2]})", item[0]) for item in inventory],
64
+ label="Select Item from Inventory"
65
+ )
66
+
67
+ add_button = gr.Button("Add to Cart")
68
+
69
+ # Dropdown for cart items (by index)
70
+ remove_dropdown = gr.Dropdown(label="Select Cart Item Index to Remove")
71
+ remove_button = gr.Button("Remove from Cart")
72
+
73
+ cart_display = gr.Textbox(label="Cart Contents", lines=8)
74
+ total_output = gr.Textbox(label="Total Bill")
75
+ status_output = gr.Textbox(label="Status")
76
+
77
+ # -----------------------------
78
+ # Event Handlers
79
+ # -----------------------------
80
+
81
+ def update_remove_dropdown(cart):
82
+ return gr.Dropdown(
83
+ choices=[(f"{i}: {item['name']}", i) for i, item in enumerate(cart)]
84
+ )
85
+
86
+ add_button.click(
87
+ add_to_cart,
88
+ inputs=item_dropdown,
89
+ outputs=[remove_dropdown, status_output]
90
+ ).then(display_cart, outputs=cart_display).then(update_remove_dropdown, inputs=remove_dropdown, outputs=remove_dropdown)
91
+
92
+ remove_button.click(
93
+ remove_from_cart,
94
+ inputs=remove_dropdown,
95
+ outputs=[remove_dropdown, status_output]
96
+ ).then(display_cart, outputs=cart_display).then(update_remove_dropdo