Rammohan0504 commited on
Commit
195fa37
·
verified ·
1 Parent(s): e012435

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -86
app.py CHANGED
@@ -26,7 +26,7 @@ def add_to_cart(dish_name, selected_extras, quantity, custom_description):
26
  "quantity": quantity,
27
  "custom_description": custom_description
28
  })
29
- return update_cart()
30
 
31
  # Update cart display
32
  def update_cart():
@@ -38,7 +38,7 @@ def update_cart():
38
  html_content += f"<li style='margin-bottom: 10px;'><strong>{item['name']}</strong> (x{item['quantity']})<br>"
39
  html_content += f"Extras: {extras if extras else 'None'}<br>"
40
  html_content += f"Note: {item['custom_description']}</li>"
41
- html_content += "</ul>"
42
  return html_content
43
 
44
  # Function to filter dishes by category
@@ -77,100 +77,51 @@ def display_dishes(category):
77
  with gr.Blocks() as demo:
78
  gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
79
 
80
- with gr.Tabs() as tabs:
81
- with gr.Tab("Menu"):
82
- with gr.Row():
83
- btn_all = gr.Button("ALL")
84
- btn_vegan = gr.Button("VEGAN")
85
- btn_halal = gr.Button("HALAL")
86
- dish_display = gr.HTML(value=display_dishes("ALL"))
87
 
88
- with gr.Tab("Cart"):
89
- cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  # Button click events
92
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
93
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
94
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
95
 
96
- # Add to cart button with input fields
97
- add_to_cart_inputs = [
98
- gr.Textbox(label="Dish Name", visible=False),
99
- gr.Textbox(label="Selected Extras", visible=False),
100
- gr.Number(label="Quantity", value=1, visible=False),
101
- gr.Textbox(label="Custom Description", visible=False)
102
- ]
103
-
104
  add_to_cart_button = gr.Button("Add to Cart", visible=False)
105
  add_to_cart_button.click(
106
  add_to_cart,
107
- inputs=add_to_cart_inputs,
108
- outputs=[cart_display]
 
 
 
 
 
 
 
109
  )
110
 
111
- gr.HTML(f"""
112
- <div id="popup" style="display:none; position: fixed; cursor: pointer; top: 50%; left: 50%; transform: translate(-50%, -50%);
113
- background-color: white; padding: 20px; border-radius: 10px;
114
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
115
- <div style="position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 20px; font-weight: bold;" onclick="closePopup()">&times;</div>
116
- <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
117
- <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
118
- <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
119
- <h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
120
- <div style="display: flex; flex-wrap: wrap;">
121
- """ + "".join([f'<label><input type="checkbox" name="extras" value="{extra["name"]}">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
122
- </div>
123
- <h4 style="text-align: left; margin-top: 20px;">Quantity</h4>
124
- <div style="display: flex; align-items: center;">
125
- <button onclick="decreaseQuantity()" style="padding: 5px 10px; margin-right: 5px;">-</button>
126
- <input type="number" id="quantity" min="1" value="1" style="width: 50px; text-align: center;" readonly>
127
- <button onclick="increaseQuantity()" style="padding: 5px 10px; margin-left: 5px;">+</button>
128
- </div>
129
- <h4 style="text-align: left; margin-top: 20px;">Add Custom Notes</h4>
130
- <textarea id="custom-description" rows="2" style="width: 100%; padding: 5px; border-radius: 5px; border: 1px solid #ddd;"></textarea>
131
- <button onclick="addItemToCart()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Add to Cart</button>
132
- </div>
133
- <script>
134
- let selectedDish = "";
135
- let selectedExtras = [];
136
-
137
- function showPopup(title, description, price) {
138
- selectedDish = title;
139
- document.getElementById('popup-title').textContent = title;
140
- document.getElementById('popup-description').textContent = description;
141
- document.getElementById('popup-price').textContent = price;
142
- document.getElementById('popup').style.display = 'block';
143
- }
144
- function closePopup() {
145
- document.getElementById('popup').style.display = 'none';
146
- }
147
- function increaseQuantity() {
148
- const quantityInput = document.getElementById('quantity');
149
- quantityInput.value = parseInt(quantityInput.value) + 1;
150
- }
151
- function decreaseQuantity() {
152
- const quantityInput = document.getElementById('quantity');
153
- if (parseInt(quantityInput.value) > 1) {
154
- quantityInput.value = parseInt(quantityInput.value) - 1;
155
- }
156
- }
157
- function addItemToCart() {
158
- const extrasInputs = document.querySelectorAll('input[name="extras"]:checked');
159
- selectedExtras = Array.from(extrasInputs).map(input => input.value);
160
- const quantity = document.getElementById('quantity').value || 1;
161
- const customDescription = document.getElementById('custom-description').value || "None";
162
-
163
- // Pass to Gradio backend
164
- gradioApp().querySelector("#component-0 input").value = selectedDish;
165
- gradioApp().querySelector("#component-1 input").value = JSON.stringify(selectedExtras);
166
- gradioApp().querySelector("#component-2 input").value = quantity;
167
- gradioApp().querySelector("#component-3 input").value = customDescription;
168
- gradioApp().querySelector("#component-4 button").click();
169
- closePopup();
170
- }
171
- </script>
172
- """)
173
-
174
  demo.launch()
175
-
176
-
 
26
  "quantity": quantity,
27
  "custom_description": custom_description
28
  })
29
+ return "Your item has been added to the cart!", update_cart()
30
 
31
  # Update cart display
32
  def update_cart():
 
38
  html_content += f"<li style='margin-bottom: 10px;'><strong>{item['name']}</strong> (x{item['quantity']})<br>"
39
  html_content += f"Extras: {extras if extras else 'None'}<br>"
40
  html_content += f"Note: {item['custom_description']}</li>"
41
+ html_content += "</ul><button style='padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;'>Place Order</button>"
42
  return html_content
43
 
44
  # Function to filter dishes by category
 
77
  with gr.Blocks() as demo:
78
  gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
79
 
80
+ with gr.Row():
81
+ btn_all = gr.Button("ALL")
82
+ btn_vegan = gr.Button("VEGAN")
83
+ btn_halal = gr.Button("HALAL")
 
 
 
84
 
85
+ cart_button = gr.HTML("""
86
+ <div style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
87
+ <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Shopping_cart_icon.svg/512px-Shopping_cart_icon.svg.png' alt='Cart'
88
+ style='width: 40px; height: 40px;' onclick="toggleCartPopup()">
89
+ </div>
90
+ <div id="cart-popup" style="display: none; position: fixed; top: 50px; right: 10px; width: 300px;
91
+ background: white; padding: 15px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.2);">
92
+ <div id="cart-content">{update_cart()}</div>
93
+ <button onclick="closeCartPopup()" style="margin-top: 15px; padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px;">Close</button>
94
+ </div>
95
+ <script>
96
+ function toggleCartPopup() {
97
+ const cartPopup = document.getElementById('cart-popup');
98
+ cartPopup.style.display = cartPopup.style.display === 'block' ? 'none' : 'block';
99
+ }
100
+ function closeCartPopup() {
101
+ document.getElementById('cart-popup').style.display = 'none';
102
+ }
103
+ </script>
104
+ """)
105
+
106
+ dish_display = gr.HTML(value=display_dishes("ALL"))
107
 
108
  # Button click events
109
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
110
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
111
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
112
 
 
 
 
 
 
 
 
 
113
  add_to_cart_button = gr.Button("Add to Cart", visible=False)
114
  add_to_cart_button.click(
115
  add_to_cart,
116
+ inputs=[
117
+ gr.Textbox(label="Dish Name", visible=False),
118
+ gr.Textbox(label="Selected Extras", visible=False),
119
+ gr.Number(label="Quantity", value=1, visible=False),
120
+ gr.Textbox(label="Custom Description", visible=False)
121
+ ],
122
+ outputs=[
123
+ gr.HTML(label="Cart Notification", visible=True)
124
+ ]
125
  )
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  demo.launch()