Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -83,14 +83,6 @@ def update_cart():
|
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
|
| 85 |
|
| 86 |
-
dish_name = gr.Textbox(visible=False)
|
| 87 |
-
spice_level = gr.Textbox(visible=False)
|
| 88 |
-
selected_extras = gr.Textbox(visible=False)
|
| 89 |
-
quantity = gr.Number(visible=False)
|
| 90 |
-
custom_description = gr.Textbox(visible=False)
|
| 91 |
-
|
| 92 |
-
cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
|
| 93 |
-
|
| 94 |
with gr.Tabs() as tabs:
|
| 95 |
with gr.Tab("Menu"):
|
| 96 |
with gr.Row():
|
|
@@ -100,7 +92,7 @@ with gr.Blocks() as demo:
|
|
| 100 |
dish_display = gr.HTML(value=display_dishes("ALL"))
|
| 101 |
|
| 102 |
with gr.Tab("Cart"):
|
| 103 |
-
cart_display
|
| 104 |
|
| 105 |
# JavaScript for managing the pop-up
|
| 106 |
gr.HTML(f"""
|
|
@@ -162,15 +154,23 @@ with gr.Blocks() as demo:
|
|
| 162 |
const quantity = document.getElementById('quantity').value || 1;
|
| 163 |
const customDescription = document.getElementById('custom-description').value || "None";
|
| 164 |
|
| 165 |
-
//
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
}
|
| 175 |
</script>
|
| 176 |
""")
|
|
@@ -180,11 +180,11 @@ with gr.Blocks() as demo:
|
|
| 180 |
btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
|
| 181 |
btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
|
| 182 |
|
| 183 |
-
#
|
| 184 |
-
|
| 185 |
-
add_to_cart,
|
| 186 |
-
inputs=[
|
| 187 |
-
outputs=
|
| 188 |
)
|
| 189 |
|
| 190 |
demo.launch()
|
|
|
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
with gr.Tabs() as tabs:
|
| 87 |
with gr.Tab("Menu"):
|
| 88 |
with gr.Row():
|
|
|
|
| 92 |
dish_display = gr.HTML(value=display_dishes("ALL"))
|
| 93 |
|
| 94 |
with gr.Tab("Cart"):
|
| 95 |
+
cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
|
| 96 |
|
| 97 |
# JavaScript for managing the pop-up
|
| 98 |
gr.HTML(f"""
|
|
|
|
| 154 |
const quantity = document.getElementById('quantity').value || 1;
|
| 155 |
const customDescription = document.getElementById('custom-description').value || "None";
|
| 156 |
|
| 157 |
+
// Pass values to backend
|
| 158 |
+
fetch("/flag", {
|
| 159 |
+
method: "POST",
|
| 160 |
+
headers: {"Content-Type": "application/json"},
|
| 161 |
+
body: JSON.stringify({
|
| 162 |
+
dish_name: selectedDish,
|
| 163 |
+
spice_level: spiceLevel,
|
| 164 |
+
selected_extras: selectedExtras,
|
| 165 |
+
quantity: quantity,
|
| 166 |
+
custom_description: customDescription
|
| 167 |
+
})
|
| 168 |
+
})
|
| 169 |
+
.then(response => response.json())
|
| 170 |
+
.then(data => {
|
| 171 |
+
document.querySelector("#cart-display").innerHTML = data.cart_html;
|
| 172 |
+
closePopup();
|
| 173 |
+
});
|
| 174 |
}
|
| 175 |
</script>
|
| 176 |
""")
|
|
|
|
| 180 |
btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
|
| 181 |
btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
|
| 182 |
|
| 183 |
+
# Handle cart updates via Gradio backend
|
| 184 |
+
cart_handler = gr.Interface(
|
| 185 |
+
fn=add_to_cart,
|
| 186 |
+
inputs=["text", "text", "text", "number", "text"],
|
| 187 |
+
outputs="html"
|
| 188 |
)
|
| 189 |
|
| 190 |
demo.launch()
|