Cart / app.py
dschandra's picture
Update app.py
77b50a3 verified
raw
history blame
2.95 kB
import gradio as gr
from simple_salesforce import Salesforce
# Salesforce Connection
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
# Sample menu items from Salesforce
def fetch_menu_items():
try:
query = "SELECT Id, Name, Price__c, Image1__c, Description__c, Section__c FROM Menu_Item__c"
menu_items = sf.query(query)['records']
return menu_items
except Exception as e:
print(f"Error fetching menu items: {e}")
return []
# Cart management
cart = {}
def update_cart(item_id, name, price, quantity):
if item_id in cart:
cart[item_id]['quantity'] += quantity
if cart[item_id]['quantity'] <= 0:
del cart[item_id]
else:
if quantity > 0:
cart[item_id] = {'name': name, 'price': price, 'quantity': quantity}
return cart_summary()
def cart_summary():
total_items = sum(item['quantity'] for item in cart.values())
total_cost = sum(item['quantity'] * item['price'] for item in cart.values())
return f"{total_items} items | Total: ₹{total_cost:.2f}"
# Cart Page
def cart_page():
items_html = "".join(
f"""
<div>
<h4>{item['name']} (₹{item['price']})</h4>
<p>Quantity: {item['quantity']}</p>
</div>
"""
for item in cart.values()
)
total_cost = sum(item['quantity'] * item['price'] for item in cart.values())
return f"""
<h3>Your Cart</h3>
<div>{items_html}</div>
<h4>Total Cost: ₹{total_cost:.2f}</h4>
<button onclick="alert('Checkout functionality to be implemented')">Proceed to Checkout</button>
"""
# Generate HTML for Menu Items
def generate_menu_html(menu_items):
html = ""
for item in menu_items:
html += f"""
<div style="border: 1px solid #ddd; padding: 10px; margin: 10px; border-radius: 8px; display: flex; align-items: center;">
<img src="{item['Image1__c']}" alt="{item['Name']}" style="width: 100px; height: 100px; margin-right: 10px;">
<div>
<h4>{item['Name']}</h4>
<p>₹{item['Price__c']}</p>
<button onclick="updateCart('{item['Id']}', '{item['Name']}', {item['Price__c']}, -1)">-</button>
<span>0</span>
<button onclick="updateCart('{item['Id']}', '{item['Name']}', {item['Price__c']}, 1)">+</button>
</div>
</div>
"""
return html
# Gradio App
with gr.Blocks() as app:
menu_items = fetch_menu_items()
with gr.Row():
gr.HTML("<h1>Welcome to Biryani Hub</h1>")
with gr.Row():
gr.HTML(generate_menu_html(menu_items))
# Floating Cart Button
with gr.Row():
cart_display = gr.HTML("0 items | Total: ₹0.00", elem_id="cart-summary")
gr.Button("View Cart", elem_id="view-cart-btn").click(cart_page, [], cart_display)
app.launch()