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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -82
app.py CHANGED
@@ -4,104 +4,110 @@ 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():
9
- query = """
10
- SELECT Name, Description__c, Price__c, Veg_NonVeg__c, Section__c, Image1__c
11
- FROM Menu_Item__c
12
- """
13
- return sf.query_all(query)['records']
14
-
15
- # Fetch Add-ons from Salesforce
16
- def fetch_add_ons():
17
- query = """
18
- SELECT Name, Price__c, Description__c, Menu_Item__c
19
- FROM Add_Ons__c
20
- WHERE Available_In_Menu__c = TRUE
21
- """
22
- return sf.query_all(query)['records']
23
-
24
- # Function to generate HTML for the menu with popups
25
- def generate_menu_html(menu, add_ons):
26
- menu_html = ""
27
- add_ons_by_item = {}
28
 
29
- # Organize add-ons by menu item
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  for add_on in add_ons:
31
- item_id = add_on['Menu_Item__c']
32
- if item_id not in add_ons_by_item:
33
- add_ons_by_item[item_id] = []
34
- add_ons_by_item[item_id].append(add_on)
35
-
36
- # Generate HTML for menu items
37
- sections = {}
38
- for item in menu:
39
- section = item['Section__c'] or 'Others'
40
- if section not in sections:
41
- sections[section] = []
42
-
43
- item_html = f"""
44
- <div class='menu-item'>
45
- <img src='{item['Image1__c']}' alt='{item['Name']}' class='menu-item-image'>
 
 
 
 
 
 
 
46
  <h3>{item['Name']}</h3>
47
- <p>{item['Description__c']}</p>
48
- <p><strong>Price: ${item['Price__c']}</strong></p>
49
- <button onclick=\"openPopup('{item['Name']}', '{item['Price__c']}', {add_ons_by_item.get(item['Id'], [])})\">Customize & Add</button>
50
  </div>
51
  """
52
- sections[section].append(item_html)
53
 
54
- # Generate sections
55
- for section, items in sections.items():
56
- menu_html += f"<h2>{section}</h2><div class='menu-section'>{''.join(items)}</div>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- return menu_html
 
 
 
 
59
 
60
- # Frontend
61
- menu_items = fetch_menu()
62
- add_ons = fetch_add_ons()
63
  menu_html = generate_menu_html(menu_items, add_ons)
64
 
65
  # Gradio app
66
  with gr.Blocks() as app:
67
- gr.HTML("<h1>Welcome to Biryani Hub</h1>")
68
- gr.HTML("<div id='menu-container'>" + menu_html + "</div>")
69
 
 
 
 
 
70
  gr.HTML("""
71
- <div id='popup' style='display:none;'>
72
- <h2 id='popup-title'></h2>
73
- <p id='popup-price'></p>
74
- <div id='popup-add-ons'></div>
75
  <button onclick="closePopup()">Close</button>
76
  </div>
77
  """)
78
 
79
- gr.HTML("""
80
- <script>
81
- function openPopup(itemName, price, addOns) {
82
- document.getElementById('popup-title').innerText = itemName;
83
- document.getElementById('popup-price').innerText = `Price: $${price}`;
84
-
85
- let addOnsContainer = document.getElementById('popup-add-ons');
86
- addOnsContainer.innerHTML = '';
87
-
88
- if (addOns.length > 0) {
89
- addOns.forEach(addOn => {
90
- let addOnDiv = document.createElement('div');
91
- addOnDiv.innerHTML = `<input type='checkbox' value='${addOn.Name}' data-price='${addOn.Price__c}'> ${addOn.Name} ($${addOn.Price__c}) - ${addOn.Description__c}`;
92
- addOnsContainer.appendChild(addOnDiv);
93
- });
94
- } else {
95
- addOnsContainer.innerHTML = '<p>No Add-ons available for this item.</p>';
96
- }
97
-
98
- document.getElementById('popup').style.display = 'block';
99
- }
100
-
101
- function closePopup() {
102
- document.getElementById('popup').style.display = 'none';
103
- }
104
- </script>
105
- """)
106
-
107
  app.launch()
 
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() {
85
+ document.getElementById('popup').style.display = 'none';
86
+ }
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()