Rammohan0504 commited on
Commit
7c5b342
·
verified ·
1 Parent(s): e572ad3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -67
app.py CHANGED
@@ -77,9 +77,18 @@ def display_dishes(category):
77
  return html_content
78
 
79
 
80
- # Main Gradio App
 
 
 
 
 
 
 
 
 
81
  with gr.Blocks() as demo:
82
- gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
83
 
84
  with gr.Row():
85
  btn_all = gr.Button("ALL")
@@ -87,77 +96,71 @@ with gr.Blocks() as demo:
87
  btn_halal = gr.Button("HALAL")
88
 
89
  dish_display = gr.HTML(value=display_dishes("ALL"))
 
90
 
91
  btn_all.click(lambda: display_dishes("ALL"), outputs=dish_display)
92
  btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=dish_display)
93
  btn_halal.click(lambda: display_dishes("HALAL"), outputs=dish_display)
94
 
95
- cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
96
-
97
- def add_to_cart_handler(item_name, price):
98
- return add_item_to_cart(item_name, price)
99
-
100
- gr.HTML("""
101
- <div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
102
- <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Shopping_cart_icon.svg/512px-Shopping_cart_icon.svg.png' alt='Cart' style='width: 40px;' onclick='toggleCartPopup()'>
103
- </div>
104
- <div id='cart-popup' style='display: none; position: fixed; top: 50px; right: 10px; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); width: 300px; height: auto; max-height: 80vh; overflow-y: auto;'>
105
- <span onclick='toggleCartPopup()' style='position: absolute; top: 10px; right: 15px; font-size: 20px; font-weight: bold; cursor: pointer;'>&times;</span>
106
- <div id='cart-content' style='margin-bottom: 10px;'>Your cart is empty.</div>
107
- <button onclick='placeOrder()' style='margin-top: 10px; padding: 10px; background-color: #007bff; color: white; border: none; border-radius: 5px; display: block; width: 100%;'>Place Order</button>
108
  </div>
109
 
110
- <div id='menu-popup' style='display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); width: 300px;'>
111
- <span onclick='closeMenuPopup()' style='position: absolute; top: 10px; right: 15px; font-size: 20px; font-weight: bold; cursor: pointer;'>&times;</span>
112
- <img id='popup-image' src='' style='width: 100%; border-radius: 5px; margin-bottom: 10px;'>
113
- <h4 id='popup-title'></h4>
114
- <p id='popup-description'></p>
115
- <p id='popup-price'></p>
116
- <button onclick='addItemToCartFromPopup()' style='padding: 5px 10px; background-color: #28a745; color: white; border: none; border-radius: 5px; width: 100%;'>Add to Cart</button>
117
  </div>
118
 
119
- <script>
120
- let selectedItem = '';
121
- let selectedPrice = 0;
122
- let selectedExtras = 'None';
123
-
124
- window.toggleCartPopup = function() {
125
- const popup = document.getElementById('cart-popup');
126
- popup.style.display = popup.style.display === 'block' ? 'none' : 'block';
127
- }
128
-
129
- window.showMenuPopup = function(name, description, price, image) {
130
- selectedItem = name;
131
- selectedPrice = price;
132
- document.getElementById('popup-title').textContent = name;
133
- document.getElementById('popup-description').textContent = description;
134
- document.getElementById('popup-price').textContent = `Price: $${price}`;
135
- document.getElementById('popup-image').src = image;
136
- document.getElementById('menu-popup').style.display = 'block';
137
- }
138
-
139
- window.closeMenuPopup = function() {
140
- document.getElementById('menu-popup').style.display = 'none';
141
- }
142
-
143
- window.addItemToCartFromPopup = function() {
144
- fetch('/run/predict', {
145
- method: 'POST',
146
- headers: { 'Content-Type': 'application/json' },
147
- body: JSON.stringify({ data: [selectedItem, selectedPrice, selectedExtras] })
148
- }).then(response => response.json()).then(data => {
149
- document.getElementById('cart-content').innerHTML = data.data;
150
- alert(`${selectedItem} has been added to your cart.`);
151
- closeMenuPopup();
152
- });
153
- }
154
-
155
- window.placeOrder = function() {
156
- alert('Your order has been placed!');
157
- toggleCartPopup();
158
- }
159
- </script>
160
- """)
161
-
162
-
163
- demo.launch()
 
77
  return html_content
78
 
79
 
80
+ Spice levels and extras options
81
+ spice_levels = ["Mild", "Medium", "Spicy", "Extra Spicy"]
82
+ extras = [
83
+ {"name": "Extra Cheese", "price": "$2.00"},
84
+ {"name": "Extra Sauce", "price": "$1.50"},
85
+ {"name": "No Onions", "price": "$0.00"},
86
+ {"name": "No Garlic", "price": "$0.00"}
87
+ ]
88
+
89
+ # Main Gradio Application
90
  with gr.Blocks() as demo:
91
+ gr.HTML("<h1 style='text-align: center;'>Biryani Hub Menu 🍴</h1>")
92
 
93
  with gr.Row():
94
  btn_all = gr.Button("ALL")
 
96
  btn_halal = gr.Button("HALAL")
97
 
98
  dish_display = gr.HTML(value=display_dishes("ALL"))
99
+ cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
100
 
101
  btn_all.click(lambda: display_dishes("ALL"), outputs=dish_display)
102
  btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=dish_display)
103
  btn_halal.click(lambda: display_dishes("HALAL"), outputs=dish_display)
104
 
105
+ demo.append(gr.HTML(f"""
106
+ <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
107
+ background-color: white; padding: 20px; border-radius: 10px;
108
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
109
+ <img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;">
110
+ <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
111
+ <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
112
+ <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
113
+
114
+ <h4 style="text-align: left; margin-top: 20px;">Choose a spice level</h4>
115
+ <div style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
116
+ """ + "".join([f'<label style="margin-right: 10px; display: block;"><input type="radio" name="spice" value="{spice}" style="margin-right: 5px;">{spice}</label>' for spice in spice_levels]) + """
 
117
  </div>
118
 
119
+ <h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
120
+ <div style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
121
+ """ + "".join([f'<label style="margin-right: 10px; display: block;"><input type="checkbox" name="extras" value="{extra["name"]}" style="margin-right: 5px;">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
 
 
 
 
122
  </div>
123
 
124
+ <button onclick="addToCartFromPopup()"
125
+ style="margin-top: 20px; padding: 10px 20px; background-color: #007bff; color: white; border: none;
126
+ border-radius: 5px; cursor: pointer;">Add to Cart</button>
127
+ <button onclick="closePopup()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745;
128
+ color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button>
129
+ </div>
130
+ <script>
131
+ let selectedItem = '';
132
+ let selectedPrice = 0;
133
+
134
+ function showPopup(image, title, description, price) {
135
+ document.getElementById('popup-image').src = image;
136
+ document.getElementById('popup-title').textContent = title;
137
+ document.getElementById('popup-description').textContent = description;
138
+ document.getElementById('popup-price').textContent = "Price: " + price;
139
+ selectedItem = title;
140
+ selectedPrice = price;
141
+
142
+ // Display the popup
143
+ document.getElementById('popup').style.display = 'block';
144
+ }
145
+
146
+ function closePopup() {
147
+ document.getElementById('popup').style.display = 'none';
148
+ }
149
+
150
+ function addToCartFromPopup() {
151
+ const spiceLevel = document.querySelector('input[name=spice]:checked')?.value || "None";
152
+ const selectedExtras = Array.from(document.querySelectorAll('input[name=extras]:checked')).map(cb => cb.value).join(", ");
153
+ fetch('/run/predict', {
154
+ method: 'POST',
155
+ headers: { 'Content-Type': 'application/json' },
156
+ body: JSON.stringify({ data: [selectedItem, selectedPrice, spiceLevel, selectedExtras] })
157
+ }).then(response => response.json()).then(data => {
158
+ document.querySelector('.cart-html').innerHTML = data.data[0];
159
+ alert(selectedItem + " with " + spiceLevel + " spice level and extras: " + selectedExtras + " has been added to the cart!");
160
+ closePopup();
161
+ });
162
+ }
163
+ </script>
164
+ """))
165
+
166
+ demo.launch()