Update app.py
Browse files
app.py
CHANGED
|
@@ -4,84 +4,74 @@ from simple_salesforce import Salesforce
|
|
| 4 |
# Salesforce Connection
|
| 5 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 6 |
|
| 7 |
-
#
|
| 8 |
def fetch_menu_items():
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
return menu_items
|
| 13 |
-
except Exception as e:
|
| 14 |
-
print(f"Error fetching menu items: {e}")
|
| 15 |
-
return []
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return cart_summary()
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
def
|
| 37 |
-
|
| 38 |
-
f"""
|
| 39 |
-
<div>
|
| 40 |
-
<h4>{item['name']} (₹{item['price']})</h4>
|
| 41 |
-
<p>Quantity: {item['quantity']}</p>
|
| 42 |
-
</div>
|
| 43 |
-
"""
|
| 44 |
-
for item in cart.values()
|
| 45 |
-
)
|
| 46 |
-
total_cost = sum(item['quantity'] * item['price'] for item in cart.values())
|
| 47 |
-
return f"""
|
| 48 |
-
<h3>Your Cart</h3>
|
| 49 |
-
<div>{items_html}</div>
|
| 50 |
-
<h4>Total Cost: ₹{total_cost:.2f}</h4>
|
| 51 |
-
<button onclick="alert('Checkout functionality to be implemented')">Proceed to Checkout</button>
|
| 52 |
-
"""
|
| 53 |
-
|
| 54 |
-
# Generate HTML for Menu Items
|
| 55 |
-
def generate_menu_html(menu_items):
|
| 56 |
-
html = ""
|
| 57 |
for item in menu_items:
|
| 58 |
-
|
| 59 |
-
<div style="
|
| 60 |
-
<img src="{item['Image1__c']}"
|
| 61 |
<div>
|
| 62 |
<h4>{item['Name']}</h4>
|
| 63 |
<p>₹{item['Price__c']}</p>
|
| 64 |
-
<button onclick="
|
| 65 |
-
<span>0</span>
|
| 66 |
-
<button onclick="updateCart('{item['Id']}', '{item['Name']}', {item['Price__c']}, 1)">+</button>
|
| 67 |
</div>
|
| 68 |
</div>
|
| 69 |
"""
|
| 70 |
-
return
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
|
|
|
|
|
|
| 79 |
with gr.Row():
|
| 80 |
-
gr.
|
| 81 |
|
| 82 |
-
# Floating Cart Button
|
| 83 |
with gr.Row():
|
| 84 |
-
|
| 85 |
-
gr.Button("View Cart",
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
app
|
|
|
|
|
|
| 4 |
# Salesforce Connection
|
| 5 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 6 |
|
| 7 |
+
# Fetch menu items from Salesforce
|
| 8 |
def fetch_menu_items():
|
| 9 |
+
query = "SELECT Id, Name, Price__c, Description__c, Image1__c FROM Menu_Item__c"
|
| 10 |
+
records = sf.query(query)["records"]
|
| 11 |
+
return records
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Fetch cart items (local data structure for demonstration)
|
| 14 |
+
cart_items = []
|
| 15 |
|
| 16 |
+
# Add item to cart
|
| 17 |
+
def add_to_cart(item_id, name, price):
|
| 18 |
+
for item in cart_items:
|
| 19 |
+
if item["id"] == item_id:
|
| 20 |
+
item["quantity"] += 1
|
| 21 |
+
return f"{name} added to the cart!", cart_items
|
| 22 |
+
cart_items.append({"id": item_id, "name": name, "price": price, "quantity": 1})
|
| 23 |
+
return f"{name} added to the cart!", cart_items
|
|
|
|
| 24 |
|
| 25 |
+
# Display cart items
|
| 26 |
+
def view_cart():
|
| 27 |
+
cart_html = ""
|
| 28 |
+
total_items = 0
|
| 29 |
+
total_price = 0
|
| 30 |
+
for item in cart_items:
|
| 31 |
+
cart_html += f"<p>{item['name']} - ₹{item['price']} x {item['quantity']}</p>"
|
| 32 |
+
total_items += item["quantity"]
|
| 33 |
+
total_price += item["price"] * item["quantity"]
|
| 34 |
+
cart_html += f"<h3>Total: ₹{total_price} ({total_items} items)</h3>"
|
| 35 |
+
return cart_html
|
| 36 |
|
| 37 |
+
# Generate menu page HTML
|
| 38 |
+
def generate_menu_page(menu_items):
|
| 39 |
+
menu_html = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
for item in menu_items:
|
| 41 |
+
menu_html += f"""
|
| 42 |
+
<div style="display: flex; align-items: center; margin-bottom: 20px;">
|
| 43 |
+
<img src="{item['Image1__c']}" style="width: 100px; height: 100px; margin-right: 20px;">
|
| 44 |
<div>
|
| 45 |
<h4>{item['Name']}</h4>
|
| 46 |
<p>₹{item['Price__c']}</p>
|
| 47 |
+
<button onclick="add_to_cart('{item['Id']}', '{item['Name']}', {item['Price__c']})">Add</button>
|
|
|
|
|
|
|
| 48 |
</div>
|
| 49 |
</div>
|
| 50 |
"""
|
| 51 |
+
return menu_html
|
| 52 |
|
| 53 |
+
# Fetch menu items for display
|
| 54 |
+
menu_items = fetch_menu_items()
|
| 55 |
+
menu_html = generate_menu_page(menu_items)
|
| 56 |
|
| 57 |
+
# Gradio interface for menu page
|
| 58 |
+
def menu_interface():
|
| 59 |
+
return menu_html, gr.update(visible=False)
|
| 60 |
+
|
| 61 |
+
def cart_interface():
|
| 62 |
+
return view_cart(), gr.update(visible=True)
|
| 63 |
|
| 64 |
+
# Gradio setup
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
with gr.Row():
|
| 67 |
+
gr.Markdown("# Welcome to Biryani Hub")
|
| 68 |
|
|
|
|
| 69 |
with gr.Row():
|
| 70 |
+
menu_display = gr.HTML(value=menu_html, interactive=False)
|
| 71 |
+
cart_button = gr.Button("View Cart", visible=True)
|
| 72 |
+
cart_display = gr.HTML(value=view_cart(), visible=False)
|
| 73 |
+
|
| 74 |
+
cart_button.click(cart_interface, inputs=[], outputs=[menu_display, cart_display])
|
| 75 |
|
| 76 |
+
# Launch the app
|
| 77 |
+
demo.launch()
|