dschandra commited on
Commit
b316f59
·
verified ·
1 Parent(s): b4be4c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -50
app.py CHANGED
@@ -4,74 +4,99 @@ from simple_salesforce import Salesforce
4
  # Salesforce Connection
5
  sf = Salesforce(username='[email protected]', 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) # Removed `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()
 
4
  # Salesforce Connection
5
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
6
 
7
+ # Initialize cart
8
+ cart = {}
9
+
10
  # Fetch menu items from Salesforce
11
  def fetch_menu_items():
12
  query = "SELECT Id, Name, Price__c, Description__c, Image1__c FROM Menu_Item__c"
13
+ menu_items = sf.query(query)["records"]
14
+ return menu_items
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Generate menu HTML with add-to-cart functionality
17
+ def generate_menu_html(menu_items):
18
+ html = ""
19
  for item in menu_items:
20
+ html += f"""
21
+ <div style='border: 1px solid #ccc; padding: 10px; margin: 10px; display: flex; align-items: center;'>
22
+ <img src='{item.get('Image1__c', '')}' style='width: 100px; height: 100px; margin-right: 10px;' />
23
  <div>
24
+ <h3>{item['Name']}</h3>
25
+ <p>{item.get('Description__c', '')}</p>
26
  <p>₹{item['Price__c']}</p>
27
+ <div style='display: flex; align-items: center;'>
28
+ <button onclick="decreaseQuantity('{item['Id']}')">-</button>
29
+ <span id="quantity-{item['Id']}" style="margin: 0 10px;">0</span>
30
+ <button onclick="increaseQuantity('{item['Id']}')">+</button>
31
+ </div>
32
  </div>
33
  </div>
34
  """
35
+ return html
36
 
37
+ # Update cart when items are added
38
+ def update_cart(item_id, quantity):
39
+ if item_id in cart:
40
+ cart[item_id]["quantity"] = quantity
41
+ else:
42
+ menu_item = sf.Menu_Item__c.get(item_id)
43
+ cart[item_id] = {
44
+ "name": menu_item["Name"],
45
+ "price": menu_item["Price__c"],
46
+ "quantity": quantity
47
+ }
48
+ return view_cart()
49
 
50
+ # View cart details dynamically
51
+ def view_cart():
52
+ cart_html = "<h3>Cart Details</h3><ul>"
53
+ total_price = 0
54
+ for item_id, details in cart.items():
55
+ item_total = details["price"] * details["quantity"]
56
+ total_price += item_total
57
+ cart_html += f"<li>{details['name']} x {details['quantity']} - ₹{item_total}</li>"
58
+ cart_html += f"</ul><p><strong>Total: ₹{total_price}</strong></p>"
59
+ return cart_html
60
+
61
+ # Place order and sync with Salesforce
62
+ def place_order():
63
+ # Create Order in Salesforce
64
+ order = sf.Order.create({
65
+ "Status": "Draft",
66
+ "TotalAmount": sum(details["price"] * details["quantity"] for details in cart.values())
67
+ })
68
+ order_id = order["id"]
69
+
70
+ # Add Order Items
71
+ for item_id, details in cart.items():
72
+ sf.OrderItem.create({
73
+ "OrderId": order_id,
74
+ "Product2Id": item_id,
75
+ "UnitPrice": details["price"],
76
+ "Quantity": details["quantity"]
77
+ })
78
 
79
+ # Clear the cart
80
+ cart.clear()
81
+ return "Order placed successfully!"
82
+
83
+ # Set up Gradio interface
84
+ menu_items = fetch_menu_items()
85
+ menu_html = generate_menu_html(menu_items)
86
 
 
87
  with gr.Blocks() as demo:
88
  with gr.Row():
89
  gr.Markdown("# Welcome to Biryani Hub")
90
+
91
+ with gr.Row():
92
+ menu_display = gr.HTML(value=menu_html)
93
+
94
  with gr.Row():
95
+ cart_display = gr.HTML(value=view_cart())
96
+ place_order_button = gr.Button("Place Order")
 
97
 
98
+ menu_display.change(update_cart, inputs=["item_id", "quantity"], outputs=cart_display)
99
+ place_order_button.click(place_order, inputs=[], outputs=cart_display)
100
 
101
  # Launch the app
102
+ demo.launch()