# If running in a notebook: # !pip install gradio import gradio as gr Inventory = [ (1, "Apples", 250), (2, "Cherry", 650), (3, "Chickoo", 50), (4, "Grapes", 90), (5, "Mangoes", 150), ] Your_Cart = [] def add_item_to_cart(item_id, inventory): """Adds an item from the inventory to the shopping cart.""" try: item = inventory[item_id - 1] Your_Cart.append(item) return f"{'>'*10} The item '{item[1]}' has been added to the cart. {'<'*10}\n\n" except (IndexError, TypeError): return f"Invalid item ID: {item_id}\n\n" def remove_item(cart_index, cart_items): """Removes an item from the shopping cart by displayed index.""" try: removed_item = cart_items.pop(cart_index - 1) return f"{'>'*10} The item '{removed_item[1]}' has been removed from the cart, and your shopping cart has been updated. {'<'*10}\n\n" except (IndexError, TypeError): return "Invalid cart index or cart is empty.\n\n" def display_cart(cart_items): """Displays the items in the shopping cart (stable, one line per item in cart order).""" if not cart_items: return f"{'>'*10} Your cart is empty !!!, Why not buy Fruits from us ... {'<'*10}\n\n" cart_str = "" cart_str += "+" * 70 + "\n" cart_str += f"{'Your Cart'.center(60)}\n" cart_str += "+" * 70 + "\n" cart_str += f"{'Index':<10} {'Name':<20} {'Price Per KG':<15}\n" cart_str += "-" * 70 + "\n" total = 0 for idx, item in enumerate(cart_items, start=1): cart_str += f"{idx:<10} {item[1]:<20} ${item[2]:<15.2f}\n" total += item[2] cart_str += "-" * 70 + "\n" cart_str += f"{'Total Cost':<32} ${total:<10.2f}\n" return cart_str def total_cost(cart_items): """Calculates total cost of all items in the cart.""" total = sum(item[2] for item in cart_items) return "~" * 40 + "\n" + f"Total cost: {total}".center(40) + "\n" + "~" * 40 + "\n\n" def gradio_interface(item_id, cart_index): """ Handles adding/removing items, displaying the cart, and calculating total cost. """ # Convert inputs (Gradio Numbers are floats) def to_int(x): try: if x is None: return 0 return int(x) except (ValueError, TypeError): return 0 item_id = to_int(item_id) cart_index = to_int(cart_index) add_msg = "" if item_id > 0: add_msg = add_item_to_cart(item_id, Inventory) remove_msg = "" if cart_index > 0: remove_msg = remove_item(cart_index, Your_Cart) cart_display = display_cart(Your_Cart) cost_msg = total_cost(Your_Cart) return cart_display, add_msg, remove_msg, cost_msg def launch_gradio(): with gr.Blocks() as demo: gr.Markdown("### Fruit Shopping Cart System") gr.Markdown( "Inventory:\n" + "\n".join([f"- {pid}: {name} (${price}/kg)" for pid, name, price in Inventory]) ) with gr.Row(): item_id_input = gr.Number(label="Enter Product ID to Add to Cart", precision=0) cart_index_input = gr.Number(label="Enter Cart Index to Remove", precision=0) # Hidden zero inputs created ONCE (don’t create components inside .click) zero_item = gr.Number(value=0, visible=False, precision=0) zero_cart = gr.Number(value=0, visible=False, precision=0) with gr.Row(): add_button = gr.Button("Add to Cart") remove_button = gr.Button("Remove from Cart") refresh_button = gr.Button("Refresh Cart View") cart_display_output = gr.Textbox(label="Your Cart", interactive=False, lines=12) add_message_output = gr.Textbox(label="Add Item Message", interactive=False, lines=2) remove_message_output = gr.Textbox(label="Remove Item Message", interactive=False, lines=2) total_cost_output = gr.Textbox(label="Total Cost", interactive=False, lines=4) add_button.click( fn=gradio_interface, inputs=[item_id_input, zero_cart], outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output] ) remove_button.click( fn=gradio_interface, inputs=[zero_item, cart_index_input], outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output] ) refresh_button.click( fn=gradio_interface, inputs=[zero_item, zero_cart], outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output] ) demo.launch() if __name__ == "__main__": launch_gradio()