eaglelandsonce commited on
Commit
064e3e4
·
verified ·
1 Parent(s): 159265d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +178 -0
app.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ # pip install gradio
3
+
4
+ import gradio as gr
5
+
6
+ # ----- Data -----
7
+ inventory = [
8
+ (101, "Hammer", 120),
9
+ (102, "Screwdriver Set", 350),
10
+ (103, "Drill Machine", 2500),
11
+ (104, "Pliers", 180),
12
+ (105, "Wrench", 220),
13
+ ]
14
+
15
+ INV = {pid: {"name": name, "price": price} for pid, name, price in inventory}
16
+
17
+
18
+ # ----- Helpers -----
19
+ def format_cart(cart):
20
+ if not cart:
21
+ return "Cart is empty."
22
+ lines = ["# Item (ID) - Price"]
23
+ for i, item in enumerate(cart, start=1):
24
+ lines.append(f"{i}. {item['name']} ({item['id']}) - ${item['price']}")
25
+ return "\n".join(lines)
26
+
27
+ def cart_dropdown_choices(cart):
28
+ # Each cart entry is removable individually (even duplicates)
29
+ choices = []
30
+ for idx, item in enumerate(cart):
31
+ label = f"{idx+1}: {item['name']} (ID {item['id']}) - ${item['price']}"
32
+ choices.append((label, idx)) # (display_label, value=index)
33
+ return choices
34
+
35
+
36
+ # ----- Actions -----
37
+ def add_to_cart(selected_id, cart):
38
+ if cart is None:
39
+ cart = []
40
+
41
+ if selected_id is None:
42
+ return (
43
+ gr.update(choices=cart_dropdown_choices(cart), value=None),
44
+ format_cart(cart),
45
+ "Please select a Product ID to add.",
46
+ "",
47
+ "",
48
+ cart,
49
+ )
50
+
51
+ item = INV.get(int(selected_id))
52
+ if not item:
53
+ return (
54
+ gr.update(choices=cart_dropdown_choices(cart), value=None),
55
+ format_cart(cart),
56
+ f"Product ID {selected_id} not found in inventory.",
57
+ "",
58
+ "",
59
+ cart,
60
+ )
61
+
62
+ cart.append({"id": int(selected_id), "name": item["name"], "price": item["price"]})
63
+
64
+ return (
65
+ gr.update(choices=cart_dropdown_choices(cart), value=None),
66
+ format_cart(cart),
67
+ f"Added: {item['name']} (ID {selected_id}) - ${item['price']}",
68
+ "",
69
+ "",
70
+ cart,
71
+ )
72
+
73
+ def remove_from_cart(selected_cart_index, cart):
74
+ if cart is None:
75
+ cart = []
76
+
77
+ if not cart:
78
+ return (
79
+ gr.update(choices=[], value=None),
80
+ format_cart(cart),
81
+ "",
82
+ "Cart is empty — nothing to remove.",
83
+ "",
84
+ cart,
85
+ )
86
+
87
+ if selected_cart_index is None:
88
+ return (
89
+ gr.update(choices=cart_dropdown_choices(cart), value=None),
90
+ format_cart(cart),
91
+ "",
92
+ "Please select an item from the cart to remove.",
93
+ "",
94
+ cart,
95
+ )
96
+
97
+ idx = int(selected_cart_index)
98
+ if idx < 0 or idx >= len(cart):
99
+ return (
100
+ gr.update(choices=cart_dropdown_choices(cart), value=None),
101
+ format_cart(cart),
102
+ "",
103
+ "Invalid selection. Try again.",
104
+ "",
105
+ cart,
106
+ )
107
+
108
+ removed = cart.pop(idx)
109
+
110
+ return (
111
+ gr.update(choices=cart_dropdown_choices(cart), value=None),
112
+ format_cart(cart),
113
+ "",
114
+ f"Removed: {removed['name']} (ID {removed['id']}) - ${removed['price']}",
115
+ "",
116
+ cart,
117
+ )
118
+
119
+ def calculate_total(cart):
120
+ if cart is None:
121
+ cart = []
122
+ total = sum(item["price"] for item in cart)
123
+ return f"${total}"
124
+
125
+
126
+ # ----- UI -----
127
+ product_choices = [(f"{pid}: {name} - ${price}", pid) for pid, name, price in inventory]
128
+
129
+ with gr.Blocks(title="Hardware Store Billing") as demo:
130
+ gr.Markdown("## Hardware Store Billing (Add / Remove / Total)")
131
+
132
+ cart_state = gr.State([])
133
+
134
+ with gr.Row():
135
+ add_dropdown = gr.Dropdown(
136
+ label="Enter Product ID to Add to Cart",
137
+ choices=product_choices,
138
+ value=None,
139
+ )
140
+ remove_dropdown = gr.Dropdown(
141
+ label="Enter Index to Remove from Cart",
142
+ choices=[],
143
+ value=None,
144
+ )
145
+
146
+ with gr.Row():
147
+ add_btn = gr.Button("Add to Cart")
148
+ remove_btn = gr.Button("Remove from Cart")
149
+
150
+ cart_box = gr.Markdown("Cart is empty.", label="Your Cart")
151
+
152
+ add_msg = gr.Textbox(label="Add Item Message", interactive=False)
153
+ remove_msg = gr.Textbox(label="Remove Item Message", interactive=False)
154
+
155
+ with gr.Row():
156
+ total_btn = gr.Button("Calculate Total")
157
+ total_out = gr.Textbox(label="Total Cost", interactive=False)
158
+
159
+ # Wire actions
160
+ add_btn.click(
161
+ fn=add_to_cart,
162
+ inputs=[add_dropdown, cart_state],
163
+ outputs=[remove_dropdown, cart_box, add_msg, remove_msg, total_out, cart_state],
164
+ )
165
+
166
+ remove_btn.click(
167
+ fn=remove_from_cart,
168
+ inputs=[remove_dropdown, cart_state],
169
+ outputs=[remove_dropdown, cart_box, add_msg, remove_msg, total_out, cart_state],
170
+ )
171
+
172
+ total_btn.click(
173
+ fn=calculate_total,
174
+ inputs=[cart_state],
175
+ outputs=[total_out],
176
+ )
177
+
178
+ demo.launch()