Rammohan0504 commited on
Commit
0511c65
·
verified ·
1 Parent(s): d672852

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -63
app.py CHANGED
@@ -10,51 +10,32 @@ menu_data = [
10
  # Cart data (persistent across sessions)
11
  cart_items = []
12
 
13
- # Extras
14
- extras = [
15
- {"name": "Coke 500ml", "price": "$2.00"},
16
- {"name": "Sprite 500ml", "price": "$2.00"},
17
- {"name": "Extra Raitha 4oz", "price": "$1.00"},
18
- {"name": "Extra Salan 8oz", "price": "$2.00"},
19
- ]
20
-
21
  # Function to handle adding items to the cart
22
- def add_to_cart(dish_name, selected_extras, quantity, custom_description):
23
- cart_items.append({
24
- "name": dish_name,
25
- "extras": selected_extras,
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):
33
- if category == "ALL":
34
- return menu_data
35
- return [dish for dish in menu_data if dish["category"] == category]
 
 
 
 
36
 
37
  # Function to display dishes
38
- def display_dishes(category):
39
- filtered_dishes = filter_dishes(category)
40
- html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%; height:100%; overflow-y: auto;'>"
41
- for dish in filtered_dishes:
42
  html_content += f"""
43
- <div style='margin: 10px; display: flex; align-items: center; justify-content: space-between;
44
- width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px;
45
- background-color: #f9f9f9;'>
46
- <div style='flex: 1; margin-right: 20px; text-align: left;'>
47
- <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4>
48
- <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p>
49
- <p style='font-size: 16px; color: #444; margin: 0;'>Price: {dish['price']}</p>
50
- </div>
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>
58
  </div>
59
  """
60
  html_content += "</div>"
@@ -62,31 +43,42 @@ def display_dishes(category):
62
 
63
  # Main Gradio App
64
  with gr.Blocks() as demo:
65
- gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
66
 
67
  with gr.Row():
68
- btn_all = gr.Button("ALL")
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
  }
@@ -95,17 +87,23 @@ with gr.Blocks() as demo:
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()
 
10
  # Cart data (persistent across sessions)
11
  cart_items = []
12
 
 
 
 
 
 
 
 
 
13
  # Function to handle adding items to the cart
14
+ def add_to_cart(dish_name):
15
+ cart_items.append(dish_name)
16
+ return update_cart()
 
 
 
 
 
17
 
18
+ # Function to update the cart display
19
+ def update_cart():
20
+ if not cart_items:
21
+ return "<p>Your cart is empty.</p>"
22
+ html_content = "<h3>Cart Items</h3><ul>"
23
+ for item in cart_items:
24
+ html_content += f"<li>{item}</li>"
25
+ html_content += "</ul><button onclick='alert(\'Order placed!\')' style='padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px;'>Place Order</button>"
26
+ return html_content
27
 
28
  # Function to display dishes
29
+ def display_dishes():
30
+ html_content = "<div style='display: flex; flex-wrap: wrap; gap: 10px;'>"
31
+ for dish in menu_data:
 
32
  html_content += f"""
33
+ <div style='border: 1px solid #ddd; padding: 10px; border-radius: 5px; width: 200px;'>
34
+ <img src='{dish['image']}' alt='{dish['name']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 5px;'>
35
+ <h4>{dish['name']}</h4>
36
+ <p>{dish['description']}</p>
37
+ <p><strong>Price:</strong> {dish['price']}</p>
38
+ <button onclick="showMenuPopup('{dish['name']}', '{dish['description']}', '{dish['price']}', '{dish['image']}')" style='padding: 5px 10px; background-color: #28a745; color: white; border: none; border-radius: 3px;'>Add to Cart</button>
 
 
 
 
 
 
 
 
 
39
  </div>
40
  """
41
  html_content += "</div>"
 
43
 
44
  # Main Gradio App
45
  with gr.Blocks() as demo:
46
+ gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
47
 
48
  with gr.Row():
49
+ gr.HTML("""
50
+ <div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
51
+ <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()'>
52
+ </div>
53
+ <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);'>
54
+ <div id='cart-content'>{update_cart()}</div>
55
+ <button onclick='toggleCartPopup()' style='margin-top: 10px; padding: 5px 10px; background-color: #007bff; color: white; border: none; border-radius: 3px;'>Close</button>
56
+ </div>
57
+ <script>
58
+ function toggleCartPopup() {
59
+ const popup = document.getElementById('cart-popup');
60
+ popup.style.display = popup.style.display === 'block' ? 'none' : 'block';
61
+ }
62
+ </script>
63
+ """)
64
 
65
  gr.HTML("""
66
+ <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);'>
67
+ <span onclick='closeMenuPopup()' style='position: absolute; top: 5px; right: 10px; cursor: pointer;'>&times;</span>
68
+ <img id='popup-image' src='' style='width: 100%; border-radius: 5px; margin-bottom: 10px;'>
69
+ <h4 id='popup-title'></h4>
70
+ <p id='popup-description'></p>
71
+ <p id='popup-price'></p>
72
+ <button onclick='addItemToCart()' style='padding: 5px 10px; background-color: #28a745; color: white; border: none; border-radius: 3px;'>Add to Cart</button>
 
 
73
  </div>
74
  <script>
75
+ let selectedItem = '';
76
+
77
  function showMenuPopup(name, description, price, image) {
78
+ selectedItem = name;
79
  document.getElementById('popup-title').textContent = name;
80
  document.getElementById('popup-description').textContent = description;
81
+ document.getElementById('popup-price').textContent = price;
82
  document.getElementById('popup-image').src = image;
83
  document.getElementById('menu-popup').style.display = 'block';
84
  }
 
87
  document.getElementById('menu-popup').style.display = 'none';
88
  }
89
 
90
+ function addItemToCart() {
 
 
91
  closeMenuPopup();
92
+ toggleCartPopup();
93
+ alert('Item added to cart!');
94
+ fetch('/run/predict', {
95
+ method: 'POST',
96
+ headers: { 'Content-Type': 'application/json' },
97
+ body: JSON.stringify({ data: [selectedItem] })
98
+ })
99
+ .then(response => response.json())
100
+ .then(data => {
101
+ document.getElementById('cart-content').innerHTML = data.data[0];
102
+ });
103
  }
104
  </script>
105
  """)
106
 
107
+ gr.HTML(value=display_dishes())
 
 
 
108
 
109
  demo.launch()