Rammohan0504 commited on
Commit
9aa2655
·
verified ·
1 Parent(s): e3a5950

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -25
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="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,10 +81,6 @@ 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
- 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():
@@ -94,30 +90,84 @@ with gr.Blocks() as demo:
94
  dish_display = gr.HTML(value=display_dishes("ALL"))
95
 
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)
120
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
121
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  demo.launch()
 
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
  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():
 
90
  dish_display = gr.HTML(value=display_dishes("ALL"))
91
 
92
  with gr.Tab("Cart"):
93
+ cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
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.querySelector("[data-testid='cart_display']");
136
+ fetch("/add_to_cart", {
137
+ method: "POST",
138
+ headers: {
139
+ "Content-Type": "application/json"
140
+ },
141
+ body: JSON.stringify({
142
+ dish_name: selectedDish,
143
+ spice_level: spiceLevel,
144
+ selected_extras: selectedExtras
145
+ })
146
+ }).then(response => response.text()).then(data => {
147
+ cartDisplay.innerHTML = data;
148
+ document.getElementById('popup').style.display = 'none';
149
+ tabs.set_active("Cart");
150
+ });
151
+ }
152
+ </script>
153
+ """)
154
 
155
  # Button click events
156
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
157
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
158
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
159
 
160
+ # Backend function for cart updates
161
+ def handle_cart_update(data):
162
+ dish_name = data["dish_name"]
163
+ spice_level = data["spice_level"]
164
+ selected_extras = data["selected_extras"]
165
+ return add_to_cart(dish_name, spice_level, selected_extras)
166
+
167
+ gr.Interface(
168
+ fn=handle_cart_update,
169
+ inputs=["text", "text", "list"],
170
+ outputs="html",
171
+ )
172
+
173
  demo.launch()