Rammohan0504 commited on
Commit
b950414
·
verified ·
1 Parent(s): d54876f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -94
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
 
3
  # Menu data
@@ -10,132 +11,170 @@ menu_data = [
10
  "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/chicken_biryani.jpg",
11
  "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.",
12
  "price": "$12.99"},
13
- {"name": "Paneer Butter Masala", "category": "VEGAN",
14
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/paneer_butter_masala.jpg",
15
- "description": "Cottage cheese cubes in a creamy, mildly spiced tomato gravy.",
16
- "price": "$10.99"},
17
- {"name": "Grilled Chicken Salad", "category": "HALAL",
18
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/grilled_chicken_salad.jpg",
19
- "description": "Healthy grilled chicken slices with a bed of fresh greens.",
20
- "price": "$9.99"},
21
- {"name": "Margherita Pizza", "category": "VEGAN",
22
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/margherita_pizza.jpg",
23
- "description": "Classic Italian pizza topped with fresh basil and mozzarella.",
24
- "price": "$11.99"},
25
- {"name": "Falafel Wrap", "category": "VEGAN",
26
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/falafel_wrap.jpg",
27
- "description": "Middle Eastern spiced falafel balls wrapped in pita bread.",
28
- "price": "$7.99"},
29
- {"name": "Beef Kebab Platter", "category": "HALAL",
30
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/beef_kebab_platter.jpg",
31
- "description": "Tender beef kebabs served with aromatic rice and salad.",
32
- "price": "$14.99"},
33
- {"name": "Veggie Sushi Rolls", "category": "VEGAN",
34
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veggie_sushi_rolls.jpg",
35
- "description": "Japanese sushi rolls with fresh vegetables and soy sauce.",
36
- "price": "$13.99"},
37
- {"name": "Lamb Curry", "category": "HALAL",
38
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/lamb_curry.jpg",
39
- "description": "Rich and flavorful lamb curry cooked with Indian spices.",
40
- "price": "$15.99"},
41
- {"name": "Pasta Primavera", "category": "VEGAN",
42
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/pasta_primavera.jpg",
43
- "description": "Italian pasta tossed with seasonal vegetables and olive oil.",
44
- "price": "$9.99"},
45
- {"name": "Chicken Shawarma", "category": "HALAL",
46
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/chicken_shawarma.jpg",
47
- "description": "Juicy chicken wrapped in soft pita with tahini sauce.",
48
- "price": "$8.99"},
49
- {"name": "Quinoa Salad", "category": "VEGAN",
50
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/quinoa_salad.jpg",
51
- "description": "Nutritious quinoa with fresh veggies and a lemon vinaigrette.",
52
- "price": "$8.49"},
53
- {"name": "Butter Chicken", "category": "HALAL",
54
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/butter_chicken.jpg",
55
- "description": "Classic Indian butter chicken in a creamy tomato-based gravy.",
56
- "price": "$13.99"},
57
- {"name": "Tofu Stir Fry", "category": "VEGAN",
58
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/tofu_stir_fry.jpg",
59
- "description": "Asian-style tofu stir-fried with mixed vegetables and soy sauce.",
60
- "price": "$8.99"},
61
- {"name": "Mixed Grill Platter", "category": "HALAL",
62
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/mixed_grill_platter.jpg",
63
- "description": "Selection of grilled chicken, beef, and lamb served with sides.",
64
- "price": "$18.99"},
65
- {"name": "Hummus with Pita", "category": "VEGAN",
66
- "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/hummus_with_pita.jpg",
67
- "description": "Creamy hummus served with warm pita bread.",
68
- "price": "$6.99"}
69
  ]
70
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  # Filter dishes by category
 
 
 
 
 
 
73
  def display_dishes(category):
74
- dishes = [dish for dish in menu_data if category == "ALL" or dish["category"] == category]
75
- html = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
76
- for dish in dishes:
77
- html += f"""
78
- <div style='margin: 10px; width: 300px; border: 1px solid #ddd; border-radius: 10px; padding: 10px; text-align: center;'>
79
- <img src='{dish['image']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 10px;'>
80
- <h4>{dish['name']}</h4>
81
- <p>{dish['description']}</p>
82
- <p><strong>{dish['price']}</strong></p>
83
- <button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}')"
84
- style='padding: 10px 15px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;'>Add</button>
 
 
 
 
 
 
 
 
85
  </div>
86
  """
87
- html += "</div>"
88
- return html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # Main Gradio App
91
  with gr.Blocks() as demo:
92
- gr.HTML("<h1 style='text-align: center;'>Dynamic Menu</h1>")
93
 
94
- with gr.Tabs():
95
  with gr.Tab("Menu"):
96
  with gr.Row():
97
  btn_all = gr.Button("ALL")
98
  btn_vegan = gr.Button("VEGAN")
99
  btn_halal = gr.Button("HALAL")
100
- menu_html = gr.HTML(value=display_dishes("ALL"))
101
 
102
  with gr.Tab("Cart"):
103
- gr.HTML("<p>Your cart is empty.</p>")
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
- btn_all.click(lambda: display_dishes("ALL"), outputs=[menu_html])
106
- btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=[menu_html])
107
- btn_halal.click(lambda: display_dishes("HALAL"), outputs=[menu_html])
 
 
 
108
 
109
- gr.HTML("""
110
- <div id="popup" style="display: none; position: absolute; left: 50%; transform: translateX(-50%);
111
  background-color: white; padding: 20px; border-radius: 10px;
112
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 400px;">
113
  <div style="position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 20px; font-weight: bold;" onclick="closePopup()">&times;</div>
114
  <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
115
  <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
116
  <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
117
- <div id="popup-content" style="margin-top: 20px; overflow-y: auto; height: 150px;">
118
- <!-- Additional Content -->
 
119
  </div>
120
- <button onclick="closePopup()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Close</button>
 
 
 
 
 
 
 
 
121
  </div>
122
  <script>
123
- function showPopup(title, description, price) {{
124
- const popup = document.getElementById('popup');
 
 
 
125
  document.getElementById('popup-title').textContent = title;
126
  document.getElementById('popup-description').textContent = description;
127
  document.getElementById('popup-price').textContent = price;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
- // Adjust position based on scroll
130
- const scrollY = window.scrollY;
131
- popup.style.top = `${{scrollY + 200}}px`; // Dynamically position 200px below current scroll
132
- popup.style.display = 'block'; // Show the popup
133
- }}
134
-
135
- function closePopup() {{
136
- const popup = document.getElementById('popup');
137
- popup.style.display = 'none'; // Hide the popup
138
- }}
139
  </script>
140
  """)
141
 
 
1
+
2
  import gradio as gr
3
 
4
  # Menu data
 
11
  "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/chicken_biryani.jpg",
12
  "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.",
13
  "price": "$12.99"},
14
+ # Add more dishes here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  ]
16
 
17
+ # Cart data (persistent across sessions)
18
+ cart_items = []
19
+
20
+ # Extras
21
+ extras = [
22
+ {"name": "Coke 500ml", "price": "$2.00"},
23
+ {"name": "Sprite 500ml", "price": "$2.00"},
24
+ {"name": "Extra Raitha 4oz", "price": "$1.00"},
25
+ {"name": "Extra Salan 8oz", "price": "$2.00"},
26
+ ]
27
 
28
  # Filter dishes by category
29
+ def filter_dishes(category):
30
+ if category == "ALL":
31
+ return menu_data
32
+ return [dish for dish in menu_data if dish["category"] == category]
33
+
34
+ # Generate dish cards with "Add" button
35
  def display_dishes(category):
36
+ filtered_dishes = filter_dishes(category)
37
+ html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%; height:100%; overflow-y: auto;'>"
38
+ for dish in filtered_dishes:
39
+ html_content += f"""
40
+ <div style='margin: 10px; display: flex; align-items: center; justify-content: space-between;
41
+ width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px;
42
+ background-color: #f9f9f9;'>
43
+ <div style='flex: 1; margin-right: 20px; text-align: left;'>
44
+ <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4>
45
+ <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p>
46
+ <p style='font-size: 16px; color: #444; margin: 0;'>Price: {dish['price']}</p>
47
+ </div>
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>
55
  </div>
56
  """
57
+ html_content += "</div>"
58
+ return html_content
59
+
60
+ # Function to handle adding items to the cart
61
+ def add_to_cart(dish_name, selected_extras, quantity, custom_description):
62
+ cart_items.append({
63
+ "name": dish_name,
64
+ "extras": selected_extras,
65
+ "quantity": quantity,
66
+ "custom_description": custom_description
67
+ })
68
+ return update_cart()
69
+
70
+ # Update cart display
71
+ def update_cart():
72
+ if not cart_items:
73
+ return "<p>Your cart is empty.</p>"
74
+ html_content = "<h3>Cart Items</h3><ul style='list-style-type: none; padding: 0;'>"
75
+ for item in cart_items:
76
+ extras = ", ".join(item["extras"])
77
+ html_content += f"<li style='margin-bottom: 10px;'><strong>{item['name']}</strong> (x{item['quantity']})<br>"
78
+ html_content += f"Extras: {extras if extras else 'None'}<br>"
79
+ html_content += f"Note: {item['custom_description']}</li>"
80
+ html_content += "</ul>"
81
+ return html_content
82
 
83
  # Main Gradio App
84
  with gr.Blocks() as demo:
85
+ gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
86
 
87
+ with gr.Tabs() as tabs:
88
  with gr.Tab("Menu"):
89
  with gr.Row():
90
  btn_all = gr.Button("ALL")
91
  btn_vegan = gr.Button("VEGAN")
92
  btn_halal = gr.Button("HALAL")
93
+ dish_display = gr.HTML(value=display_dishes("ALL"))
94
 
95
  with gr.Tab("Cart"):
96
+ cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
97
+
98
+ # Button click events
99
+ btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
100
+ btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
101
+ btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
102
+
103
+ # Add to cart button with input fields
104
+ add_to_cart_inputs = [
105
+ gr.Textbox(label="Dish Name", visible=False),
106
+ gr.Textbox(label="Selected Extras", visible=False),
107
+ gr.Number(label="Quantity", value=1, visible=False),
108
+ gr.Textbox(label="Custom Description", visible=False)
109
+ ]
110
 
111
+ add_to_cart_button = gr.Button("Add to Cart", visible=False)
112
+ add_to_cart_button.click(
113
+ add_to_cart,
114
+ inputs=add_to_cart_inputs,
115
+ outputs=[cart_display]
116
+ )
117
 
118
+ gr.HTML(f"""
119
+ <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
120
  background-color: white; padding: 20px; border-radius: 10px;
121
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
122
  <div style="position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 20px; font-weight: bold;" onclick="closePopup()">&times;</div>
123
  <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
124
  <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
125
  <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
126
+ <h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
127
+ <div style="display: flex; flex-wrap: wrap;">
128
+ """ + "".join([f'<label><input type="checkbox" name="extras" value="{extra["name"]}">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
129
  </div>
130
+ <h4 style="text-align: left; margin-top: 20px;">Quantity</h4>
131
+ <div style="display: flex; align-items: center;">
132
+ <button onclick="decreaseQuantity()" style="padding: 5px 10px; margin-right: 5px;">-</button>
133
+ <input type="number" id="quantity" min="1" value="1" style="width: 50px; text-align: center;" readonly>
134
+ <button onclick="increaseQuantity()" style="padding: 5px 10px; margin-left: 5px;">+</button>
135
+ </div>
136
+ <h4 style="text-align: left; margin-top: 20px;">Add Custom Notes</h4>
137
+ <textarea id="custom-description" rows="2" style="width: 100%; padding: 5px; border-radius: 5px; border: 1px solid #ddd;"></textarea>
138
+ <button onclick="addItemToCart()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Add to Cart</button>
139
  </div>
140
  <script>
141
+ let selectedDish = "";
142
+ let selectedExtras = [];
143
+
144
+ function showPopup(title, description, price) {
145
+ selectedDish = title;
146
  document.getElementById('popup-title').textContent = title;
147
  document.getElementById('popup-description').textContent = description;
148
  document.getElementById('popup-price').textContent = price;
149
+ document.getElementById('popup').style.display = 'block';
150
+ }
151
+ function closePopup() {
152
+ document.getElementById('popup').style.display = 'none';
153
+ }
154
+ function increaseQuantity() {
155
+ const quantityInput = document.getElementById('quantity');
156
+ quantityInput.value = parseInt(quantityInput.value) + 1;
157
+ }
158
+ function decreaseQuantity() {
159
+ const quantityInput = document.getElementById('quantity');
160
+ if (parseInt(quantityInput.value) > 1) {
161
+ quantityInput.value = parseInt(quantityInput.value) - 1;
162
+ }
163
+ }
164
+ function addItemToCart() {
165
+ const extrasInputs = document.querySelectorAll('input[name="extras"]:checked');
166
+ selectedExtras = Array.from(extrasInputs).map(input => input.value);
167
+ const quantity = document.getElementById('quantity').value || 1;
168
+ const customDescription = document.getElementById('custom-description').value || "None";
169
 
170
+ // Pass to Gradio backend
171
+ gradioApp().querySelector("#component-0 input").value = selectedDish;
172
+ gradioApp().querySelector("#component-1 input").value = JSON.stringify(selectedExtras);
173
+ gradioApp().querySelector("#component-2 input").value = quantity;
174
+ gradioApp().querySelector("#component-3 input").value = customDescription;
175
+ gradioApp().querySelector("#component-4 button").click();
176
+ closePopup();
177
+ }
 
 
178
  </script>
179
  """)
180