Update app.py
Browse files
app.py
CHANGED
|
@@ -4,48 +4,50 @@ from simple_salesforce import Salesforce
|
|
| 4 |
# Salesforce Connection
|
| 5 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def fetch_menu_from_salesforce(preference):
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
for section, items in sections.items():
|
| 32 |
-
html += f"<h2 style='text-align: left;'>{section}</h2>"
|
| 33 |
-
for item in items:
|
| 34 |
-
html += f"""
|
| 35 |
-
<div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 10px; margin-bottom: 10px;">
|
| 36 |
-
<div style="flex: 1;">
|
| 37 |
-
<h3 style="margin: 0;">{item['Name']}</h3>
|
| 38 |
-
<p style="margin: 5px 0;">{item.get('Description__c', 'No description available')}</p>
|
| 39 |
-
<p style="margin: 5px 0;"><b>Price:</b> ${item['Price__c']}</p>
|
| 40 |
-
</div>
|
| 41 |
-
<div>
|
| 42 |
-
<img src="{item.get('Image1__c', '')}" alt="{item['Name']}" style="width: 100px; height: 100px; border-radius: 8px; margin-left: 10px;">
|
| 43 |
-
</div>
|
| 44 |
-
</div>
|
| 45 |
-
"""
|
| 46 |
-
return html if html else "<p>No menu items available.</p>"
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Gradio App
|
| 51 |
with gr.Blocks() as app:
|
|
@@ -56,14 +58,6 @@ with gr.Blocks() as app:
|
|
| 56 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
| 57 |
menu_display = gr.HTML()
|
| 58 |
|
| 59 |
-
|
| 60 |
-
preference.change(
|
| 61 |
-
lambda pref: fetch_menu_from_salesforce(pref),
|
| 62 |
-
inputs=preference,
|
| 63 |
-
outputs=menu_display
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# Initial Load
|
| 67 |
-
menu_display.value = fetch_menu_from_salesforce("All")
|
| 68 |
|
| 69 |
-
app.launch()
|
|
|
|
| 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:
|
|
|
|
| 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()
|