| | import gradio as gr |
| |
|
| | |
| |
|
| | Inventory = [ |
| | (101, "Hammer", 120), |
| | (102, "Screwdriver Set", 350), |
| | (103, "Drill Machine", 2500), |
| | (104, "Pliers", 180), |
| | (105, "Wrench", 220), |
| | ] |
| |
|
| |
|
| | def format_cart(cart): |
| | if not cart: |
| | return "Cart is empty." |
| | lines = [] |
| | for item_id, name, price in cart: |
| | lines.append(f"{item_id} - {name}: ₹{price}") |
| | return "\n".join(lines) |
| |
|
| |
|
| | def inventory_choices(): |
| | return [f"{item_id} - {name} (₹{price})" for item_id, name, price in Inventory] |
| |
|
| |
|
| | def cart_choices(cart): |
| | return [f"{item_id} - {name} (₹{price})" for item_id, name, price in cart] |
| |
|
| |
|
| | def parse_id_from_choice(choice: str | None): |
| | if not choice: |
| | return None |
| | |
| | try: |
| | return int(choice.split(" - ")[0].strip()) |
| | except Exception: |
| | return None |
| |
|
| |
|
| | |
| |
|
| | def add_item(selected_inventory_item, cart): |
| | cart = cart or [] |
| | item_id = parse_id_from_choice(selected_inventory_item) |
| | if item_id is None: |
| | return cart, format_cart(cart), gr.Dropdown(choices=cart_choices(cart)) |
| |
|
| | for inv_item in Inventory: |
| | if inv_item[0] == item_id: |
| | cart.append(inv_item) |
| | break |
| |
|
| | return cart, format_cart(cart), gr.Dropdown(choices=cart_choices(cart)) |
| |
|
| |
|
| | def remove_item(selected_cart_item, cart): |
| | cart = cart or [] |
| | item_id = parse_id_from_choice(selected_cart_item) |
| | if item_id is None: |
| | return cart, format_cart(cart), gr.Dropdown(choices=cart_choices(cart)) |
| |
|
| | cart = [item for item in cart if item[0] != item_id] |
| | return cart, format_cart(cart), gr.Dropdown(choices=cart_choices(cart)) |
| |
|
| |
|
| | def calculate_total(cart): |
| | cart = cart or [] |
| | return sum(price for _, _, price in cart) |
| |
|
| |
|
| | |
| |
|
| | with gr.Blocks() as demo: |
| | gr.Markdown("## 🧰 Hardware Store Billing System") |
| |
|
| | cart_state = gr.State([]) |
| |
|
| | with gr.Row(): |
| | inventory_dropdown = gr.Dropdown( |
| | choices=inventory_choices(), |
| | label="Select Item to Add", |
| | ) |
| | add_btn = gr.Button("Add Tool") |
| |
|
| | with gr.Row(): |
| | cart_dropdown = gr.Dropdown( |
| | choices=[], |
| | label="Select Item to Remove", |
| | ) |
| | remove_btn = gr.Button("Remove Tool") |
| |
|
| | cart_display = gr.Textbox( |
| | label="Cart Contents", |
| | lines=10, |
| | interactive=False, |
| | ) |
| |
|
| | total_btn = gr.Button("Calculate Total") |
| | total_output = gr.Number(label="Total Bill") |
| |
|
| | |
| | add_btn.click( |
| | fn=add_item, |
| | inputs=[inventory_dropdown, cart_state], |
| | outputs=[cart_state, cart_display, cart_dropdown], |
| | ) |
| |
|
| | |
| | remove_btn.click( |
| | fn=remove_item, |
| | inputs=[cart_dropdown, cart_state], |
| | outputs=[cart_state, cart_display, cart_dropdown], |
| | ) |
| |
|
| | |
| | total_btn.click( |
| | fn=calculate_total, |
| | inputs=[cart_state], |
| | outputs=[total_output], |
| | ) |
| |
|
| | if __name__ == "__main__": |
| | demo.launch() |
| |
|