Update app.py
Browse files
app.py
CHANGED
@@ -4,92 +4,149 @@ from simple_salesforce import Salesforce
|
|
4 |
# Salesforce Connection
|
5 |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
9 |
query = "SELECT Id, Name, Price__c, Description__c, Image1__c, Section__c, Veg_NonVeg__c FROM Menu_Item__c"
|
10 |
-
|
11 |
-
return
|
12 |
-
|
13 |
-
#
|
14 |
-
def
|
15 |
-
query = "SELECT Name, Price__c, Menu_Item__c FROM Add_Ons__c"
|
16 |
-
|
17 |
-
return
|
18 |
-
|
19 |
-
#
|
20 |
-
def generate_menu_html(menu_items
|
21 |
-
|
22 |
-
for
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
add_ons_list = add_ons_by_item.get(item['Id'], [])
|
34 |
-
add_ons_html = "<br>".join(add_ons_list)
|
35 |
-
html += f"""
|
36 |
-
<div style='width: 250px; margin: 10px; border: 1px solid #ccc; border-radius: 8px; padding: 10px;'>
|
37 |
-
<img src='{item['Image1__c']}' alt='{item['Name']}' style='width: 100%; height: 150px; object-fit: cover;'>
|
38 |
-
<h3>{item['Name']}</h3>
|
39 |
-
<p>{item['Description__c']}</p>
|
40 |
-
<p>Price: ${item['Price__c']}</p>
|
41 |
-
<button onclick="openPopup('{item['Id']}', '{item['Name']}', '{item['Price__c']}', '{item['Image1__c']}', `{add_ons_html}`)">Customize & Add</button>
|
42 |
-
</div>
|
43 |
-
"""
|
44 |
-
html += "</div>"
|
45 |
return html
|
46 |
|
47 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
popup_script = """
|
49 |
<script>
|
50 |
-
function openPopup(id, name, price
|
51 |
-
|
52 |
-
popup.style.display = 'block';
|
53 |
-
popup.style.width = '400px';
|
54 |
-
popup.style.height = '600px';
|
55 |
-
popup.style.margin = 'auto';
|
56 |
-
document.getElementById('popup-title').innerText = name;
|
57 |
document.getElementById('popup-price').innerText = `$${price}`;
|
58 |
-
document.getElementById('popup
|
59 |
-
document.getElementById('popup
|
60 |
}
|
61 |
|
62 |
function closePopup() {
|
63 |
document.getElementById('popup').style.display = 'none';
|
64 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
</script>
|
66 |
"""
|
67 |
|
68 |
# Gradio App
|
69 |
-
menu_items = fetch_menu_items()
|
70 |
-
add_ons = fetch_add_ons()
|
71 |
-
menu_html = generate_menu_html(menu_items, add_ons)
|
72 |
-
|
73 |
with gr.Blocks() as app:
|
|
|
|
|
|
|
74 |
with gr.Row():
|
75 |
-
gr.HTML("<h1
|
76 |
|
77 |
with gr.Row():
|
78 |
-
gr.
|
|
|
79 |
|
80 |
with gr.Row():
|
81 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
# Popup
|
84 |
gr.HTML("""
|
85 |
-
<div id=
|
86 |
-
<
|
87 |
-
<
|
88 |
-
<
|
89 |
-
<
|
90 |
-
<
|
91 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
</div>
|
93 |
-
"""
|
|
|
|
|
94 |
|
95 |
-
app.launch()
|
|
|
4 |
# Salesforce Connection
|
5 |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
6 |
|
7 |
+
# Cart to store items
|
8 |
+
cart = []
|
9 |
+
|
10 |
+
# Function to fetch menu items
|
11 |
+
def fetch_menu_from_salesforce():
|
12 |
query = "SELECT Id, Name, Price__c, Description__c, Image1__c, Section__c, Veg_NonVeg__c FROM Menu_Item__c"
|
13 |
+
menu_items = sf.query(query)
|
14 |
+
return menu_items["records"]
|
15 |
+
|
16 |
+
# Function to fetch add-ons
|
17 |
+
def fetch_add_ons_from_salesforce():
|
18 |
+
query = "SELECT Id, Name, Price__c, Menu_Item__c FROM Add_Ons__c WHERE Available_In_Menu__c = true"
|
19 |
+
add_ons = sf.query(query)
|
20 |
+
return add_ons["records"]
|
21 |
+
|
22 |
+
# Function to generate menu HTML
|
23 |
+
def generate_menu_html(menu_items):
|
24 |
+
html = "<div style='display: flex; flex-wrap: wrap;'>"
|
25 |
+
for item in menu_items:
|
26 |
+
html += f"""
|
27 |
+
<div style="border: 1px solid #ddd; padding: 10px; margin: 10px; width: 300px; border-radius: 8px; text-align: center;">
|
28 |
+
<img src="{item.get('Image1__c')}" alt="{item.get('Name')}" style="width: 100%; height: 200px; object-fit: cover; border-radius: 8px;">
|
29 |
+
<h3>{item.get('Name')}</h3>
|
30 |
+
<p>{item.get('Description__c')}</p>
|
31 |
+
<p style="font-weight: bold;">${item.get('Price__c')}</p>
|
32 |
+
<button onclick="openPopup('{item.get('Id')}', '{item.get('Name')}', {item.get('Price__c')})">Customize & Add</button>
|
33 |
+
</div>
|
34 |
+
"""
|
35 |
+
html += "</div>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
return html
|
37 |
|
38 |
+
# Function to save order in Salesforce
|
39 |
+
def save_order_in_salesforce(cart_data, total_cost):
|
40 |
+
try:
|
41 |
+
if not cart_data:
|
42 |
+
return "Cart is empty!"
|
43 |
+
|
44 |
+
# Save Order
|
45 |
+
order = {
|
46 |
+
"Name": f"Order-{datetime.now().strftime('%Y%m%d%H%M%S')}",
|
47 |
+
"Total_Amount__c": total_cost,
|
48 |
+
"Order_Date__c": datetime.now().isoformat(),
|
49 |
+
}
|
50 |
+
order_result = sf.Order__c.create(order)
|
51 |
+
order_id = order_result["id"]
|
52 |
+
|
53 |
+
# Save Order Items
|
54 |
+
for item in cart_data:
|
55 |
+
order_item = {
|
56 |
+
"Order__c": order_id,
|
57 |
+
"Menu_Item__c": item["menu_item_id"],
|
58 |
+
"Quantity__c": item["quantity"],
|
59 |
+
"Price__c": item["price"],
|
60 |
+
"Total_Price__c": item["total_price"],
|
61 |
+
"Add_Ons__c": ", ".join(addon["name"] for addon in item["add_ons"]),
|
62 |
+
"Special_Instructions__c": item["instructions"],
|
63 |
+
}
|
64 |
+
sf.Order_Item__c.create(order_item)
|
65 |
+
|
66 |
+
return "Order placed successfully!"
|
67 |
+
except Exception as e:
|
68 |
+
return f"Error saving order: {str(e)}"
|
69 |
+
|
70 |
+
# JS for Popup
|
71 |
popup_script = """
|
72 |
<script>
|
73 |
+
function openPopup(id, name, price) {
|
74 |
+
document.getElementById('popup-name').innerText = name;
|
|
|
|
|
|
|
|
|
|
|
75 |
document.getElementById('popup-price').innerText = `$${price}`;
|
76 |
+
document.getElementById('popup').style.display = 'block';
|
77 |
+
document.getElementById('popup').setAttribute('data-id', id);
|
78 |
}
|
79 |
|
80 |
function closePopup() {
|
81 |
document.getElementById('popup').style.display = 'none';
|
82 |
}
|
83 |
+
|
84 |
+
function addToCart() {
|
85 |
+
const id = document.getElementById('popup').getAttribute('data-id');
|
86 |
+
const name = document.getElementById('popup-name').innerText;
|
87 |
+
const price = parseFloat(document.getElementById('popup-price').innerText.replace('$', ''));
|
88 |
+
const quantity = parseInt(document.getElementById('popup-quantity').value);
|
89 |
+
const instructions = document.getElementById('popup-instructions').value;
|
90 |
+
const addOns = Array.from(document.querySelectorAll('input[name="addon"]:checked')).map(addon => ({
|
91 |
+
name: addon.getAttribute('data-name'),
|
92 |
+
price: parseFloat(addon.getAttribute('data-price')),
|
93 |
+
}));
|
94 |
+
|
95 |
+
const totalPrice = price * quantity + addOns.reduce((acc, addon) => acc + addon.price, 0);
|
96 |
+
cart.push({ menu_item_id: id, name, price, quantity, add_ons: addOns, instructions, total_price: totalPrice });
|
97 |
+
closePopup();
|
98 |
+
alert(`${name} added to cart!`);
|
99 |
+
}
|
100 |
</script>
|
101 |
"""
|
102 |
|
103 |
# Gradio App
|
|
|
|
|
|
|
|
|
104 |
with gr.Blocks() as app:
|
105 |
+
menu_items = fetch_menu_from_salesforce()
|
106 |
+
add_ons = fetch_add_ons_from_salesforce()
|
107 |
+
|
108 |
with gr.Row():
|
109 |
+
gr.HTML("<h1>Welcome to Biryani Hub</h1>")
|
110 |
|
111 |
with gr.Row():
|
112 |
+
cart_view = gr.Textbox(label="Cart Items", interactive=False)
|
113 |
+
total_cost_view = gr.Textbox(label="Total Cost", interactive=False)
|
114 |
|
115 |
with gr.Row():
|
116 |
+
menu_output = gr.HTML(generate_menu_html(menu_items))
|
117 |
+
|
118 |
+
with gr.Row():
|
119 |
+
submit_order = gr.Button("Proceed to Order")
|
120 |
+
order_status = gr.Textbox(label="Order Status", interactive=False)
|
121 |
+
|
122 |
+
submit_order.click(
|
123 |
+
lambda: save_order_in_salesforce(cart, sum(item["total_price"] for item in cart)),
|
124 |
+
inputs=[],
|
125 |
+
outputs=order_status,
|
126 |
+
)
|
127 |
|
|
|
128 |
gr.HTML("""
|
129 |
+
<div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 400px; height: 600px; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);">
|
130 |
+
<h3 id="popup-name"></h3>
|
131 |
+
<p id="popup-price"></p>
|
132 |
+
<label>Quantity:</label>
|
133 |
+
<input type="number" id="popup-quantity" value="1" min="1">
|
134 |
+
<label>Special Instructions:</label>
|
135 |
+
<textarea id="popup-instructions" rows="3"></textarea>
|
136 |
+
<label>Add-Ons:</label>
|
137 |
+
<div>
|
138 |
+
""" +
|
139 |
+
"".join(
|
140 |
+
f'<input type="checkbox" name="addon" data-name="{addon["Name"]}" data-price="{addon["Price__c"]}"> {addon["Name"]} (+${addon["Price__c"]})<br>'
|
141 |
+
for addon in add_ons
|
142 |
+
) +
|
143 |
+
"""
|
144 |
+
</div>
|
145 |
+
<button onclick="addToCart()">Add to Cart</button>
|
146 |
+
<button onclick="closePopup()">Close</button>
|
147 |
</div>
|
148 |
+
""")
|
149 |
+
|
150 |
+
gr.HTML(popup_script)
|
151 |
|
152 |
+
app.launch()
|