Rammohan0504 commited on
Commit
d672852
·
verified ·
1 Parent(s): 195fa37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -50
app.py CHANGED
@@ -26,20 +26,7 @@ def add_to_cart(dish_name, selected_extras, quantity, custom_description):
26
  "quantity": quantity,
27
  "custom_description": custom_description
28
  })
29
- return "Your item has been added to the cart!", update_cart()
30
-
31
- # Update cart display
32
- def update_cart():
33
- if not cart_items:
34
- return "<p>Your cart is empty.</p>"
35
- html_content = "<h3>Cart Items</h3><ul style='list-style-type: none; padding: 0;'>"
36
- for item in cart_items:
37
- extras = ", ".join(item["extras"])
38
- html_content += f"<li style='margin-bottom: 10px;'><strong>{item['name']}</strong> (x{item['quantity']})<br>"
39
- html_content += f"Extras: {extras if extras else 'None'}<br>"
40
- html_content += f"Note: {item['custom_description']}</li>"
41
- html_content += "</ul><button style='padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;'>Place Order</button>"
42
- return html_content
43
 
44
  # Function to filter dishes by category
45
  def filter_dishes(category):
@@ -64,7 +51,7 @@ def display_dishes(category):
64
  <div style='text-align: center;'>
65
  <img src='{dish['image']}' alt='{dish['name']}'
66
  style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'>
67
- <button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}')"
68
  style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
69
  border-radius: 5px; cursor: pointer;">Add</button>
70
  </div>
@@ -82,46 +69,43 @@ with gr.Blocks() as demo:
82
  btn_vegan = gr.Button("VEGAN")
83
  btn_halal = gr.Button("HALAL")
84
 
85
- cart_button = gr.HTML("""
86
- <div style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
87
- <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Shopping_cart_icon.svg/512px-Shopping_cart_icon.svg.png' alt='Cart'
88
- style='width: 40px; height: 40px;' onclick="toggleCartPopup()">
89
- </div>
90
- <div id="cart-popup" style="display: none; position: fixed; top: 50px; right: 10px; width: 300px;
91
- background: white; padding: 15px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.2);">
92
- <div id="cart-content">{update_cart()}</div>
93
- <button onclick="closeCartPopup()" style="margin-top: 15px; padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px;">Close</button>
94
- </div>
95
- <script>
96
- function toggleCartPopup() {
97
- const cartPopup = document.getElementById('cart-popup');
98
- cartPopup.style.display = cartPopup.style.display === 'block' ? 'none' : 'block';
99
- }
100
- function closeCartPopup() {
101
- document.getElementById('cart-popup').style.display = 'none';
102
- }
103
- </script>
104
- """)
105
-
106
  dish_display = gr.HTML(value=display_dishes("ALL"))
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  # Button click events
109
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
110
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
111
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
112
 
113
- add_to_cart_button = gr.Button("Add to Cart", visible=False)
114
- add_to_cart_button.click(
115
- add_to_cart,
116
- inputs=[
117
- gr.Textbox(label="Dish Name", visible=False),
118
- gr.Textbox(label="Selected Extras", visible=False),
119
- gr.Number(label="Quantity", value=1, visible=False),
120
- gr.Textbox(label="Custom Description", visible=False)
121
- ],
122
- outputs=[
123
- gr.HTML(label="Cart Notification", visible=True)
124
- ]
125
- )
126
-
127
  demo.launch()
 
26
  "quantity": quantity,
27
  "custom_description": custom_description
28
  })
29
+ return "Your item has been added to the cart!"
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Function to filter dishes by category
32
  def filter_dishes(category):
 
51
  <div style='text-align: center;'>
52
  <img src='{dish['image']}' alt='{dish['name']}'
53
  style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'>
54
+ <button onclick="showMenuPopup('{dish['name']}', '{dish['description']}', '{dish['price']}', '{dish['image']}')"
55
  style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
56
  border-radius: 5px; cursor: pointer;">Add</button>
57
  </div>
 
69
  btn_vegan = gr.Button("VEGAN")
70
  btn_halal = gr.Button("HALAL")
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  dish_display = gr.HTML(value=display_dishes("ALL"))
73
 
74
+ gr.HTML("""
75
+ <div id="menu-popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
76
+ background-color: white; padding: 20px; border-radius: 10px;
77
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; width: 300px; text-align: center;">
78
+ <span onclick="closeMenuPopup()" style="position: absolute; top: 10px; right: 15px; cursor: pointer; font-size: 20px;">&times;</span>
79
+ <img id="popup-image" src="" alt="Dish Image" style="width: 100%; height: auto; border-radius: 10px; margin-bottom: 10px;">
80
+ <h3 id="popup-title" style="margin: 0; font-size: 18px; color: #444;"></h3>
81
+ <p id="popup-description" style="font-size: 14px; color: #666;"></p>
82
+ <p id="popup-price" style="font-size: 16px; color: #444;"></p>
83
+ <button onclick="addToCartFromPopup()" style="margin-top: 10px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">Add to Cart</button>
84
+ </div>
85
+ <script>
86
+ function showMenuPopup(name, description, price, image) {
87
+ document.getElementById('popup-title').textContent = name;
88
+ document.getElementById('popup-description').textContent = description;
89
+ document.getElementById('popup-price').textContent = `Price: ${price}`;
90
+ document.getElementById('popup-image').src = image;
91
+ document.getElementById('menu-popup').style.display = 'block';
92
+ }
93
+
94
+ function closeMenuPopup() {
95
+ document.getElementById('menu-popup').style.display = 'none';
96
+ }
97
+
98
+ function addToCartFromPopup() {
99
+ // Add your integration with Gradio's backend here if necessary.
100
+ alert('Item added to cart!');
101
+ closeMenuPopup();
102
+ }
103
+ </script>
104
+ """)
105
+
106
  # Button click events
107
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
108
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
109
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  demo.launch()