geethareddy commited on
Commit
d0caa41
·
verified ·
1 Parent(s): 2f53b58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -19
app.py CHANGED
@@ -73,7 +73,78 @@ def load_add_ons_from_salesforce():
73
  except Exception as e:
74
  return []
75
 
76
- # Function to filter menu items
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  def filter_menu(preference):
78
  menu_data = load_menu_from_salesforce()
79
 
@@ -96,7 +167,7 @@ def filter_menu(preference):
96
  html_content += f"""
97
  <div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
98
  <img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;"
99
- onclick="openModal('{item['Name']}', '{item.get('Image2__c', '')}', '{item['Description__c']}', '{item['Price__c']}')">
100
  <div style="padding: 10px;">
101
  <h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
102
  <p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
@@ -111,19 +182,7 @@ def filter_menu(preference):
111
  return "<p>No items match your filter.</p>"
112
  return html_content
113
 
114
- # Function to add item to cart and Salesforce
115
- def add_to_cart(item_name, item_price, quantity, special_instructions, selected_addons):
116
- cart_item = sf.Cart_Item__c.create({
117
- 'Name': item_name,
118
- 'Quantity__c': quantity,
119
- 'Price__c': item_price,
120
- 'Special_Instructions__c': special_instructions,
121
- 'Add_ons__c': [addon['Id'] for addon in selected_addons]
122
- })
123
- # Additional code for cart management
124
- return "Item added to cart!"
125
-
126
- # Function to handle modal popup HTML and content
127
  def open_modal(item_name, item_image, item_description, item_price):
128
  add_ons = load_add_ons_from_salesforce()
129
  add_ons_html = ""
@@ -152,15 +211,64 @@ def open_modal(item_name, item_image, item_description, item_price):
152
  """
153
  return gr.HTML(modal_content)
154
 
 
155
  # Gradio app definition
156
  with gr.Blocks() as app:
157
  with gr.Row():
158
  gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
159
 
160
- with gr.Row(visible=True) as menu_page:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  with gr.Column():
162
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
163
- menu_output = gr.HTML() # Dynamically updated based on the filter
164
- preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
- app.launch()
 
73
  except Exception as e:
74
  return []
75
 
76
+ # Cart Management Functions
77
+ cart = []
78
+ total_cart_cost = 0
79
+ cart_id = None # Store the Cart__c record ID
80
+
81
+ # Function to add item to the cart and Salesforce
82
+ def add_to_cart(item_name, item_price, quantity, special_instructions, selected_addons):
83
+ global total_cart_cost, cart_id
84
+ total_cost = item_price * quantity
85
+ addons_cost = sum([addon['Price__c'] for addon in selected_addons])
86
+
87
+ # If this is the first item, create a new Cart__c record in Salesforce
88
+ if not cart_id:
89
+ cart_record = sf.Cart__c.create({
90
+ 'Total_Cost__c': total_cost + addons_cost
91
+ })
92
+ cart_id = cart_record['Id']
93
+ else:
94
+ # Update the total cost of the cart
95
+ sf.Cart__c.update(cart_id, {
96
+ 'Total_Cost__c': total_cart_cost + total_cost + addons_cost
97
+ })
98
+
99
+ # Create the Cart_Item__c record and link it to Cart__c
100
+ cart_item = sf.Cart_Item__c.create({
101
+ 'Name': item_name,
102
+ 'Quantity__c': quantity,
103
+ 'Price__c': item_price,
104
+ 'Special_Instructions__c': special_instructions,
105
+ 'Cart__c': cart_id, # Link this item to the Cart__c
106
+ 'Add_ons__c': [addon['Id'] for addon in selected_addons] # Link selected add-ons
107
+ })
108
+
109
+ # Add the item to the cart list (for local tracking)
110
+ cart.append({
111
+ 'name': item_name,
112
+ 'price': item_price,
113
+ 'quantity': quantity,
114
+ 'instructions': special_instructions,
115
+ 'total_cost': total_cost + addons_cost,
116
+ 'addons': selected_addons
117
+ })
118
+
119
+ total_cart_cost += total_cost + addons_cost
120
+ return cart, total_cart_cost
121
+
122
+ # Function to remove item from the cart
123
+ def remove_from_cart(index):
124
+ global total_cart_cost
125
+ total_cart_cost -= cart[index]['total_cost']
126
+ cart.pop(index)
127
+
128
+ # Update the Cart__c record in Salesforce to reflect the new total cost
129
+ sf.Cart__c.update(cart_id, {
130
+ 'Total_Cost__c': total_cart_cost
131
+ })
132
+
133
+ return cart, total_cart_cost
134
+
135
+ # Checkout Summary
136
+ def proceed_to_checkout():
137
+ summary = ""
138
+ for item in cart:
139
+ summary += f"{item['name']} (x{item['quantity']}) - ${item['total_cost']:.2f}\n"
140
+ if item['instructions']:
141
+ summary += f"Instructions: {item['instructions']}\n"
142
+ if item['addons']:
143
+ summary += f"Add-ons: {', '.join([addon['Name'] for addon in item['addons']])}\n"
144
+ summary += f"Total Bill: ${total_cart_cost:.2f}"
145
+ return summary
146
+
147
+ # Function to filter menu items based on user preference
148
  def filter_menu(preference):
149
  menu_data = load_menu_from_salesforce()
150
 
 
167
  html_content += f"""
168
  <div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
169
  <img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;"
170
+ onclick="openModal('{item['Name']}', '{item.get('Image2__c', '')}', '{item['Description__c']}', '${item['Price__c']}')">
171
  <div style="padding: 10px;">
172
  <h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
173
  <p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
 
182
  return "<p>No items match your filter.</p>"
183
  return html_content
184
 
185
+ # Modal Simulation Function (handled in Python)
 
 
 
 
 
 
 
 
 
 
 
 
186
  def open_modal(item_name, item_image, item_description, item_price):
187
  add_ons = load_add_ons_from_salesforce()
188
  add_ons_html = ""
 
211
  """
212
  return gr.HTML(modal_content)
213
 
214
+
215
  # Gradio app definition
216
  with gr.Blocks() as app:
217
  with gr.Row():
218
  gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
219
 
220
+ with gr.Row(visible=True) as login_page:
221
+ with gr.Column():
222
+ login_email = gr.Textbox(label="Email")
223
+ login_password = gr.Textbox(label="Password", type="password")
224
+ login_button = gr.Button("Login")
225
+ signup_button = gr.Button("Go to Signup")
226
+ login_output = gr.Textbox(label="Status")
227
+
228
+ with gr.Row(visible=False) as signup_page:
229
+ with gr.Column():
230
+ signup_name = gr.Textbox(label="Name")
231
+ signup_email = gr.Textbox(label="Email")
232
+ signup_phone = gr.Textbox(label="Phone")
233
+ signup_password = gr.Textbox(label="Password", type="password")
234
+ submit_signup = gr.Button("Signup")
235
+ login_redirect = gr.Button("Go to Login")
236
+ signup_output = gr.Textbox(label="Status")
237
+
238
+ with gr.Row(visible=False) as menu_page:
239
  with gr.Column():
240
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
241
+ menu_output = gr.HTML()
242
+ gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer;'>View Cart</div>")
243
+ cart_summary = gr.Textbox(label="Cart Summary", interactive=False)
244
+ checkout_button = gr.Button("Proceed to Checkout")
245
+
246
+ checkout_button.click(proceed_to_checkout, [], cart_summary)
247
+
248
+ login_button.click(
249
+ lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
250
+ if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
251
+ [login_email, login_password], [login_page, menu_page, menu_output, login_output]
252
+ )
253
+
254
+ submit_signup.click(
255
+ lambda name, email, phone, password: signup(name, email, phone, password),
256
+ inputs=[signup_name, signup_email, signup_phone, signup_password],
257
+ outputs=signup_output
258
+ )
259
+
260
+ signup_button.click(
261
+ lambda: (gr.update(visible=False), gr.update(visible=True)),
262
+ inputs=[],
263
+ outputs=[login_page, signup_page]
264
+ )
265
+
266
+ login_redirect.click(
267
+ lambda: (gr.update(visible=True), gr.update(visible=False)),
268
+ inputs=[],
269
+ outputs=[login_page, signup_page]
270
+ )
271
+
272
+ preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
273
 
274
+ app.launch()