Cart / app.py
dschandra's picture
Update app.py
cbf0cb6 verified
raw
history blame
3.63 kB
import gradio as gr
from simple_salesforce import Salesforce
# Salesforce Connection
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
# Initialize cart
cart = {}
# Fetch menu items from Salesforce
def fetch_menu_items():
query = "SELECT Id, Name, Price__c, Description__c, Image1__c FROM Menu_Item__c"
menu_items = sf.query(query)["records"]
return menu_items
# Generate menu HTML with add-to-cart functionality
def generate_menu_html(menu_items):
html = ""
for item in menu_items:
html += f"""
<div style='border: 1px solid #ccc; padding: 10px; margin: 10px; display: flex; align-items: center;'>
<img src='{item.get('Image1__c', '')}' style='width: 100px; height: 100px; margin-right: 10px;' />
<div>
<h3>{item['Name']}</h3>
<p>{item.get('Description__c', '')}</p>
<p>₹{item['Price__c']}</p>
<div style='display: flex; align-items: center;'>
<button onclick="decreaseQuantity('{item['Id']}')">-</button>
<span id="quantity-{item['Id']}" style="margin: 0 10px;">0</span>
<button onclick="increaseQuantity('{item['Id']}')">+</button>
</div>
</div>
</div>
"""
return html
# Update cart when items are added
def update_cart(item_id, quantity):
if item_id in cart:
cart[item_id]["quantity"] = quantity
else:
menu_item = sf.Menu_Item__c.get(item_id)
cart[item_id] = {
"name": menu_item["Name"],
"price": menu_item["Price__c"],
"quantity": quantity
}
return view_cart()
# View cart details dynamically
def view_cart():
cart_html = "<h3>Cart Details</h3><ul>"
total_price = 0
for item_id, details in cart.items():
item_total = details["price"] * details["quantity"]
total_price += item_total
cart_html += f"<li>{details['name']} x {details['quantity']} - ₹{item_total}</li>"
cart_html += f"</ul><p><strong>Total: ₹{total_price}</strong></p>"
return cart_html
# Place order and sync with Salesforce
def place_order():
# Create Order in Salesforce
order = sf.Order.create({
"Status": "Draft",
"TotalAmount": sum(details["price"] * details["quantity"] for details in cart.values())
})
order_id = order["id"]
# Add Order Items
for item_id, details in cart.items():
sf.OrderItem.create({
"OrderId": order_id,
"Product2Id": item_id,
"UnitPrice": details["price"],
"Quantity": details["quantity"]
})
# Clear the cart
cart.clear()
return "Order placed successfully!"
# Fetch menu and initialize UI components
menu_items = fetch_menu_items()
menu_html = generate_menu_html(menu_items)
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("# Welcome to Biryani Hub")
with gr.Row():
menu_display = gr.HTML(value=menu_html)
with gr.Row():
cart_display = gr.HTML(value=view_cart())
item_id_input = gr.Textbox(placeholder="Item ID")
quantity_input = gr.Number(label="Quantity", value=1, interactive=True)
add_to_cart_button = gr.Button("Add to Cart")
place_order_button = gr.Button("Place Order")
add_to_cart_button.click(update_cart, inputs=[item_id_input, quantity_input], outputs=cart_display)
place_order_button.click(place_order, inputs=[], outputs=cart_display)
# Launch the app
demo.launch()