Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,7 +48,7 @@ def display_dishes(category):
|
|
| 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="
|
| 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>
|
|
@@ -81,6 +81,10 @@ 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 |
with gr.Tabs() as tabs:
|
| 85 |
with gr.Tab("Menu"):
|
| 86 |
with gr.Row():
|
|
@@ -92,65 +96,24 @@ with gr.Blocks() as demo:
|
|
| 92 |
with gr.Tab("Cart"):
|
| 93 |
cart_display = gr.HTML(value="<p>Your cart is empty.</p>", elem_id="cart-display")
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
gr.
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 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 |
-
const cartDisplay = document.getElementById('cart-display');
|
| 136 |
-
const xhr = new XMLHttpRequest();
|
| 137 |
-
xhr.open("POST", "", true);
|
| 138 |
-
xhr.setRequestHeader("Content-Type", "application/json");
|
| 139 |
-
xhr.onreadystatechange = function () {
|
| 140 |
-
if (xhr.readyState === 4 && xhr.status === 200) {
|
| 141 |
-
cartDisplay.innerHTML = xhr.responseText;
|
| 142 |
-
document.getElementById('popup').style.display = 'none';
|
| 143 |
-
tabs.set_active("Cart");
|
| 144 |
-
}
|
| 145 |
-
};
|
| 146 |
-
xhr.send(JSON.stringify({
|
| 147 |
-
dish_name: selectedDish,
|
| 148 |
-
spice_level: spiceLevel,
|
| 149 |
-
selected_extras: selectedExtras
|
| 150 |
-
}));
|
| 151 |
-
}
|
| 152 |
-
</script>
|
| 153 |
-
""")
|
| 154 |
|
| 155 |
# Button click events
|
| 156 |
btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
|
|
|
|
| 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="selectDish('{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>
|
|
|
|
| 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(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 |
with gr.Tab("Cart"):
|
| 97 |
cart_display = gr.HTML(value="<p>Your cart is empty.</p>", elem_id="cart-display")
|
| 98 |
|
| 99 |
+
# Interaction for Add to Cart
|
| 100 |
+
add_to_cart_button = gr.Button("Add to Cart", visible=False)
|
| 101 |
+
|
| 102 |
+
def show_dish_popup(dish_name):
|
| 103 |
+
return gr.update(visible=True), gr.update(value=dish_name, visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 104 |
+
|
| 105 |
+
def add_item_to_cart(dish_name, spice_level, extras):
|
| 106 |
+
return add_to_cart(dish_name, spice_level, extras)
|
| 107 |
+
|
| 108 |
+
dish_display.change(show_dish_popup, inputs=[selected_dish_name], outputs=[
|
| 109 |
+
selected_spice_level, selected_dish_name, selected_extras, add_to_cart_button
|
| 110 |
+
])
|
| 111 |
+
|
| 112 |
+
add_to_cart_button.click(
|
| 113 |
+
add_item_to_cart,
|
| 114 |
+
inputs=[selected_dish_name, selected_spice_level, selected_extras],
|
| 115 |
+
outputs=cart_display
|
| 116 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Button click events
|
| 119 |
btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
|