Rammohan0504 commited on
Commit
6a4a33f
·
verified ·
1 Parent(s): 0f07fbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -51
app.py CHANGED
@@ -67,77 +67,63 @@ def display_dishes(category):
67
  html_content += "</div>"
68
  return html_content
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  # Main Gradio App
71
  with gr.Blocks() as demo:
72
  gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Biryani Hub Menu 🍛</h1>")
73
 
74
- # Buttons for category selection
75
  with gr.Row():
76
  btn_all = gr.Button("ALL")
77
  btn_vegan = gr.Button("VEGAN")
78
  btn_halal = gr.Button("HALAL")
79
 
80
- # Dish display
81
  dish_display = gr.HTML(value=display_dishes("ALL"))
 
82
 
83
- # JavaScript for managing the pop-up
84
  gr.HTML(f"""
85
- <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
86
- background-color: white; padding: 20px; border-radius: 10px;
87
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
88
- <img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;">
89
- <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
90
- <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
91
- <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
92
-
93
- <h4 style="text-align: left; margin-top: 20px;">Choose a spice level</h4>
94
- <div style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
95
- """ + "".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]) + """
96
- </div>
97
-
98
- <h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
99
- <div style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
100
- """ + "".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]) + """
101
- </div>
102
-
103
- <button onclick="addToCart(document.getElementById('popup-title').textContent, document.querySelector('input[name=spice]:checked').value)"
104
- style="margin-top: 20px; padding: 10px 20px; background-color: #007bff; color: white; border: none;
105
- border-radius: 5px; cursor: pointer;">Add to Cart</button>
106
- <button onclick="closePopup()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745;
107
- color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button>
108
  </div>
109
  <script>
110
- function showPopup(image, title, description, price) {
111
- document.getElementById('popup-image').src = image;
112
- document.getElementById('popup-title').textContent = title;
113
- document.getElementById('popup-description').textContent = description;
114
- document.getElementById('popup-price').textContent = "Price: " + price;
115
-
116
- // Display the popup
117
  document.getElementById('popup').style.display = 'block';
118
  }
119
  function closePopup() {
120
  document.getElementById('popup').style.display = 'none';
121
  }
122
- function addToCart(dishName, spiceLevel) {
123
- const selectedExtras = Array.from(document.querySelectorAll('input[name=extras]:checked')).map(cb => cb.value).join(", ");
124
- alert(dishName + " with " + spiceLevel + " spice level and extras: " + selectedExtras + " has been added to the cart!");
 
 
 
125
  }
126
  </script>
127
- """)
128
 
129
- # Button click events
130
- btn_all.click(
131
- lambda: gr.update(value=display_dishes("ALL")),
132
- outputs=dish_display
133
- )
134
- btn_vegan.click(
135
- lambda: gr.update(value=display_dishes("VEGAN")),
136
- outputs=dish_display
137
- )
138
- btn_halal.click(
139
- lambda: gr.update(value=display_dishes("HALAL")),
140
- outputs=dish_display
141
- )
142
 
143
- demo.launch()
 
67
  html_content += "</div>"
68
  return html_content
69
 
70
+ # Add to cart
71
+ def add_to_cart(item_name, spice_level, extras_selected):
72
+ cart.append({"name": item_name, "spice": spice_level, "extras": extras_selected})
73
+ return "Item added to cart successfully!"
74
+
75
+ # Display cart items
76
+ def display_cart():
77
+ if not cart:
78
+ return "<p>Your cart is empty.</p>"
79
+ html_content = "<div style='text-align: left;'>"
80
+ for item in cart:
81
+ html_content += f"<p><b>{item['name']}</b> - Spice: {item['spice']} - Extras: {', '.join(item['extras'])}</p>"
82
+ html_content += "</div>"
83
+ return html_content
84
+
85
  # Main Gradio App
86
  with gr.Blocks() as demo:
87
  gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Biryani Hub Menu 🍛</h1>")
88
 
 
89
  with gr.Row():
90
  btn_all = gr.Button("ALL")
91
  btn_vegan = gr.Button("VEGAN")
92
  btn_halal = gr.Button("HALAL")
93
 
 
94
  dish_display = gr.HTML(value=display_dishes("ALL"))
95
+ cart_display = gr.HTML(value=display_cart())
96
 
 
97
  gr.HTML(f"""
98
+ <div id="popup" style="display: none;">
99
+ <h2 id="popup-title"></h2>
100
+ <label>Spice Level:</label>
101
+ <select id="spice-select">""" + "".join([f'<option>{spice}</option>' for spice in spice_levels]) + """</select>
102
+ <label>Extras:</label>
103
+ <div id="extras-select">""" + "".join([f'<label><input type="checkbox" value="{extra['name']}">{extra['name']} + {extra['price']}</label>' for extra in extras]) + """</div>
104
+ <button onclick="submitPopup()">Add to Cart</button>
105
+ <button onclick="closePopup()">Close</button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  </div>
107
  <script>
108
+ function showPopup(name, price) {
109
+ document.getElementById('popup-title').textContent = name;
 
 
 
 
 
110
  document.getElementById('popup').style.display = 'block';
111
  }
112
  function closePopup() {
113
  document.getElementById('popup').style.display = 'none';
114
  }
115
+ function submitPopup() {
116
+ const name = document.getElementById('popup-title').textContent;
117
+ const spice = document.getElementById('spice-select').value;
118
+ const extras = Array.from(document.querySelectorAll('#extras-select input:checked')).map(cb => cb.value);
119
+ gr.Interface.postMessage('add_to_cart', [name, spice, extras]);
120
+ closePopup();
121
  }
122
  </script>
123
+ ")
124
 
125
+ btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
126
+ btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
127
+ btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
 
 
 
 
 
 
 
 
 
 
128
 
129
+ demo.launch()