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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -52
app.py CHANGED
@@ -4,60 +4,104 @@ from simple_salesforce import Salesforce
4
  # Salesforce Connection
5
  sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
6
 
7
- # Function to fetch menu items from Salesforce
8
  def fetch_menu():
9
- try:
10
- query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
11
- menu_items = sf.query(query)['records']
12
-
13
- # Format the menu items into a dictionary grouped by section
14
- formatted_menu = {}
15
- for item in menu_items:
16
- section = item['Section__c'] or "Miscellaneous"
17
- if section not in formatted_menu:
18
- formatted_menu[section] = []
19
-
20
- formatted_menu[section].append({
21
- "name": item['Name'],
22
- "price": item['Price__c'],
23
- "description": item['Description__c'],
24
- "image1": item['Image1__c'],
25
- "image2": item['Image2__c'],
26
- "veg_nonveg": item['Veg_NonVeg__c']
27
- })
28
-
29
- return formatted_menu
30
- except Exception as e:
31
- return f"Error fetching menu from Salesforce: {str(e)}"
32
-
33
- # Function to display menu based on user preference
34
- def display_menu(preference):
35
- menu = fetch_menu()
36
- if isinstance(menu, str): # Error handling
37
- return menu
38
-
39
- html_output = ""
40
- for section, items in menu.items():
41
- html_output += f"<h2>{section}</h2>"
42
- for item in items:
43
- if preference == "All" or item['veg_nonveg'] == preference:
44
- html_output += f"<div style='border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;'>"
45
- html_output += f"<h3>{item['name']}</h3>"
46
- html_output += f"<p>{item['description']}</p>"
47
- html_output += f"<p>Price: ${item['price']}</p>"
48
- html_output += f"<img src='{item['image1']}' alt='{item['name']}' style='width: 100px; height: 100px;'>"
49
- html_output += f"</div>"
50
- return html_output
51
-
52
- # Gradio App
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  with gr.Blocks() as app:
54
- with gr.Row():
55
- gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- with gr.Row():
58
- preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
59
- menu_display = gr.HTML()
60
 
61
- preference.change(display_menu, inputs=preference, outputs=menu_display)
 
 
 
 
62
 
63
- app.launch()
 
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():
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()