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