Spaces:
No application file
No application file
Download app.py from schrabfab/hardwareStore_Example: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/schrabfab/hardwareStore_Example/resolve/main/app.py
- Command line
-
hf download hf://spaces/schrabfab/hardwareStore_Example/app.py
-
curl -L -o app.py https://huggingface.co/spaces/schrabfab/hardwareStore_Example/resolve/main/app.py
3.24 kB
| import gradio as gr | |
| # Inventory data | |
| inventory = [ | |
| (101, "Hammer", 120), | |
| (102, "Screwdriver Set", 350), | |
| (103, "Drill Machine", 2500), | |
| (104, "Pliers", 180), | |
| (105, "Wrench", 220), | |
| ] | |
| # Cart data | |
| my_cart = [] | |
| # Create lookup dictionaries | |
| inventory_by_id = {item_id: {"name": name, "price": price} for item_id, name, price in inventory} | |
| def get_inventory_choices(): | |
| return [f"{item_id} - {data['name']} (${data['price']})" for item_id, data in inventory_by_id.items()] | |
| def get_cart_choices(): | |
| if not my_cart: | |
| return [] | |
| return [f"{index}: {item['id']} - {item['name']} (${item['price']})" for index, item in enumerate(my_cart)] | |
| def show_cart(): | |
| if not my_cart: | |
| return "Cart is empty." | |
| lines = [] | |
| for i, item in enumerate(my_cart, start=1): | |
| lines.append(f"{i}. {item['id']} - {item['name']} - ${item['price']}") | |
| return "\n".join(lines) | |
| def add_tool(selected_item): | |
| if not selected_item: | |
| return show_cart(), gr.update(choices=get_cart_choices(), value=None), "Please select an item." | |
| item_id = int(selected_item.split(" - ")[0]) | |
| item = inventory_by_id[item_id] | |
| my_cart.append({ | |
| "id": item_id, | |
| "name": item["name"], | |
| "price": item["price"] | |
| }) | |
| return ( | |
| show_cart(), | |
| gr.update(choices=get_cart_choices(), value=None), | |
| f"Added {item['name']} to cart." | |
| ) | |
| def remove_tool(selected_cart_item): | |
| if not selected_cart_item: | |
| return show_cart(), gr.update(choices=get_cart_choices(), value=None), "Please select a cart item to remove." | |
| index = int(selected_cart_item.split(":")[0]) | |
| removed_item = my_cart.pop(index) | |
| return ( | |
| show_cart(), | |
| gr.update(choices=get_cart_choices(), value=None), | |
| f"Removed {removed_item['name']} from cart." | |
| ) | |
| def calculate_total(): | |
| total = sum(item["price"] for item in my_cart) | |
| return f"Total Bill: ${total}" | |
| with gr.Blocks(title="Hardware Store Billing System") as demo: | |
| gr.Markdown("## Hardware Store Billing System") | |
| with gr.Row(): | |
| item_dropdown = gr.Dropdown( | |
| choices=get_inventory_choices(), | |
| label="Select a tool from inventory" | |
| ) | |
| cart_dropdown = gr.Dropdown( | |
| choices=get_cart_choices(), | |
| label="Select a tool from cart to remove" | |
| ) | |
| with gr.Row(): | |
| add_button = gr.Button("Add Tool") | |
| remove_button = gr.Button("Remove Tool") | |
| cart_display = gr.Textbox( | |
| label="Cart Contents", | |
| lines=10, | |
| interactive=False | |
| ) | |
| status_output = gr.Textbox( | |
| label="Status Message", | |
| interactive=False | |
| ) | |
| total_button = gr.Button("Calculate Total") | |
| total_output = gr.Textbox( | |
| label="Final Bill", | |
| interactive=False | |
| ) | |
| add_button.click( | |
| fn=add_tool, | |
| inputs=item_dropdown, | |
| outputs=[cart_display, cart_dropdown, status_output] | |
| ) | |
| remove_button.click( | |
| fn=remove_tool, | |
| inputs=cart_dropdown, | |
| outputs=[cart_display, cart_dropdown, status_output] | |
| ) | |
| total_button.click( | |
| fn=calculate_total, | |
| inputs=[], | |
| outputs=total_output | |
| ) | |
| demo.launch() |