Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
# -----------------------------
|
| 4 |
-
# Module 1: Data Structures
|
| 5 |
# -----------------------------
|
| 6 |
|
| 7 |
Inventory = [
|
| 8 |
-
(101, "Hammer",
|
| 9 |
-
(102, "Screwdriver Set",
|
| 10 |
-
(103, "Drill Machine",
|
| 11 |
-
(104, "Pliers",
|
| 12 |
-
(105, "Wrench",
|
| 13 |
]
|
| 14 |
|
| 15 |
-
#
|
| 16 |
inventory_lookup = {item[0]: {"name": item[1], "price": item[2]} for item in Inventory}
|
| 17 |
|
| 18 |
-
# Cart
|
| 19 |
My_Cart = []
|
| 20 |
|
| 21 |
|
|
@@ -41,24 +41,35 @@ def get_cart_contents():
|
|
| 41 |
lines = []
|
| 42 |
for item_id in My_Cart:
|
| 43 |
item = inventory_lookup[item_id]
|
| 44 |
-
lines.append(f"{item_id} - {item['name']} -
|
| 45 |
return "\n".join(lines)
|
| 46 |
|
| 47 |
def calculate_total():
|
| 48 |
total = sum(inventory_lookup[item_id]["price"] for item_id in My_Cart)
|
| 49 |
-
return f"Total Amount:
|
| 50 |
|
| 51 |
|
| 52 |
# -----------------------------
|
| 53 |
-
# Module 3:
|
| 54 |
# -----------------------------
|
| 55 |
|
| 56 |
-
def
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return get_cart_contents(), msg
|
| 59 |
|
| 60 |
-
def handle_remove(
|
| 61 |
-
|
|
|
|
| 62 |
return get_cart_contents(), msg
|
| 63 |
|
| 64 |
def handle_total():
|
|
@@ -66,24 +77,29 @@ def handle_total():
|
|
| 66 |
|
| 67 |
|
| 68 |
# -----------------------------
|
| 69 |
-
# Module
|
| 70 |
# -----------------------------
|
| 71 |
|
| 72 |
-
with gr.Blocks(title="Hardware Store Billing System") as demo:
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
add_dropdown = gr.Dropdown(
|
| 78 |
-
choices=
|
| 79 |
-
label="Select Item to Add
|
| 80 |
)
|
| 81 |
add_button = gr.Button("Add Tool")
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
remove_dropdown = gr.Dropdown(
|
| 85 |
-
choices=
|
| 86 |
-
label="Select Item to Remove
|
| 87 |
)
|
| 88 |
remove_button = gr.Button("Remove Tool")
|
| 89 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
# -----------------------------
|
| 4 |
+
# Module 1: Data Structures (USD)
|
| 5 |
# -----------------------------
|
| 6 |
|
| 7 |
Inventory = [
|
| 8 |
+
(101, "Hammer", 1.20),
|
| 9 |
+
(102, "Screwdriver Set", 3.50),
|
| 10 |
+
(103, "Drill Machine", 25.00),
|
| 11 |
+
(104, "Pliers", 1.80),
|
| 12 |
+
(105, "Wrench", 2.20),
|
| 13 |
]
|
| 14 |
|
| 15 |
+
# Lookup dictionary for fast access
|
| 16 |
inventory_lookup = {item[0]: {"name": item[1], "price": item[2]} for item in Inventory}
|
| 17 |
|
| 18 |
+
# Cart holds item IDs
|
| 19 |
My_Cart = []
|
| 20 |
|
| 21 |
|
|
|
|
| 41 |
lines = []
|
| 42 |
for item_id in My_Cart:
|
| 43 |
item = inventory_lookup[item_id]
|
| 44 |
+
lines.append(f"{item_id} - {item['name']} - ${item['price']:.2f}")
|
| 45 |
return "\n".join(lines)
|
| 46 |
|
| 47 |
def calculate_total():
|
| 48 |
total = sum(inventory_lookup[item_id]["price"] for item_id in My_Cart)
|
| 49 |
+
return f"Total Amount: ${total:.2f}"
|
| 50 |
|
| 51 |
|
| 52 |
# -----------------------------
|
| 53 |
+
# Module 3: Helpers for Dropdown Text
|
| 54 |
# -----------------------------
|
| 55 |
|
| 56 |
+
def extract_id(choice_text):
|
| 57 |
+
# Dropdown text looks like: "101 - Hammer - $1.20"
|
| 58 |
+
return int(choice_text.split(" - ")[0])
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# -----------------------------
|
| 62 |
+
# Module 4: Gradio Callbacks
|
| 63 |
+
# -----------------------------
|
| 64 |
+
|
| 65 |
+
def handle_add(selected_text):
|
| 66 |
+
item_id = extract_id(selected_text)
|
| 67 |
+
msg = add_to_cart(item_id)
|
| 68 |
return get_cart_contents(), msg
|
| 69 |
|
| 70 |
+
def handle_remove(selected_text):
|
| 71 |
+
item_id = extract_id(selected_text)
|
| 72 |
+
msg = remove_from_cart(item_id)
|
| 73 |
return get_cart_contents(), msg
|
| 74 |
|
| 75 |
def handle_total():
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
# -----------------------------
|
| 80 |
+
# Module 5: Gradio Interface
|
| 81 |
# -----------------------------
|
| 82 |
|
| 83 |
+
with gr.Blocks(title="Hardware Store Billing System (USD)") as demo:
|
| 84 |
+
|
| 85 |
+
gr.Markdown("## 🧰 Hardware Store Billing System (USD)")
|
| 86 |
|
| 87 |
+
# Dropdowns show: "101 - Hammer - $1.20"
|
| 88 |
+
formatted_choices = [
|
| 89 |
+
f"{item[0]} - {item[1]} - ${item[2]:.2f}" for item in Inventory
|
| 90 |
+
]
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
add_dropdown = gr.Dropdown(
|
| 94 |
+
choices=formatted_choices,
|
| 95 |
+
label="Select Item to Add"
|
| 96 |
)
|
| 97 |
add_button = gr.Button("Add Tool")
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
remove_dropdown = gr.Dropdown(
|
| 101 |
+
choices=formatted_choices,
|
| 102 |
+
label="Select Item to Remove"
|
| 103 |
)
|
| 104 |
remove_button = gr.Button("Remove Tool")
|
| 105 |
|