Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# ---------- 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 |
+
# Global cart
|
| 13 |
+
cart = []
|
| 14 |
+
|
| 15 |
+
# ---------- Helper Functions ----------
|
| 16 |
+
def add_tool(tool_name):
|
| 17 |
+
for item in inventory:
|
| 18 |
+
if item[1] == tool_name:
|
| 19 |
+
cart.append(item)
|
| 20 |
+
break
|
| 21 |
+
return display_cart(), f"✅ Added {tool_name} to cart."
|
| 22 |
+
|
| 23 |
+
def remove_tool(tool_name):
|
| 24 |
+
global cart
|
| 25 |
+
for item in cart:
|
| 26 |
+
if item[1] == tool_name:
|
| 27 |
+
cart.remove(item)
|
| 28 |
+
break
|
| 29 |
+
return display_cart(), f"🗑️ Removed {tool_name} from cart."
|
| 30 |
+
|
| 31 |
+
def display_cart():
|
| 32 |
+
if not cart:
|
| 33 |
+
return "Cart is empty."
|
| 34 |
+
cart_display = "\n".join([f"{t[1]} - ${t[2]}" for t in cart])
|
| 35 |
+
return cart_display
|
| 36 |
+
|
| 37 |
+
def calculate_total():
|
| 38 |
+
total = sum([t[2] for t in cart])
|
| 39 |
+
return f"💰 Total Bill: ${total}"
|
| 40 |
+
|
| 41 |
+
# ---------- Interface Components ----------
|
| 42 |
+
tool_names = [item[1] for item in inventory]
|
| 43 |
+
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("## 🧰 Hardware Store Billing System\nAdd or remove tools and calculate your total.")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
tool_dropdown = gr.Dropdown(tool_names, label="Select Tool to Add", interactive=True)
|
| 49 |
+
add_btn = gr.Button("Add Tool")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
remove_dropdown = gr.Dropdown(tool_names, label="Select Tool to Remove", interactive=True)
|
| 53 |
+
remove_btn = gr.Button("Remove Tool")
|
| 54 |
+
|
| 55 |
+
cart_box = gr.Textbox(label="🛒 Cart Contents", lines=6)
|
| 56 |
+
total_btn = gr.Button("Calculate Total")
|
| 57 |
+
result_box = gr.Textbox(label="💵 Result", interactive=False)
|
| 58 |
+
|
| 59 |
+
add_btn.click(add_tool, inputs=tool_dropdown, outputs=[cart_box, result_box])
|
| 60 |
+
remove_btn.click(remove_tool, inputs=remove_dropdown, outputs=[cart_box, result_box])
|
| 61 |
+
total_btn.click(calculate_total, outputs=result_box)
|
| 62 |
+
|
| 63 |
+
demo.launch()
|