schrabfab commited on
Commit
77bfe74
·
verified ·
1 Parent(s): c334172

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # Inventory data
4
+ inventory = [
5
+ (101, "Hammer", 120),
6
+ (102, "Screwdriver Set", 350),
7
+ (103, "Drill Machine", 2500),
8
+ (104, "Pliers", 180),
9
+ (105, "Wrench", 220),
10
+ ]
11
+
12
+ # Cart data
13
+ my_cart = []
14
+
15
+ # Create lookup dictionaries
16
+ inventory_by_id = {item_id: {"name": name, "price": price} for item_id, name, price in inventory}
17
+
18
+
19
+ def get_inventory_choices():
20
+ return [f"{item_id} - {data['name']} (${data['price']})" for item_id, data in inventory_by_id.items()]
21
+
22
+
23
+ def get_cart_choices():
24
+ if not my_cart:
25
+ return []
26
+ return [f"{index}: {item['id']} - {item['name']} (${item['price']})" for index, item in enumerate(my_cart)]
27
+
28
+
29
+ def show_cart():
30
+ if not my_cart:
31
+ return "Cart is empty."
32
+ lines = []
33
+ for i, item in enumerate(my_cart, start=1):
34
+ lines.append(f"{i}. {item['id']} - {item['name']} - ${item['price']}")
35
+ return "\n".join(lines)
36
+
37
+
38
+ def add_tool(selected_item):
39
+ if not selected_item:
40
+ return show_cart(), gr.update(choices=get_cart_choices(), value=None), "Please select an item."
41
+
42
+ item_id = int(selected_item.split(" - ")[0])
43
+ item = inventory_by_id[item_id]
44
+
45
+ my_cart.append({
46
+ "id": item_id,
47
+ "name": item["name"],
48
+ "price": item["price"]
49
+ })
50
+
51
+ return (
52
+ show_cart(),
53
+ gr.update(choices=get_cart_choices(), value=None),
54
+ f"Added {item['name']} to cart."
55
+ )
56
+
57
+
58
+ def remove_tool(selected_cart_item):
59
+ if not selected_cart_item:
60
+ return show_cart(), gr.update(choices=get_cart_choices(), value=None), "Please select a cart item to remove."
61
+
62
+ index = int(selected_cart_item.split(":")[0])
63
+ removed_item = my_cart.pop(index)
64
+
65
+ return (
66
+ show_cart(),
67
+ gr.update(choices=get_cart_choices(), value=None),
68
+ f"Removed {removed_item['name']} from cart."
69
+ )
70
+
71
+
72
+ def calculate_total():
73
+ total = sum(item["price"] for item in my_cart)
74
+ return f"Total Bill: ${total}"
75
+
76
+
77
+ with gr.Blocks(title="Hardware Store Billing System") as demo:
78
+ gr.Markdown("## Hardware Store Billing System")
79
+
80
+ with gr.Row():
81
+ item_dropdown = gr.Dropdown(
82
+ choices=get_inventory_choices(),
83
+ label="Select a tool from inventory"
84
+ )
85
+ cart_dropdown = gr.Dropdown(
86
+ choices=get_cart_choices(),
87
+ label="Select a tool from cart to remove"
88
+ )
89
+
90
+ with gr.Row():
91
+ add_button = gr.Button("Add Tool")
92
+ remove_button = gr.Button("Remove Tool")
93
+
94
+ cart_display = gr.Textbox(
95
+ label="Cart Contents",
96
+ lines=10,
97
+ interactive=False
98
+ )
99
+
100
+ status_output = gr.Textbox(
101
+ label="Status Message",
102
+ interactive=False
103
+ )
104
+
105
+ total_button = gr.Button("Calculate Total")
106
+ total_output = gr.Textbox(
107
+ label="Final Bill",
108
+ interactive=False
109
+ )
110
+
111
+ add_button.click(
112
+ fn=add_tool,
113
+ inputs=item_dropdown,
114
+ outputs=[cart_display, cart_dropdown, status_output]
115
+ )
116
+
117
+ remove_button.click(
118
+ fn=remove_tool,
119
+ inputs=cart_dropdown,
120
+ outputs=[cart_display, cart_dropdown, status_output]
121
+ )
122
+
123
+ total_button.click(
124
+ fn=calculate_total,
125
+ inputs=[],
126
+ outputs=total_output
127
+ )
128
+
129
+ demo.launch()