Spaces:
Sleeping
Sleeping
import gradio as gr | |
# ----------------------- | |
# Data | |
# ----------------------- | |
INVENTORY = [ | |
(101, "Hammer", 120), | |
(102, "Screwdriver Set", 350), | |
(103, "Drill Machine", 2500), | |
(104, "Pliers", 180), | |
(105, "Wrench", 220), | |
] | |
# Index by ID for quick lookup | |
INV_BY_ID = {item_id: (name, price) for item_id, name, price in INVENTORY} | |
# ----------------------- | |
# Cart state helpers | |
# ----------------------- | |
def fmt_cart(cart): | |
if not cart: | |
return "β (empty) β" | |
lines = [f"{i}. [{item_id}] {name} β ${price}" for i, (item_id, name, price) in enumerate(cart)] | |
return "\n".join(lines) | |
def total_cost(cart): | |
return sum(price for _, _, price in cart) | |
# ----------------------- | |
# Actions | |
# ----------------------- | |
def add_to_cart(item_id, cart): | |
cart = cart or [] | |
msg = "" | |
try: | |
item_id = int(item_id) | |
except (TypeError, ValueError): | |
return fmt_cart(cart), "Invalid ID: enter a number.", "", total_cost(cart), cart | |
if item_id not in INV_BY_ID: | |
return fmt_cart(cart), f"ID {item_id} not found in inventory.", "", total_cost(cart), cart | |
name, price = INV_BY_ID[item_id] | |
cart.append((item_id, name, price)) | |
return fmt_cart(cart), f"Added: [{item_id}] {name} (${price})", "", total_cost(cart), cart | |
def remove_from_cart(index, cart): | |
cart = cart or [] | |
msg_ok, msg_err = "", "" | |
try: | |
index = int(index) | |
except (TypeError, ValueError): | |
return fmt_cart(cart), "", "Invalid index: enter a number.", total_cost(cart), cart | |
if 0 <= index < len(cart): | |
item_id, name, price = cart.pop(index) | |
msg_ok = f"Removed: [{item_id}] {name} (${price})" | |
else: | |
msg_err = f"Index {index} not in cart range (0β{max(len(cart)-1,0)})." | |
return fmt_cart(cart), msg_ok, msg_err, total_cost(cart), cart | |
# ----------------------- | |
# UI | |
# ----------------------- | |
with gr.Blocks(title="Hardware Store Billing") as demo: | |
gr.Markdown("## Hardware Store Billing\nAdd items by **Inventory ID**; remove by **cart index** (0-based).") | |
with gr.Row(): | |
add_id = gr.Number(label="Enter Inventory ID to Add to Cart", value=0, precision=0) | |
rem_idx = gr.Number(label="Enter Index ID to Remove from Cart", value=0, precision=0) | |
with gr.Row(): | |
btn_add = gr.Button("Add to Cart", variant="primary") | |
btn_rem = gr.Button("Remove from Cart") | |
cart_box = gr.Textbox(label="Your Cart", value="β (empty) β", lines=10) | |
add_msg = gr.Textbox(label="Add Item Message", interactive=False) | |
rem_msg = gr.Textbox(label="Remove Item Message", interactive=False) | |
total = gr.Number(label="Total Cost", value=0, interactive=False, precision=0) | |
# Keep cart in state | |
cart_state = gr.State([]) | |
btn_add.click( | |
fn=add_to_cart, | |
inputs=[add_id, cart_state], | |
outputs=[cart_box, add_msg, rem_msg, total, cart_state] | |
) | |
btn_rem.click( | |
fn=remove_from_cart, | |
inputs=[rem_idx, cart_state], | |
outputs=[cart_box, add_msg, rem_msg, total, cart_state] | |
) | |
with gr.Accordion("Inventory", open=False): | |
gr.Dataframe( | |
headers=["ID", "Name", "Price"], | |
value=INVENTORY, | |
wrap=True | |
) | |
if __name__ == "__main__": | |
demo.launch() | |