eaglelandsonce commited on
Commit
e81d7d6
Β·
verified Β·
1 Parent(s): 0d9a307

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # -----------------------
4
+ # 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
+ # Index by ID for quick lookup
15
+ INV_BY_ID = {item_id: (name, price) for item_id, name, price in INVENTORY}
16
+
17
+ # -----------------------
18
+ # Cart state helpers
19
+ # -----------------------
20
+ def fmt_cart(cart):
21
+ if not cart:
22
+ return "β€” (empty) β€”"
23
+ lines = [f"{i}. [{item_id}] {name} β€” ${price}" for i, (item_id, name, price) in enumerate(cart)]
24
+ return "\n".join(lines)
25
+
26
+ def total_cost(cart):
27
+ return sum(price for _, _, price in cart)
28
+
29
+ # -----------------------
30
+ # Actions
31
+ # -----------------------
32
+ def add_to_cart(item_id, cart):
33
+ cart = cart or []
34
+ msg = ""
35
+ try:
36
+ item_id = int(item_id)
37
+ except (TypeError, ValueError):
38
+ return fmt_cart(cart), "Invalid ID: enter a number.", "", total_cost(cart), cart
39
+
40
+ if item_id not in INV_BY_ID:
41
+ return fmt_cart(cart), f"ID {item_id} not found in inventory.", "", total_cost(cart), cart
42
+
43
+ name, price = INV_BY_ID[item_id]
44
+ cart.append((item_id, name, price))
45
+ return fmt_cart(cart), f"Added: [{item_id}] {name} (${price})", "", total_cost(cart), cart
46
+
47
+ def remove_from_cart(index, cart):
48
+ cart = cart or []
49
+ msg_ok, msg_err = "", ""
50
+ try:
51
+ index = int(index)
52
+ except (TypeError, ValueError):
53
+ return fmt_cart(cart), "", "Invalid index: enter a number.", total_cost(cart), cart
54
+
55
+ if 0 <= index < len(cart):
56
+ item_id, name, price = cart.pop(index)
57
+ msg_ok = f"Removed: [{item_id}] {name} (${price})"
58
+ else:
59
+ msg_err = f"Index {index} not in cart range (0–{max(len(cart)-1,0)})."
60
+ return fmt_cart(cart), msg_ok, msg_err, total_cost(cart), cart
61
+
62
+ # -----------------------
63
+ # UI
64
+ # -----------------------
65
+ with gr.Blocks(title="Hardware Store Billing") as demo:
66
+ gr.Markdown("## Hardware Store Billing\nAdd items by **Inventory ID**; remove by **cart index** (0-based).")
67
+
68
+ with gr.Row():
69
+ add_id = gr.Number(label="Enter Inventory ID to Add to Cart", value=0, precision=0)
70
+ rem_idx = gr.Number(label="Enter Index ID to Remove from Cart", value=0, precision=0)
71
+
72
+ with gr.Row():
73
+ btn_add = gr.Button("Add to Cart", variant="primary")
74
+ btn_rem = gr.Button("Remove from Cart")
75
+
76
+ cart_box = gr.Textbox(label="Your Cart", value="β€” (empty) β€”", lines=10)
77
+ add_msg = gr.Textbox(label="Add Item Message", interactive=False)
78
+ rem_msg = gr.Textbox(label="Remove Item Message", interactive=False)
79
+ total = gr.Number(label="Total Cost", value=0, interactive=False, precision=0)
80
+
81
+ # Keep cart in state
82
+ cart_state = gr.State([])
83
+
84
+ btn_add.click(
85
+ fn=add_to_cart,
86
+ inputs=[add_id, cart_state],
87
+ outputs=[cart_box, add_msg, rem_msg, total, cart_state]
88
+ )
89
+
90
+ btn_rem.click(
91
+ fn=remove_from_cart,
92
+ inputs=[rem_idx, cart_state],
93
+ outputs=[cart_box, add_msg, rem_msg, total, cart_state]
94
+ )
95
+
96
+ with gr.Accordion("Inventory", open=False):
97
+ gr.Dataframe(
98
+ headers=["ID", "Name", "Price"],
99
+ value=INVENTORY,
100
+ wrap=True
101
+ )
102
+
103
+ if __name__ == "__main__":
104
+ demo.launch()