Rammohan0504 commited on
Commit
da74b34
·
verified ·
1 Parent(s): 4affe87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -34
app.py CHANGED
@@ -81,10 +81,6 @@ def update_cart():
81
  with gr.Blocks() as demo:
82
  gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
83
 
84
- selected_dish_name = gr.Textbox(label="Dish Name", visible=False)
85
- selected_spice_level = gr.Dropdown(spice_levels, label="Choose Spice Level", visible=False)
86
- selected_extras = gr.CheckboxGroup([extra['name'] for extra in extras], label="Choose Extras", visible=False)
87
-
88
  with gr.Tabs() as tabs:
89
  with gr.Tab("Menu"):
90
  with gr.Row():
@@ -96,38 +92,60 @@ with gr.Blocks() as demo:
96
  with gr.Tab("Cart"):
97
  cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
98
 
99
- # Add to Cart Button
100
- add_to_cart_button = gr.Button("Add to Cart", visible=False)
101
-
102
- # Function to update popup visibility
103
- def show_popup(dish_name):
104
- return (gr.update(value=dish_name, visible=True),
105
- gr.update(visible=True),
106
- gr.update(visible=True),
107
- gr.update(visible=True))
108
-
109
- # Link Add to Cart button to Python backend
110
- def handle_add_to_cart(dish_name, spice_level, extras):
111
- return add_to_cart(dish_name, spice_level, extras)
112
-
113
- # Event binding
114
- add_to_cart_button.click(
115
- handle_add_to_cart,
116
- inputs=[selected_dish_name, selected_spice_level, selected_extras],
117
- outputs=cart_display
118
- )
119
-
120
- # Trigger popup display
121
- dish_display.change(
122
- show_popup,
123
- inputs=[selected_dish_name],
124
- outputs=[selected_dish_name, selected_spice_level, selected_extras, add_to_cart_button]
125
- )
126
-
127
- # Button click events for categories
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
129
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
130
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
131
 
132
  demo.launch()
133
-
 
81
  with gr.Blocks() as demo:
82
  gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
83
 
 
 
 
 
84
  with gr.Tabs() as tabs:
85
  with gr.Tab("Menu"):
86
  with gr.Row():
 
92
  with gr.Tab("Cart"):
93
  cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
94
 
95
+ # JavaScript for managing the pop-up
96
+ gr.HTML(f"""
97
+ <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
98
+ background-color: white; padding: 20px; border-radius: 10px;
99
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
100
+ <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
101
+ <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
102
+ <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
103
+ <h4 style="text-align: left; margin-top: 20px;">Choose a spice level</h4>
104
+ <div style="display: flex; flex-wrap: wrap; justify-content: space-between;">
105
+ """ + "".join([f'<label><input type="radio" name="spice" value="{spice}">{spice}</label>' for spice in spice_levels]) + """
106
+ </div>
107
+ <h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
108
+ <div style="display: flex; flex-wrap: wrap;">
109
+ """ + "".join([f'<label><input type="checkbox" name="extras" value="{extra["name"]}">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
110
+ </div>
111
+ <button onclick="addItemToCart()" style="margin-top: 20px;">Add to Cart</button>
112
+ <button onclick="closePopup()">Close</button>
113
+ </div>
114
+ <script>
115
+ let selectedDish = "";
116
+ let selectedExtras = [];
117
+ let spiceLevel = "";
118
+
119
+ function showPopup(title, description, price) {
120
+ selectedDish = title;
121
+ document.getElementById('popup-title').textContent = title;
122
+ document.getElementById('popup-description').textContent = description;
123
+ document.getElementById('popup-price').textContent = price;
124
+ document.getElementById('popup').style.display = 'block';
125
+ }
126
+ function closePopup() {
127
+ document.getElementById('popup').style.display = 'none';
128
+ }
129
+ function addItemToCart() {
130
+ const spiceInput = document.querySelector('input[name="spice"]:checked');
131
+ spiceLevel = spiceInput ? spiceInput.value : "No Spice Selected";
132
+ const extrasInputs = document.querySelectorAll('input[name="extras"]:checked');
133
+ selectedExtras = Array.from(extrasInputs).map(input => input.value);
134
+
135
+ // Call Python function via Gradio
136
+ google.colab.kernel.invokeFunction("notebook.add_to_cart", [
137
+ selectedDish, spiceLevel, selectedExtras
138
+ ], {}).then(result => {
139
+ document.querySelector("#cart-display").innerHTML = result.data["text/html"];
140
+ closePopup();
141
+ });
142
+ }
143
+ </script>
144
+ """)
145
+
146
+ # Button click events
147
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
148
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
149
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
150
 
151
  demo.launch()