|
import gradio as gr |
|
|
|
""" |
|
Create a Gradio interface for the complete code of the above case study. |
|
The interface should allow users to add items to the cart, remove items, display the cart, and calculate the total cost interactively. |
|
""" |
|
|
|
from gradio import Blocks, Number, Button, Textbox |
|
|
|
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: |
|
return f"Invalid item ID: {item_id}" |
|
|
|
def remove_item(cart_index, cart_items): |
|
""" |
|
Removes an item from the shopping cart. |
|
""" |
|
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: |
|
return "Invalid cart index or cart is empty." |
|
|
|
def display_cart(Your_Cart): |
|
""" |
|
Displays the items in the shopping cart. |
|
""" |
|
if Your_Cart: |
|
cart_str = "" |
|
cart_str += "+" * 50 + "\n" |
|
cart_str += f"{'Your Cart'.center(40)}" + "\n" |
|
cart_str += "+" * 50 + "\n" |
|
cart_str += f"{'Index':<10} {'Name':<20} {'Quantity':<10} {'Price Per KG':<10}" + "\n" |
|
cart_str += "-" * 50 + "\n" |
|
unique_items = set(Your_Cart) |
|
total_cost = 0 |
|
for index, item in enumerate(unique_items): |
|
quantity = Your_Cart.count(item) |
|
price_per_kg = item[2] |
|
total_price = price_per_kg * quantity |
|
cart_str += f"{index+1:<10} {item[1]:<20} {quantity:<10} ${total_price:<10.2f}" + "\n" |
|
total_cost += total_price |
|
cart_str += "-" * 50 + "\n" |
|
cart_str += f"{'Total Cost':<40} ${total_cost:<10.2f}" |
|
return cart_str |
|
else: |
|
return f"{'>'*10} Your cart is empty !!!, Why not buy Fruits from us ... {'<'*10} \n\n" |
|
|
|
def total_cost(cart_items): |
|
""" |
|
Calculates the total cost of all items in the shopping cart. |
|
""" |
|
total = 0 |
|
for item in cart_items: |
|
total += item[2] |
|
return f"~"*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 the total cost. |
|
|
|
Args: |
|
item_id (int): The product ID to add to the cart (if any). |
|
cart_index (int): The product ID to remove from the cart (if any). |
|
|
|
Returns: |
|
tuple: A tuple containing the updated cart display, add message, remove message, and total cost. |
|
""" |
|
|
|
|
|
add_msg = "" |
|
if item_id: |
|
add_msg = add_item_to_cart(item_id, Inventory) |
|
|
|
|
|
remove_msg = "" |
|
if cart_index: |
|
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(): |
|
""" |
|
Launches the Gradio interface for the fruit shopping cart system. |
|
""" |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("### Fruit Shopping Cart System") |
|
|
|
with gr.Row(): |
|
item_id_input = gr.Number(label="Enter Product ID to Add to Cart", interactive=True) |
|
cart_index_input = gr.Number(label="Enter Index ID to Remove from Cart", interactive=True) |
|
|
|
with gr.Row(): |
|
add_button = gr.Button("Add to Cart") |
|
remove_button = gr.Button("Remove from Cart") |
|
view_button = gr.Button("View Cart") |
|
clear_button = gr.Button("Clear Cart") |
|
|
|
|
|
cart_display_output = gr.Textbox(label="Your Cart", interactive=False) |
|
add_message_output = gr.Textbox(label="Add Item Message", interactive=False) |
|
remove_message_output = gr.Textbox(label="Remove Item Message", interactive=False) |
|
total_cost_output = gr.Textbox(label="Total Cost", interactive=False) |
|
|
|
|
|
add_button.click( |
|
gradio_interface, |
|
inputs=[item_id_input, gr.Number(visible=False, value=0)], |
|
outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output] |
|
) |
|
|
|
|
|
remove_button.click( |
|
gradio_interface, |
|
inputs=[gr.Number(visible=False, value=0), cart_index_input], |
|
outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output] |
|
) |
|
|
|
|
|
view_button.click( |
|
gradio_interface, |
|
outputs=[cart_display_output] |
|
) |
|
demo.launch() |
|
|
|
|
|
if __name__ == "__main__": |
|
launch_gradio() |