dschandra commited on
Commit
f5513ef
·
verified ·
1 Parent(s): c0caa98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -82
app.py CHANGED
@@ -4,81 +4,54 @@ from simple_salesforce import Salesforce
4
  # Salesforce Connection
5
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
6
 
7
-
8
- # Function to fetch menu items from Salesforce
9
- def load_menu_from_salesforce():
10
- try:
11
- query = "SELECT Id, Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
12
- result = sf.query(query)
13
- return result['records']
14
- except Exception as e:
15
- print(f"Error fetching menu items: {e}")
16
- return []
17
-
18
- # Function to fetch add-ons from Salesforce
19
- def load_add_ons_from_salesforce():
20
- try:
21
- query = "SELECT Name, Price__c, Menu_Item__c FROM Add_Ons__c"
22
- result = sf.query(query)
23
- return result['records']
24
- except Exception as e:
25
- print(f"Error fetching add-ons: {e}")
26
- return []
27
-
28
- # Function to generate HTML for menu items
29
- def generate_menu_html(menu_items, add_ons):
30
- add_ons_by_item = {}
31
- for add_on in add_ons:
32
- menu_item_id = add_on.get('Menu_Item__c', None)
33
- if menu_item_id:
34
- if menu_item_id not in add_ons_by_item:
35
- add_ons_by_item[menu_item_id] = []
36
- add_ons_by_item[menu_item_id].append({
37
- "name": add_on["Name"],
38
- "price": add_on["Price__c"]
39
- })
40
-
41
  html = ""
42
- current_section = None
43
- for item in menu_items:
44
- if item["Section__c"] != current_section:
45
- if current_section is not None:
46
- html += "</div>"
47
- current_section = item["Section__c"]
48
- html += f"<h2>{current_section}</h2><div class='menu-section'>"
49
-
50
- add_ons_list = add_ons_by_item.get(item['Id'], [])
51
- html += f"""
52
- <div class="menu-item">
53
- <img src="{item.get('Image1__c', '')}" alt="{item['Name']}" style="width: 150px; height: 150px; object-fit: cover;">
54
- <h3>{item['Name']}</h3>
55
- <p>{item.get('Description__c', 'No description available.')}</p>
56
- <p>Price: ${item['Price__c']}</p>
57
- <button onclick="openPopup('{item['Name']}', '{item['Price__c']}', {add_ons_list})">Customize & Add</button>
58
- </div>
59
- """
60
-
61
- if current_section is not None:
62
  html += "</div>"
63
  return html
64
 
65
  # JavaScript for popup functionality
66
  popup_script = """
67
  <script>
68
- function openPopup(name, price, addOns) {
69
  const popup = document.getElementById('popup');
70
- const addOnsContainer = document.getElementById('add-ons-container');
71
-
72
  popup.style.display = 'block';
73
- document.getElementById('popup-item-name').innerText = name;
74
- document.getElementById('popup-item-price').innerText = `Price: $${price}`;
75
-
76
- addOnsContainer.innerHTML = "";
77
- addOns.forEach(addOn => {
78
- const addOnElement = document.createElement('div');
79
- addOnElement.innerHTML = `${addOn.name} (+$${addOn.price}) <input type='checkbox' value='${addOn.name}'>`;
80
- addOnsContainer.appendChild(addOnElement);
81
- });
82
  }
83
 
84
  function closePopup() {
@@ -87,27 +60,30 @@ popup_script = """
87
  </script>
88
  """
89
 
90
- # Load data from Salesforce
91
- menu_items = load_menu_from_salesforce()
92
- add_ons = load_add_ons_from_salesforce()
93
- menu_html = generate_menu_html(menu_items, add_ons)
94
-
95
  # Gradio app
96
  with gr.Blocks() as app:
97
  with gr.Row():
98
- gr.HTML("<h1>Welcome to Biryani Hub</h1>")
 
 
 
99
 
100
  with gr.Row():
101
- gr.HTML(menu_html)
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
- gr.HTML(popup_script)
104
- gr.HTML("""
105
- <div id="popup" style="display: none; position: fixed; top: 20%; left: 30%; width: 40%; background: white; padding: 20px; border: 1px solid #ccc; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);">
106
- <h2 id="popup-item-name"></h2>
107
- <p id="popup-item-price"></p>
108
- <div id="add-ons-container"></div>
109
- <button onclick="closePopup()">Close</button>
110
- </div>
111
- """)
112
 
113
  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():
9
+ query = "SELECT Id, Name, Price__c, Description__c, Image1__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
10
+ result = sf.query(query)
11
+ return result['records']
12
+
13
+ # Fetch add-ons from Salesforce
14
+ def fetch_add_ons():
15
+ query = "SELECT Id, Name, Price__c, Menu_Item__c FROM Add_Ons__c"
16
+ result = sf.query(query)
17
+ return result['records']
18
+
19
+ # Generate HTML for the menu items
20
+ def generate_menu_html(menu_items, add_ons, preference):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  html = ""
22
+ sections = set(item['Section__c'] for item in menu_items)
23
+
24
+ for section in sorted(sections):
25
+ html += f"<h2>{section}</h2><div style='display: flex; flex-wrap: wrap; gap: 20px;'>"
26
+ for item in menu_items:
27
+ if item['Section__c'] == section and (preference == 'All' or item['Veg_NonVeg__c'] == preference):
28
+ item_add_ons = [addon for addon in add_ons if addon['Menu_Item__c'] == item['Id']]
29
+ add_ons_html = "".join(
30
+ f"<div><input type='checkbox' value='{addon['Name']}' data-price='{addon['Price__c']}'> {addon['Name']} (+${addon['Price__c']})</div>"
31
+ for addon in item_add_ons
32
+ )
33
+ html += f"""
34
+ <div style='width: 300px; border: 1px solid #ddd; padding: 10px; border-radius: 8px;'>
35
+ <img src='{item['Image1__c']}' alt='{item['Name']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 8px;'>
36
+ <h3>{item['Name']}</h3>
37
+ <p>{item['Description__c']}</p>
38
+ <p>Price: ${item['Price__c']}</p>
39
+ <button onclick="openPopup('{item['Name']}', '{item['Price__c']}', '{item['Image1__c']}', `{add_ons_html}`)">Customize & Add</button>
40
+ </div>
41
+ """
42
  html += "</div>"
43
  return html
44
 
45
  # JavaScript for popup functionality
46
  popup_script = """
47
  <script>
48
+ function openPopup(name, price, image, addOnsHtml) {
49
  const popup = document.getElementById('popup');
 
 
50
  popup.style.display = 'block';
51
+ document.getElementById('popup-name').innerText = name;
52
+ document.getElementById('popup-price').innerText = `$${price}`;
53
+ document.getElementById('popup-image').src = image;
54
+ document.getElementById('popup-addons').innerHTML = addOnsHtml;
 
 
 
 
 
55
  }
56
 
57
  function closePopup() {
 
60
  </script>
61
  """
62
 
 
 
 
 
 
63
  # Gradio app
64
  with gr.Blocks() as app:
65
  with gr.Row():
66
+ gr.Markdown("# Welcome to Biryani Hub")
67
+
68
+ preference = gr.Radio(label="Filter Preference", choices=["All", "Veg", "Non-Veg"], value="All")
69
+ menu_html = gr.HTML()
70
 
71
  with gr.Row():
72
+ gr.HTML("""
73
+ <div id='popup' style='display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.2);'>
74
+ <img id='popup-image' src='' alt='' style='width: 100%; height: 150px; object-fit: cover; border-radius: 8px;'>
75
+ <h2 id='popup-name'></h2>
76
+ <p id='popup-price'></p>
77
+ <div id='popup-addons'></div>
78
+ <button onclick='closePopup()'>Close</button>
79
+ </div>
80
+ """ + popup_script)
81
+
82
+ def update_menu(preference):
83
+ menu_items = fetch_menu()
84
+ add_ons = fetch_add_ons()
85
+ return generate_menu_html(menu_items, add_ons, preference)
86
 
87
+ preference.change(update_menu, inputs=preference, outputs=menu_html)
 
 
 
 
 
 
 
 
88
 
89
  app.launch()