requirements.txt
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Initial Inventory Data: (ID, Name, Price per KG)
|
| 4 |
+
inventory = [
|
| 5 |
+
(101, "Apple", 120),
|
| 6 |
+
(102, "Banana", 50),
|
| 7 |
+
(103, "Mango", 80),
|
| 8 |
+
(104, "Grapes", 90),
|
| 9 |
+
(105, "Orange", 70)
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
# Global cart state for the session
|
| 13 |
+
shopping_cart = []
|
| 14 |
+
|
| 15 |
+
def get_inventory_table():
|
| 16 |
+
"""Formats inventory for display."""
|
| 17 |
+
table = "| ID | Product Name | Price (per KG) |\n|---|---|---|\n"
|
| 18 |
+
for item in inventory:
|
| 19 |
+
table += f"| {item[0]} | {item[1]} | βΉ{item[2]} |\n"
|
| 20 |
+
return table
|
| 21 |
+
|
| 22 |
+
def add_item_to_cart(item_id):
|
| 23 |
+
"""Adds an item from inventory to the global cart."""
|
| 24 |
+
try:
|
| 25 |
+
item_id = int(item_id)
|
| 26 |
+
# Find item in inventory
|
| 27 |
+
product = next((item for item in inventory if item[0] == item_id), None)
|
| 28 |
+
|
| 29 |
+
if product:
|
| 30 |
+
shopping_cart.append(product)
|
| 31 |
+
return f"β
Added {product[1]} to cart!", display_cart(), total_cost()
|
| 32 |
+
else:
|
| 33 |
+
return "β Error: Product ID not found in inventory.", display_cart(), total_cost()
|
| 34 |
+
except ValueError:
|
| 35 |
+
return "β Error: Please enter a valid numerical ID.", display_cart(), total_cost()
|
| 36 |
+
|
| 37 |
+
def remove_item(cart_index):
|
| 38 |
+
"""Removes an item from the cart based on its display index."""
|
| 39 |
+
try:
|
| 40 |
+
idx = int(cart_index) - 1 # Convert 1-based UI index to 0-based list index
|
| 41 |
+
if 0 <= idx < len(shopping_cart):
|
| 42 |
+
removed = shopping_cart.pop(idx)
|
| 43 |
+
return f"ποΈ Removed {removed[1]} from cart.", display_cart(), total_cost()
|
| 44 |
+
else:
|
| 45 |
+
return "β Error: Invalid cart index.", display_cart(), total_cost()
|
| 46 |
+
except ValueError:
|
| 47 |
+
return "β Error: Please enter a valid numerical index.", display_cart(), total_cost()
|
| 48 |
+
|
| 49 |
+
def display_cart():
|
| 50 |
+
"""Returns a formatted string representing the cart."""
|
| 51 |
+
if not shopping_cart:
|
| 52 |
+
return "Your cart is currently empty."
|
| 53 |
+
|
| 54 |
+
cart_display = "| # | Name | Price |\n|---|---|---|\n"
|
| 55 |
+
for i, item in enumerate(shopping_cart, 1):
|
| 56 |
+
cart_display += f"| {i} | {item[1]} | βΉ{item[2]} |\n"
|
| 57 |
+
return cart_display
|
| 58 |
+
|
| 59 |
+
def total_cost():
|
| 60 |
+
"""Calculates the sum of prices in the cart."""
|
| 61 |
+
total = sum(item[2] for item in shopping_cart)
|
| 62 |
+
return f"### Total Bill: βΉ{total}"
|
| 63 |
+
|
| 64 |
+
# --- Gradio Interface ---
|
| 65 |
+
|
| 66 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 67 |
+
gr.Markdown("# π Fruit Seller Shopping Cart System")
|
| 68 |
+
gr.Markdown("Manage your inventory and billing seamlessly.")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
with gr.Column():
|
| 72 |
+
gr.Markdown("### π¦ Available Inventory")
|
| 73 |
+
gr.Markdown(get_inventory_table())
|
| 74 |
+
|
| 75 |
+
product_input = gr.Textbox(label="Enter Product ID to Add", placeholder="e.g., 101")
|
| 76 |
+
add_btn = gr.Button("Add to Cart", variant="primary")
|
| 77 |
+
|
| 78 |
+
with gr.Column():
|
| 79 |
+
gr.Markdown("### π Your Shopping Cart")
|
| 80 |
+
cart_output = gr.Markdown(display_cart())
|
| 81 |
+
total_output = gr.Markdown(total_cost())
|
| 82 |
+
|
| 83 |
+
remove_input = gr.Textbox(label="Enter Cart # to Remove", placeholder="e.g., 1")
|
| 84 |
+
remove_btn = gr.Button("Remove Item", variant="stop")
|
| 85 |
+
|
| 86 |
+
status_msg = gr.Textbox(label="System Status", interactive=False)
|
| 87 |
+
|
| 88 |
+
# Interactivity
|
| 89 |
+
add_btn.click(
|
| 90 |
+
add_item_to_cart,
|
| 91 |
+
inputs=[product_input],
|
| 92 |
+
outputs=[status_msg, cart_output, total_output]
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
remove_btn.click(
|
| 96 |
+
remove_item,
|
| 97 |
+
inputs=[remove_input],
|
| 98 |
+
outputs=[status_msg, cart_output, total_output]
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|