Rammohan0504 commited on
Commit
6cc2eff
·
verified ·
1 Parent(s): 979dd3c

Update app.py

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