Rammohan0504 commited on
Commit
e3a5950
Β·
verified Β·
1 Parent(s): 420b3b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -60
app.py CHANGED
@@ -48,7 +48,7 @@ def display_dishes(category):
48
  <div style='text-align: center;'>
49
  <img src='{dish['image']}' alt='{dish['name']}'
50
  style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'>
51
- <button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}')"
52
  style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
53
  border-radius: 5px; cursor: pointer;">Add</button>
54
  </div>
@@ -81,6 +81,10 @@ def update_cart():
81
  with gr.Blocks() as demo:
82
  gr.HTML("<h1 style='text-align: center; color: #333;'>πŸ› Dynamic Menu with Cart πŸ›</h1>")
83
 
 
 
 
 
84
  with gr.Tabs() as tabs:
85
  with gr.Tab("Menu"):
86
  with gr.Row():
@@ -92,65 +96,24 @@ with gr.Blocks() as demo:
92
  with gr.Tab("Cart"):
93
  cart_display = gr.HTML(value="<p>Your cart is empty.</p>", elem_id="cart-display")
94
 
95
- # JavaScript for managing the pop-up
96
- gr.HTML(f"""
97
- <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
98
- background-color: white; padding: 20px; border-radius: 10px;
99
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
100
- <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
101
- <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
102
- <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
103
- <h4 style="text-align: left; margin-top: 20px;">Choose a spice level</h4>
104
- <div style="display: flex; flex-wrap: wrap; justify-content: space-between;">
105
- """ + "".join([f'<label><input type="radio" name="spice" value="{spice}">{spice}</label>' for spice in spice_levels]) + """
106
- </div>
107
- <h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
108
- <div style="display: flex; flex-wrap: wrap;">
109
- """ + "".join([f'<label><input type="checkbox" name="extras" value="{extra["name"]}">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
110
- </div>
111
- <button onclick="addItemToCart()" style="margin-top: 20px;">Add to Cart</button>
112
- <button onclick="closePopup()">Close</button>
113
- </div>
114
- <script>
115
- let selectedDish = "";
116
- let selectedExtras = [];
117
- let spiceLevel = "";
118
-
119
- function showPopup(title, description, price) {
120
- selectedDish = title;
121
- document.getElementById('popup-title').textContent = title;
122
- document.getElementById('popup-description').textContent = description;
123
- document.getElementById('popup-price').textContent = price;
124
- document.getElementById('popup').style.display = 'block';
125
- }
126
- function closePopup() {
127
- document.getElementById('popup').style.display = 'none';
128
- }
129
- function addItemToCart() {
130
- const spiceInput = document.querySelector('input[name="spice"]:checked');
131
- spiceLevel = spiceInput ? spiceInput.value : "No Spice Selected";
132
- const extrasInputs = document.querySelectorAll('input[name="extras"]:checked');
133
- selectedExtras = Array.from(extrasInputs).map(input => input.value);
134
-
135
- const cartDisplay = document.getElementById('cart-display');
136
- const xhr = new XMLHttpRequest();
137
- xhr.open("POST", "", true);
138
- xhr.setRequestHeader("Content-Type", "application/json");
139
- xhr.onreadystatechange = function () {
140
- if (xhr.readyState === 4 && xhr.status === 200) {
141
- cartDisplay.innerHTML = xhr.responseText;
142
- document.getElementById('popup').style.display = 'none';
143
- tabs.set_active("Cart");
144
- }
145
- };
146
- xhr.send(JSON.stringify({
147
- dish_name: selectedDish,
148
- spice_level: spiceLevel,
149
- selected_extras: selectedExtras
150
- }));
151
- }
152
- </script>
153
- """)
154
 
155
  # Button click events
156
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
 
48
  <div style='text-align: center;'>
49
  <img src='{dish['image']}' alt='{dish['name']}'
50
  style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'>
51
+ <button onclick="selectDish('{dish['name']}', '{dish['description']}', '{dish['price']}')"
52
  style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
53
  border-radius: 5px; cursor: pointer;">Add</button>
54
  </div>
 
81
  with gr.Blocks() as demo:
82
  gr.HTML("<h1 style='text-align: center; color: #333;'>πŸ› Dynamic Menu with Cart πŸ›</h1>")
83
 
84
+ selected_dish_name = gr.Textbox(visible=False)
85
+ selected_spice_level = gr.Dropdown(spice_levels, label="Choose Spice Level", visible=False)
86
+ selected_extras = gr.CheckboxGroup([extra['name'] for extra in extras], label="Choose Extras", visible=False)
87
+
88
  with gr.Tabs() as tabs:
89
  with gr.Tab("Menu"):
90
  with gr.Row():
 
96
  with gr.Tab("Cart"):
97
  cart_display = gr.HTML(value="<p>Your cart is empty.</p>", elem_id="cart-display")
98
 
99
+ # Interaction for Add to Cart
100
+ add_to_cart_button = gr.Button("Add to Cart", visible=False)
101
+
102
+ def show_dish_popup(dish_name):
103
+ return gr.update(visible=True), gr.update(value=dish_name, visible=True), gr.update(visible=True), gr.update(visible=True)
104
+
105
+ def add_item_to_cart(dish_name, spice_level, extras):
106
+ return add_to_cart(dish_name, spice_level, extras)
107
+
108
+ dish_display.change(show_dish_popup, inputs=[selected_dish_name], outputs=[
109
+ selected_spice_level, selected_dish_name, selected_extras, add_to_cart_button
110
+ ])
111
+
112
+ add_to_cart_button.click(
113
+ add_item_to_cart,
114
+ inputs=[selected_dish_name, selected_spice_level, selected_extras],
115
+ outputs=cart_display
116
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  # Button click events
119
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)