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) | |
| ] | |
| # Global cart | |
| cart = [] | |
| # ---------- Helper Functions ---------- | |
| def add_tool(tool_name): | |
| for item in inventory: | |
| if item[1] == tool_name: | |
| cart.append(item) | |
| break | |
| return display_cart(), f"β Added {tool_name} to cart." | |
| def remove_tool(tool_name): | |
| global cart | |
| for item in cart: | |
| if item[1] == tool_name: | |
| cart.remove(item) | |
| break | |
| return display_cart(), f"ποΈ Removed {tool_name} from cart." | |
| def display_cart(): | |
| if not cart: | |
| return "Cart is empty." | |
| cart_display = "\n".join([f"{t[1]} - ${t[2]}" for t in cart]) | |
| return cart_display | |
| def calculate_total(): | |
| total = sum([t[2] for t in cart]) | |
| return f"π° Total Bill: ${total}" | |
| # ---------- Interface Components ---------- | |
| tool_names = [item[1] for item in inventory] | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## π§° Hardware Store Billing System\nAdd or remove tools and calculate your total.") | |
| with gr.Row(): | |
| tool_dropdown = gr.Dropdown(tool_names, label="Select Tool to Add", interactive=True) | |
| add_btn = gr.Button("Add Tool") | |
| with gr.Row(): | |
| remove_dropdown = gr.Dropdown(tool_names, label="Select Tool to Remove", interactive=True) | |
| remove_btn = gr.Button("Remove Tool") | |
| cart_box = gr.Textbox(label="π Cart Contents", lines=6) | |
| total_btn = gr.Button("Calculate Total") | |
| result_box = gr.Textbox(label="π΅ Result", interactive=False) | |
| add_btn.click(add_tool, inputs=tool_dropdown, outputs=[cart_box, result_box]) | |
| remove_btn.click(remove_tool, inputs=remove_dropdown, outputs=[cart_box, result_box]) | |
| total_btn.click(calculate_total, outputs=result_box) | |
| demo.launch() | |