Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| # -------------- Data Structures -------------- | |
| Inventory = [ | |
| (1, "Apples", 250), | |
| (2, "Cherry", 650), | |
| (3, "Chickoo", 50), | |
| (4, "Grapes", 90), | |
| (5, "Mangoes", 150), | |
| ] | |
| # make lookup by ID | |
| inventory_dict = { | |
| item_id: {"name": name, "price": price} | |
| for item_id, name, price in Inventory | |
| } | |
| def calculate_total(cart): | |
| return sum(entry["quantity"] * entry["unit_price"] for entry in cart) | |
| # -------------- Cart Operations -------------- | |
| def add_to_cart(selection, cart): | |
| """ | |
| - selection: string like "3 - Chickoo ($50)" | |
| - cart: list of dicts carried in gr.State | |
| """ | |
| item_id = int(selection.split(" - ")[0]) | |
| item_info = inventory_dict[item_id] | |
| # see if it’s already in cart | |
| for entry in cart: | |
| if entry["id"] == item_id: | |
| entry["quantity"] += 1 | |
| break | |
| else: | |
| cart.append({ | |
| "id": item_id, | |
| "name": item_info["name"], | |
| "unit_price": item_info["price"], | |
| "quantity": 1 | |
| }) | |
| # build table for display | |
| table = [] | |
| for idx, e in enumerate(cart): | |
| line_total = e["quantity"] * e["unit_price"] | |
| table.append([idx + 1, e["name"], e["quantity"], e["unit_price"], line_total]) | |
| total = calculate_total(cart) | |
| return cart, table, f"${total:.2f}" | |
| def remove_from_cart(idx, cart): | |
| """ | |
| Remove entire line by its displayed index. | |
| """ | |
| i = int(idx) - 1 | |
| if 0 <= i < len(cart): | |
| cart.pop(i) | |
| table = [] | |
| for j, e in enumerate(cart): | |
| line_total = e["quantity"] * e["unit_price"] | |
| table.append([j + 1, e["name"], e["quantity"], e["unit_price"], line_total]) | |
| total = calculate_total(cart) | |
| return cart, table, f"${total:.2f}" | |
| # -------------- Gradio UI -------------- | |
| with gr.Blocks(title="Shopping Cart with Aggregation") as demo: | |
| gr.Markdown("## 🛒 Shopping Cart (Aggregated)") | |
| with gr.Row(): | |
| product_dd = gr.Dropdown( | |
| choices=[f"{i} - {n} (${p})" for i, n, p in Inventory], | |
| label="Select Product to Add" | |
| ) | |
| add_btn = gr.Button("Add to Cart") | |
| cart_state = gr.State([]) | |
| cart_display = gr.Dataframe( | |
| headers=["#", "Name", "Quantity", "Unit Price", "Line Total"], | |
| interactive=False, | |
| label="Your Cart" | |
| ) | |
| total_box = gr.Textbox(value="$0.00", label="Total", interactive=False) | |
| add_btn.click( | |
| fn=add_to_cart, | |
| inputs=[product_dd, cart_state], | |
| outputs=[cart_state, cart_display, total_box] | |
| ) | |
| gr.Markdown("---") | |
| with gr.Row(): | |
| remove_idx = gr.Number(value=1, label="Remove Line #", precision=0) | |
| remove_btn = gr.Button("Remove from Cart") | |
| remove_btn.click( | |
| fn=remove_from_cart, | |
| inputs=[remove_idx, cart_state], | |
| outputs=[cart_state, cart_display, total_box] | |
| ) | |
| gr.Markdown( | |
| """ | |
| **Usage** | |
| 1. **Add to Cart**: duplicates increase quantity. | |
| 2. **Remove Line #**: deletes that product entirely. | |
| 3. **Total** auto-recalculates. | |
| """ | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |