Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -93,43 +93,85 @@ extras = [
|
|
| 93 |
{"name": "Extra Fried Onion 4oz", "price": "$2.00"},
|
| 94 |
]
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def display_dishes(category):
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px;
|
| 110 |
-
background-color: #f9f9f9;'>
|
| 111 |
-
<div style='flex: 1; margin-right: 20px; text-align: left;'>
|
| 112 |
-
<h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4>
|
| 113 |
-
<p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p>
|
| 114 |
-
<p style='font-size: 16px; color: #444; margin: 0;'>Price: {dish['price']}</p>
|
| 115 |
-
</div>
|
| 116 |
-
<div style='text-align: center;'>
|
| 117 |
<img src='{dish['image']}' alt='{dish['name']}'
|
| 118 |
-
style='width:
|
| 119 |
-
<
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
</div>
|
| 123 |
-
|
| 124 |
-
"""
|
| 125 |
html_content += "</div>"
|
| 126 |
return html_content
|
| 127 |
|
| 128 |
# Main Gradio App
|
| 129 |
with gr.Blocks() as demo:
|
| 130 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
# Buttons for
|
| 133 |
with gr.Row():
|
| 134 |
btn_all = gr.Button("ALL")
|
| 135 |
btn_vegan = gr.Button("VEGAN")
|
|
@@ -138,52 +180,6 @@ with gr.Blocks() as demo:
|
|
| 138 |
# Dish display
|
| 139 |
dish_display = gr.HTML(value=display_dishes("ALL"))
|
| 140 |
|
| 141 |
-
# JavaScript for managing the pop-up
|
| 142 |
-
gr.HTML(f"""
|
| 143 |
-
<div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
|
| 144 |
-
background-color: white; padding: 20px; border-radius: 10px;
|
| 145 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
|
| 146 |
-
<img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;">
|
| 147 |
-
<h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
|
| 148 |
-
<p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
|
| 149 |
-
<p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
|
| 150 |
-
|
| 151 |
-
<h4 style="text-align: left; margin-top: 20px;">Choose a spice level</h4>
|
| 152 |
-
<div style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
|
| 153 |
-
""" + "".join([f'<label style="margin-right: 10px; display: block;"><input type="radio" name="spice" value="{spice}" style="margin-right: 5px;">{spice}</label>' for spice in spice_levels]) + """
|
| 154 |
-
</div>
|
| 155 |
-
|
| 156 |
-
<h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
|
| 157 |
-
<div style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
|
| 158 |
-
""" + "".join([f'<label style="margin-right: 10px; display: block;"><input type="checkbox" name="extras" value="{extra["name"]}" style="margin-right: 5px;">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
|
| 159 |
-
</div>
|
| 160 |
-
|
| 161 |
-
<button onclick="addToCart(document.getElementById('popup-title').textContent, document.querySelector('input[name=spice]:checked').value)"
|
| 162 |
-
style="margin-top: 20px; padding: 10px 20px; background-color: #007bff; color: white; border: none;
|
| 163 |
-
border-radius: 5px; cursor: pointer;">Add to Cart</button>
|
| 164 |
-
<button onclick="closePopup()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745;
|
| 165 |
-
color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button>
|
| 166 |
-
</div>
|
| 167 |
-
<script>
|
| 168 |
-
function showPopup(image, title, description, price) {
|
| 169 |
-
document.getElementById('popup-image').src = image;
|
| 170 |
-
document.getElementById('popup-title').textContent = title;
|
| 171 |
-
document.getElementById('popup-description').textContent = description;
|
| 172 |
-
document.getElementById('popup-price').textContent = "Price: " + price;
|
| 173 |
-
|
| 174 |
-
// Display the popup
|
| 175 |
-
document.getElementById('popup').style.display = 'block';
|
| 176 |
-
}
|
| 177 |
-
function closePopup() {
|
| 178 |
-
document.getElementById('popup').style.display = 'none';
|
| 179 |
-
}
|
| 180 |
-
function addToCart(dishName, spiceLevel) {
|
| 181 |
-
const selectedExtras = Array.from(document.querySelectorAll('input[name=extras]:checked')).map(cb => cb.value).join(", ");
|
| 182 |
-
alert(dishName + " with " + spiceLevel + " spice level and extras: " + selectedExtras + " has been added to the cart!");
|
| 183 |
-
}
|
| 184 |
-
</script>
|
| 185 |
-
""")
|
| 186 |
-
|
| 187 |
# Button click events
|
| 188 |
btn_all.click(
|
| 189 |
lambda: gr.update(value=display_dishes("ALL")),
|
|
|
|
| 93 |
{"name": "Extra Fried Onion 4oz", "price": "$2.00"},
|
| 94 |
]
|
| 95 |
|
| 96 |
+
popup_js = """
|
| 97 |
+
<script>
|
| 98 |
+
function showPopup(name, description, price) {
|
| 99 |
+
const popup = document.getElementById("custom-popup");
|
| 100 |
+
const overlay = document.getElementById("overlay");
|
| 101 |
+
popup.innerHTML = `
|
| 102 |
+
<h3 style="margin: 0;">${name}</h3>
|
| 103 |
+
<p style="margin: 5px 0;">${description}</p>
|
| 104 |
+
<p style="margin: 5px 0; font-weight: bold;">Price: ${price}</p>
|
| 105 |
+
<h4 style="margin: 20px 0 10px;">Choose a spice level</h4>
|
| 106 |
+
<div style="display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 10px;">
|
| 107 |
+
<label><input type="radio" name="spice" value="American Mild"> American Mild</label>
|
| 108 |
+
<label><input type="radio" name="spice" value="Indian Spicy"> Indian Spicy</label>
|
| 109 |
+
<label><input type="radio" name="spice" value="American Spicy"> American Spicy</label>
|
| 110 |
+
</div>
|
| 111 |
+
<h4 style="margin: 20px 0 10px;">Choose Extras</h4>
|
| 112 |
+
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
|
| 113 |
+
<label><input type="checkbox" name="extras" value="Extra Raitha"> Extra Raitha</label>
|
| 114 |
+
<label><input type="checkbox" name="extras" value="Extra Onion"> Extra Onion</label>
|
| 115 |
+
</div>
|
| 116 |
+
<button onclick="addToCart('${name}')" style="margin-top: 20px; padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;">Add to Cart</button>
|
| 117 |
+
<button onclick="closePopup()" style="margin-left: 10px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button>
|
| 118 |
+
`;
|
| 119 |
+
popup.style.display = "block";
|
| 120 |
+
overlay.style.display = "block";
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
function closePopup() {
|
| 124 |
+
const popup = document.getElementById("custom-popup");
|
| 125 |
+
const overlay = document.getElementById("overlay");
|
| 126 |
+
popup.style.display = "none";
|
| 127 |
+
overlay.style.display = "none";
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
function addToCart(dishName) {
|
| 131 |
+
alert(`${dishName} has been added to the cart!`);
|
| 132 |
+
closePopup();
|
| 133 |
+
}
|
| 134 |
+
</script>
|
| 135 |
+
"""
|
| 136 |
|
| 137 |
+
# HTML for popup modal
|
| 138 |
+
popup_html = """
|
| 139 |
+
<div id="overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; z-index: 999;">
|
| 140 |
+
</div>
|
| 141 |
+
<div id="custom-popup"
|
| 142 |
+
style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 50%; background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 6px rgba(0,0,0,0.1); display: none; z-index: 1000; text-align: center;">
|
| 143 |
+
</div>
|
| 144 |
+
"""
|
| 145 |
+
|
| 146 |
+
# Generate dish cards
|
| 147 |
def display_dishes(category):
|
| 148 |
+
html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: space-between;'>"
|
| 149 |
+
for dish in menu_data:
|
| 150 |
+
if category == "ALL" or dish["category"] == category:
|
| 151 |
+
html_content += f"""
|
| 152 |
+
<div style='margin: 10px; width: 30%; border: 1px solid #ddd; border-radius: 10px; padding: 10px;'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
<img src='{dish['image']}' alt='{dish['name']}'
|
| 154 |
+
style='width: 100%; height: auto; border-radius: 5px;'>
|
| 155 |
+
<h3>{dish['name']}</h3>
|
| 156 |
+
<p>{dish['description']}</p>
|
| 157 |
+
<p>Price: {dish['price']}</p>
|
| 158 |
+
<button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}')"
|
| 159 |
+
style='background-color: #007bff; color: white; border: none; padding: 5px 10px; cursor: pointer;'>
|
| 160 |
+
Add
|
| 161 |
+
</button>
|
| 162 |
</div>
|
| 163 |
+
"""
|
|
|
|
| 164 |
html_content += "</div>"
|
| 165 |
return html_content
|
| 166 |
|
| 167 |
# Main Gradio App
|
| 168 |
with gr.Blocks() as demo:
|
| 169 |
+
gr.HTML(popup_html) # Add the popup container and overlay
|
| 170 |
+
gr.HTML(popup_js) # Add the JavaScript
|
| 171 |
+
|
| 172 |
+
gr.HTML("<h1 style='text-align: center;'>Dynamic Restaurant Menu</h1>")
|
| 173 |
|
| 174 |
+
# Buttons for filtering categories
|
| 175 |
with gr.Row():
|
| 176 |
btn_all = gr.Button("ALL")
|
| 177 |
btn_vegan = gr.Button("VEGAN")
|
|
|
|
| 180 |
# Dish display
|
| 181 |
dish_display = gr.HTML(value=display_dishes("ALL"))
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
# Button click events
|
| 184 |
btn_all.click(
|
| 185 |
lambda: gr.update(value=display_dishes("ALL")),
|