Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
# 1. Mock Inventory (Simulates your database)
|
| 5 |
+
INVENTORY = {
|
| 6 |
+
"Apples (1kg)": 2.50,
|
| 7 |
+
"Bananas (1kg)": 1.20,
|
| 8 |
+
"Oranges (1kg)": 1.80,
|
| 9 |
+
"Mangoes (1kg)": 4.00,
|
| 10 |
+
"Strawberries (box)": 3.50,
|
| 11 |
+
"Watermelon (each)": 5.00
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def format_cart_df(cart):
|
| 15 |
+
"""Formats the cart state into a pandas DataFrame for UI display."""
|
| 16 |
+
if not cart:
|
| 17 |
+
return pd.DataFrame(columns=["Product", "Price ($)", "Quantity", "Total ($)"])
|
| 18 |
+
df = pd.DataFrame(cart)
|
| 19 |
+
# Reorder and format columns
|
| 20 |
+
df = df[["name", "price", "quantity", "total"]]
|
| 21 |
+
df.columns = ["Product", "Price ($)", "Quantity", "Total ($)"]
|
| 22 |
+
return df
|
| 23 |
+
|
| 24 |
+
def calculate_total(cart):
|
| 25 |
+
"""Calculates the sum total of the cart."""
|
| 26 |
+
total = sum(item['total'] for item in cart)
|
| 27 |
+
return f"${total:.2f}"
|
| 28 |
+
|
| 29 |
+
def add_item(cart, fruit, quantity):
|
| 30 |
+
"""Adds a product to the cart or updates its quantity if it already exists."""
|
| 31 |
+
if not fruit or quantity <= 0:
|
| 32 |
+
return cart, format_cart_df(cart), gr.update(), calculate_total(cart)
|
| 33 |
+
|
| 34 |
+
price = INVENTORY[fruit]
|
| 35 |
+
found = False
|
| 36 |
+
|
| 37 |
+
for item in cart:
|
| 38 |
+
if item['name'] == fruit:
|
| 39 |
+
item['quantity'] += quantity
|
| 40 |
+
item['total'] = item['quantity'] * price
|
| 41 |
+
found = True
|
| 42 |
+
break
|
| 43 |
+
|
| 44 |
+
if not found:
|
| 45 |
+
cart.append({
|
| 46 |
+
"name": fruit,
|
| 47 |
+
"price": price,
|
| 48 |
+
"quantity": quantity,
|
| 49 |
+
"total": price * quantity
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
choices = [item['name'] for item in cart]
|
| 53 |
+
return cart, format_cart_df(cart), gr.update(choices=choices), calculate_total(cart)
|
| 54 |
+
|
| 55 |
+
def remove_item(cart, fruit_to_remove):
|
| 56 |
+
"""Removes a specific product entirely from the cart."""
|
| 57 |
+
if fruit_to_remove:
|
| 58 |
+
cart = [item for item in cart if item['name'] != fruit_to_remove]
|
| 59 |
+
|
| 60 |
+
choices = [item['name'] for item in cart]
|
| 61 |
+
return cart, format_cart_df(cart), gr.update(choices=choices, value=None), calculate_total(cart)
|
| 62 |
+
|
| 63 |
+
# 2. Gradio User Interface
|
| 64 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Fruit Cart POS") as demo:
|
| 65 |
+
gr.Markdown("# 🛒 Fruit Seller Shopping Cart System")
|
| 66 |
+
gr.Markdown("Streamline billing by adding items from the inventory, removing mistakes, and generating an automated total bill.")
|
| 67 |
+
|
| 68 |
+
# Hidden state to store cart data across interactions
|
| 69 |
+
cart_state = gr.State([])
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
# Left Column: Controls
|
| 73 |
+
with gr.Column(scale=1):
|
| 74 |
+
gr.Markdown("### ➕ Add Products")
|
| 75 |
+
fruit_dropdown = gr.Dropdown(choices=list(INVENTORY.keys()), label="Select Product")
|
| 76 |
+
quantity_input = gr.Number(value=1, minimum=1, label="Quantity")
|
| 77 |
+
add_btn = gr.Button("Add to Cart", variant="primary")
|
| 78 |
+
|
| 79 |
+
gr.Markdown("---")
|
| 80 |
+
|
| 81 |
+
gr.Markdown("### ➖ Remove Products")
|
| 82 |
+
remove_dropdown = gr.Dropdown(choices=[], label="Select Product to Remove", interactive=True)
|
| 83 |
+
remove_btn = gr.Button("Remove Item", variant="stop")
|
| 84 |
+
|
| 85 |
+
# Right Column: Cart Display
|
| 86 |
+
with gr.Column(scale=2):
|
| 87 |
+
gr.Markdown("### 🧾 Current Cart & Billing")
|
| 88 |
+
cart_display = gr.Dataframe(
|
| 89 |
+
headers=["Product", "Price ($)", "Quantity", "Total ($)"],
|
| 90 |
+
interactive=False,
|
| 91 |
+
row_count=(5, "dynamic")
|
| 92 |
+
)
|
| 93 |
+
total_display = gr.Textbox(label="Total Bill", value="$0.00", text_align="right")
|
| 94 |
+
|
| 95 |
+
# 3. Event Listeners (Wiring UI to logic)
|
| 96 |
+
add_btn.click(
|
| 97 |
+
fn=add_item,
|
| 98 |
+
inputs=[cart_state, fruit_dropdown, quantity_input],
|
| 99 |
+
outputs=[cart_state, cart_display, remove_dropdown, total_display]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
remove_btn.click(
|
| 103 |
+
fn=remove_item,
|
| 104 |
+
inputs=[cart_state, remove_dropdown],
|
| 105 |
+
outputs=[cart_state, cart_display, remove_dropdown, total_display]
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Run the app
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
demo.launch()
|