dschandra commited on
Commit
1ff1c4f
·
verified ·
1 Parent(s): 77b50a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -62
app.py CHANGED
@@ -4,84 +4,74 @@ from simple_salesforce import Salesforce
4
  # Salesforce Connection
5
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
6
 
7
- # Sample menu items from Salesforce
8
  def fetch_menu_items():
9
- try:
10
- query = "SELECT Id, Name, Price__c, Image1__c, Description__c, Section__c FROM Menu_Item__c"
11
- menu_items = sf.query(query)['records']
12
- return menu_items
13
- except Exception as e:
14
- print(f"Error fetching menu items: {e}")
15
- return []
16
 
17
- # Cart management
18
- cart = {}
19
 
20
- def update_cart(item_id, name, price, quantity):
21
- if item_id in cart:
22
- cart[item_id]['quantity'] += quantity
23
- if cart[item_id]['quantity'] <= 0:
24
- del cart[item_id]
25
- else:
26
- if quantity > 0:
27
- cart[item_id] = {'name': name, 'price': price, 'quantity': quantity}
28
- return cart_summary()
29
 
30
- def cart_summary():
31
- total_items = sum(item['quantity'] for item in cart.values())
32
- total_cost = sum(item['quantity'] * item['price'] for item in cart.values())
33
- return f"{total_items} items | Total: ₹{total_cost:.2f}"
 
 
 
 
 
 
 
34
 
35
- # Cart Page
36
- def cart_page():
37
- items_html = "".join(
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
- html += f"""
59
- <div style="border: 1px solid #ddd; padding: 10px; margin: 10px; border-radius: 8px; display: flex; align-items: center;">
60
- <img src="{item['Image1__c']}" alt="{item['Name']}" style="width: 100px; height: 100px; margin-right: 10px;">
61
  <div>
62
  <h4>{item['Name']}</h4>
63
  <p>₹{item['Price__c']}</p>
64
- <button onclick="updateCart('{item['Id']}', '{item['Name']}', {item['Price__c']}, -1)">-</button>
65
- <span>0</span>
66
- <button onclick="updateCart('{item['Id']}', '{item['Name']}', {item['Price__c']}, 1)">+</button>
67
  </div>
68
  </div>
69
  """
70
- return html
71
 
72
- # Gradio App
73
- with gr.Blocks() as app:
74
- menu_items = fetch_menu_items()
75
 
76
- with gr.Row():
77
- gr.HTML("<h1>Welcome to Biryani Hub</h1>")
 
 
 
 
78
 
 
 
79
  with gr.Row():
80
- gr.HTML(generate_menu_html(menu_items))
81
 
82
- # Floating Cart Button
83
  with gr.Row():
84
- cart_display = gr.HTML("0 items | Total: ₹0.00", elem_id="cart-summary")
85
- gr.Button("View Cart", elem_id="view-cart-btn").click(cart_page, [], cart_display)
 
 
 
86
 
87
- app.launch()
 
 
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, 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()