Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
from gradio import Blocks, Number, Button, Textbox
|
| 4 |
|
| 5 |
Inventory = [
|
| 6 |
(1, "Apples", 250),
|
|
@@ -12,126 +13,123 @@ Inventory = [
|
|
| 12 |
|
| 13 |
Your_Cart = []
|
| 14 |
|
| 15 |
-
def add_item_to_cart(item_id,
|
| 16 |
-
"""
|
| 17 |
-
Adds an item from the Inventory to the shopping cart.
|
| 18 |
-
"""
|
| 19 |
try:
|
| 20 |
-
item =
|
| 21 |
Your_Cart.append(item)
|
| 22 |
-
return f"{'>'*10} The item '{item[1]}' has been added to the cart. {'<'*10}
|
| 23 |
-
except IndexError:
|
| 24 |
-
return f"Invalid item ID: {item_id}"
|
| 25 |
|
| 26 |
def remove_item(cart_index, cart_items):
|
| 27 |
-
"""
|
| 28 |
-
Removes an item from the shopping cart.
|
| 29 |
-
"""
|
| 30 |
try:
|
| 31 |
removed_item = cart_items.pop(cart_index - 1)
|
| 32 |
-
return f"{'>'*10} The item '{removed_item[1]}' has been removed from the cart, and your shopping cart has been updated. {'<'*10}
|
| 33 |
-
except IndexError:
|
| 34 |
-
return "Invalid cart index or cart is empty."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
cart_str += "+" * 50 + "\n"
|
| 45 |
-
cart_str += f"{'Index':<10} {'Name':<20} {'Quantity':<10} {'Price Per KG':<10}" + "\n"
|
| 46 |
-
cart_str += "-" * 50 + "\n"
|
| 47 |
-
unique_items = set(Your_Cart) # Get unique items in the cart
|
| 48 |
-
total_cost = 0
|
| 49 |
-
for index, item in enumerate(unique_items):
|
| 50 |
-
quantity = Your_Cart.count(item)
|
| 51 |
-
price_per_kg = item[2]
|
| 52 |
-
total_price = price_per_kg * quantity
|
| 53 |
-
cart_str += f"{index+1:<10} {item[1]:<20} {quantity:<10} ${total_price:<10.2f}" + "\n"
|
| 54 |
-
total_cost += total_price
|
| 55 |
-
cart_str += "-" * 50 + "\n"
|
| 56 |
-
cart_str += f"{'Total Cost':<40} ${total_cost:<10.2f}" # Display grand total
|
| 57 |
-
return cart_str # Return the string representation of the cart
|
| 58 |
-
else:
|
| 59 |
-
return f"{'>'*10} Your cart is empty !!!, Why not buy Fruits from us ... {'<'*10} \n\n"
|
| 60 |
|
| 61 |
def total_cost(cart_items):
|
| 62 |
-
"""
|
| 63 |
-
|
| 64 |
-
"""
|
| 65 |
-
total = 0
|
| 66 |
-
for item in cart_items:
|
| 67 |
-
total += item[2] # Assuming quantity is 1 for now
|
| 68 |
-
return f"~"*40 + "\n" + f"Total cost: {total}".center(40) + "\n" + "~"*40 + "\n\n"
|
| 69 |
|
| 70 |
def gradio_interface(item_id, cart_index):
|
| 71 |
"""
|
| 72 |
-
Handles adding/removing items, displaying the cart, and calculating
|
| 73 |
-
|
| 74 |
-
Args:
|
| 75 |
-
item_id (int): The product ID to add to the cart (if any).
|
| 76 |
-
cart_index (int): The product ID to remove from the cart (if any).
|
| 77 |
-
|
| 78 |
-
Returns:
|
| 79 |
-
tuple: A tuple containing the updated cart display, add message, remove message, and total cost.
|
| 80 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
# Add item
|
| 83 |
add_msg = ""
|
| 84 |
-
if item_id:
|
| 85 |
add_msg = add_item_to_cart(item_id, Inventory)
|
| 86 |
|
| 87 |
-
# Remove item
|
| 88 |
remove_msg = ""
|
| 89 |
-
if cart_index:
|
| 90 |
remove_msg = remove_item(cart_index, Your_Cart)
|
| 91 |
|
| 92 |
-
# Display the cart and calculate total cost
|
| 93 |
cart_display = display_cart(Your_Cart)
|
| 94 |
cost_msg = total_cost(Your_Cart)
|
| 95 |
|
| 96 |
return cart_display, add_msg, remove_msg, cost_msg
|
| 97 |
|
| 98 |
-
# Gradio interface creation
|
| 99 |
def launch_gradio():
|
| 100 |
-
"""
|
| 101 |
-
Launches the Gradio interface for the fruit shopping cart system.
|
| 102 |
-
"""
|
| 103 |
-
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
gr.Markdown("### Fruit Shopping Cart System")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
with gr.Row():
|
| 108 |
-
item_id_input = gr.Number(label="Enter Product ID to Add to Cart",
|
| 109 |
-
cart_index_input = gr.Number(label="Enter Index
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
add_button = gr.Button("Add to Cart")
|
| 113 |
remove_button = gr.Button("Remove from Cart")
|
|
|
|
| 114 |
|
| 115 |
-
cart_display_output = gr.Textbox(label="Your Cart", interactive=False)
|
| 116 |
-
add_message_output = gr.Textbox(label="Add Item Message", interactive=False)
|
| 117 |
-
remove_message_output = gr.Textbox(label="Remove Item Message", interactive=False)
|
| 118 |
-
total_cost_output = gr.Textbox(label="Total Cost", interactive=False)
|
| 119 |
|
| 120 |
-
# Add item button click behavior
|
| 121 |
add_button.click(
|
| 122 |
-
gradio_interface,
|
| 123 |
-
inputs=[item_id_input,
|
| 124 |
outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output]
|
| 125 |
)
|
| 126 |
|
| 127 |
-
# Remove item button click behavior
|
| 128 |
remove_button.click(
|
| 129 |
-
gradio_interface,
|
| 130 |
-
inputs=[
|
| 131 |
outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output]
|
| 132 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
demo.launch()
|
| 134 |
|
| 135 |
-
# Run the Gradio interface
|
| 136 |
if __name__ == "__main__":
|
| 137 |
launch_gradio()
|
|
|
|
| 1 |
+
# If running in a notebook:
|
| 2 |
+
# !pip install gradio
|
| 3 |
+
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
Inventory = [
|
| 7 |
(1, "Apples", 250),
|
|
|
|
| 13 |
|
| 14 |
Your_Cart = []
|
| 15 |
|
| 16 |
+
def add_item_to_cart(item_id, inventory):
|
| 17 |
+
"""Adds an item from the inventory to the shopping cart."""
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
item = inventory[item_id - 1]
|
| 20 |
Your_Cart.append(item)
|
| 21 |
+
return f"{'>'*10} The item '{item[1]}' has been added to the cart. {'<'*10}\n\n"
|
| 22 |
+
except (IndexError, TypeError):
|
| 23 |
+
return f"Invalid item ID: {item_id}\n\n"
|
| 24 |
|
| 25 |
def remove_item(cart_index, cart_items):
|
| 26 |
+
"""Removes an item from the shopping cart by displayed index."""
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
removed_item = cart_items.pop(cart_index - 1)
|
| 29 |
+
return f"{'>'*10} The item '{removed_item[1]}' has been removed from the cart, and your shopping cart has been updated. {'<'*10}\n\n"
|
| 30 |
+
except (IndexError, TypeError):
|
| 31 |
+
return "Invalid cart index or cart is empty.\n\n"
|
| 32 |
+
|
| 33 |
+
def display_cart(cart_items):
|
| 34 |
+
"""Displays the items in the shopping cart (stable, one line per item in cart order)."""
|
| 35 |
+
if not cart_items:
|
| 36 |
+
return f"{'>'*10} Your cart is empty !!!, Why not buy Fruits from us ... {'<'*10}\n\n"
|
| 37 |
+
|
| 38 |
+
cart_str = ""
|
| 39 |
+
cart_str += "+" * 70 + "\n"
|
| 40 |
+
cart_str += f"{'Your Cart'.center(60)}\n"
|
| 41 |
+
cart_str += "+" * 70 + "\n"
|
| 42 |
+
cart_str += f"{'Index':<10} {'Name':<20} {'Price Per KG':<15}\n"
|
| 43 |
+
cart_str += "-" * 70 + "\n"
|
| 44 |
|
| 45 |
+
total = 0
|
| 46 |
+
for idx, item in enumerate(cart_items, start=1):
|
| 47 |
+
cart_str += f"{idx:<10} {item[1]:<20} ${item[2]:<15.2f}\n"
|
| 48 |
+
total += item[2]
|
| 49 |
+
|
| 50 |
+
cart_str += "-" * 70 + "\n"
|
| 51 |
+
cart_str += f"{'Total Cost':<32} ${total:<10.2f}\n"
|
| 52 |
+
return cart_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def total_cost(cart_items):
|
| 55 |
+
"""Calculates total cost of all items in the cart."""
|
| 56 |
+
total = sum(item[2] for item in cart_items)
|
| 57 |
+
return "~" * 40 + "\n" + f"Total cost: {total}".center(40) + "\n" + "~" * 40 + "\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def gradio_interface(item_id, cart_index):
|
| 60 |
"""
|
| 61 |
+
Handles adding/removing items, displaying the cart, and calculating total cost.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
"""
|
| 63 |
+
# Convert inputs (Gradio Numbers are floats)
|
| 64 |
+
def to_int(x):
|
| 65 |
+
try:
|
| 66 |
+
if x is None:
|
| 67 |
+
return 0
|
| 68 |
+
return int(x)
|
| 69 |
+
except (ValueError, TypeError):
|
| 70 |
+
return 0
|
| 71 |
+
|
| 72 |
+
item_id = to_int(item_id)
|
| 73 |
+
cart_index = to_int(cart_index)
|
| 74 |
|
|
|
|
| 75 |
add_msg = ""
|
| 76 |
+
if item_id > 0:
|
| 77 |
add_msg = add_item_to_cart(item_id, Inventory)
|
| 78 |
|
|
|
|
| 79 |
remove_msg = ""
|
| 80 |
+
if cart_index > 0:
|
| 81 |
remove_msg = remove_item(cart_index, Your_Cart)
|
| 82 |
|
|
|
|
| 83 |
cart_display = display_cart(Your_Cart)
|
| 84 |
cost_msg = total_cost(Your_Cart)
|
| 85 |
|
| 86 |
return cart_display, add_msg, remove_msg, cost_msg
|
| 87 |
|
|
|
|
| 88 |
def launch_gradio():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown("### Fruit Shopping Cart System")
|
| 91 |
+
gr.Markdown(
|
| 92 |
+
"Inventory:\n"
|
| 93 |
+
+ "\n".join([f"- {pid}: {name} (${price}/kg)" for pid, name, price in Inventory])
|
| 94 |
+
)
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
+
item_id_input = gr.Number(label="Enter Product ID to Add to Cart", precision=0)
|
| 98 |
+
cart_index_input = gr.Number(label="Enter Cart Index to Remove", precision=0)
|
| 99 |
+
|
| 100 |
+
# Hidden zero inputs created ONCE (don’t create components inside .click)
|
| 101 |
+
zero_item = gr.Number(value=0, visible=False, precision=0)
|
| 102 |
+
zero_cart = gr.Number(value=0, visible=False, precision=0)
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
add_button = gr.Button("Add to Cart")
|
| 106 |
remove_button = gr.Button("Remove from Cart")
|
| 107 |
+
refresh_button = gr.Button("Refresh Cart View")
|
| 108 |
|
| 109 |
+
cart_display_output = gr.Textbox(label="Your Cart", interactive=False, lines=12)
|
| 110 |
+
add_message_output = gr.Textbox(label="Add Item Message", interactive=False, lines=2)
|
| 111 |
+
remove_message_output = gr.Textbox(label="Remove Item Message", interactive=False, lines=2)
|
| 112 |
+
total_cost_output = gr.Textbox(label="Total Cost", interactive=False, lines=4)
|
| 113 |
|
|
|
|
| 114 |
add_button.click(
|
| 115 |
+
fn=gradio_interface,
|
| 116 |
+
inputs=[item_id_input, zero_cart],
|
| 117 |
outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output]
|
| 118 |
)
|
| 119 |
|
|
|
|
| 120 |
remove_button.click(
|
| 121 |
+
fn=gradio_interface,
|
| 122 |
+
inputs=[zero_item, cart_index_input],
|
| 123 |
outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output]
|
| 124 |
)
|
| 125 |
+
|
| 126 |
+
refresh_button.click(
|
| 127 |
+
fn=gradio_interface,
|
| 128 |
+
inputs=[zero_item, zero_cart],
|
| 129 |
+
outputs=[cart_display_output, add_message_output, remove_message_output, total_cost_output]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
demo.launch()
|
| 133 |
|
|
|
|
| 134 |
if __name__ == "__main__":
|
| 135 |
launch_gradio()
|