Spaces:
Sleeping
Sleeping
File size: 7,732 Bytes
ba4dd53 9acada1 ba4dd53 92b51ea ba4dd53 92b51ea ba4dd53 2653a35 92b51ea 37e70db ba4dd53 0f06d66 2653a35 0f06d66 37e70db 0f06d66 37e70db a2a66c1 807b0c3 a2a66c1 807b0c3 37e70db 2653a35 a2a66c1 2653a35 a2a66c1 2653a35 54b5ff0 a2a66c1 2653a35 92b51ea 37e70db 3c97bed 0f06d66 118d5fd 92b51ea 0d58d8b 92b51ea 4b578a2 92b51ea 6b86d43 a2a66c1 dcf3813 a2a66c1 dcf3813 a2a66c1 dcf3813 a2a66c1 dcf3813 a2a66c1 dcf3813 a2a66c1 dcf3813 a2a66c1 dcf3813 4b578a2 dcf3813 92b51ea a2a66c1 3c97bed 2653a35 dcf3813 7980609 37e70db 7980609 45bc2f8 92b51ea 45bc2f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
import gradio as gr
import pandas as pd
# Function to load the menu data
def load_menu():
menu_file = "menu.xlsx" # Ensure this file exists in the same directory
try:
return pd.read_excel(menu_file)
except Exception as e:
raise ValueError(f"Error loading menu file: {e}")
# Initialize cart globally
cart_items = []
# Function to filter menu items based on preference
def filter_menu(preference):
menu_data = load_menu()
if preference == "Halal/Non-Veg":
filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
elif preference == "Vegetarian":
filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
elif preference == "Guilt-Free":
filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
else:
filtered_data = menu_data
html_content = ""
for _, item in filtered_data.iterrows():
html_content += f"""
<div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
<div style="flex: 1; margin-right: 15px;">
<h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
<p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
<p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
</div>
<div style="flex-shrink: 0; text-align: center;">
<img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
<button class="add-btn" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="openModal(event, '{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')">Add</button>
</div>
</div>
"""
return html_content
# Function to update the cart display
def update_cart(cart):
global cart_items
cart_items = cart # Update global cart items
if len(cart_items) == 0:
return "Your cart is empty."
cart_html = "<h3>Your Cart:</h3><ul>"
for item in cart_items:
extras = ", ".join(item.get("extras", []))
cart_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item.get('spiceLevel', 'None')}, Extras: {extras}) - {item['price']}</li>"
cart_html += "</ul>"
return cart_html
# Gradio app definition
def app():
with gr.Blocks() as demo:
gr.Markdown("## Dynamic Menu with Preferences")
# Radio button for selecting preference
selected_preference = gr.Radio(
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
value="All",
label="Choose a Preference",
)
# Output area for menu items
menu_output = gr.HTML(value=filter_menu("All"))
# Cart display
cart_output = gr.HTML(value="Your cart is empty.")
# Modal window
modal_window = gr.HTML("""
<div id="modal" style="
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* Centers the modal on the page */
background: white;
padding: 20px;
border-radius: 15px;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
font-family: Arial, sans-serif;
z-index: 1000;
min-width: 350px; /* Minimum width of the modal */
min-height: 300px; /* Minimum height of the modal */
overflow-y: auto; /* Allow scrolling inside the modal if content is too tall */
">
<div style="text-align: right;">
<button onclick="closeModal()" style="
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: #333;
">×</button>
</div>
<img id="modal-image" style="
width: 100%;
height: auto;
border-radius: 12px;
margin-bottom: 15px;
" />
<h2 id="modal-name" style="margin-bottom: 10px; font-size: 22px; color: #333;"></h2>
<p id="modal-description" style="margin-bottom: 10px; color: #555; font-size: 16px;"></p>
<p id="modal-price" style="margin-bottom: 20px; font-size: 18px; font-weight: bold; color: #222;"></p>
<div style="display: flex; justify-content: space-between; align-items: center;">
<label for="quantity" style="font-weight: bold;">Quantity:</label>
<input type="number" id="quantity" value="1" min="1" style="
width: 60px;
padding: 5px;
border: 1px solid #ccc;
border-radius: 5px;
" />
<button style="
background-color: #28a745;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 8px;
cursor: pointer;
" onclick="addToCart()">Add</button>
</div>
</div>
<script>
let cart = [];
function openModal(event, name, image, description, price) {
const modal = document.getElementById('modal');
modal.style.display = 'block';
// Get the clicked button's position
const rect = event.target.getBoundingClientRect();
// Set the popup position based on the clicked element
modal.style.top = `${rect.top + window.scrollY + rect.height + 10}px`; // 10px below the button
modal.style.left = `${rect.left + window.scrollX}px`;
document.getElementById('modal-image').src = image;
document.getElementById('modal-name').innerText = name;
document.getElementById('modal-description').innerText = description;
document.getElementById('modal-price').innerText = `$${price}`;
}
function closeModal() {
const modal = document.getElementById('modal');
modal.style.display = 'none';
}
function addToCart() {
const name = document.getElementById('modal-name').innerText;
const price = document.getElementById('modal-price').innerText;
const description = document.getElementById('modal-description').innerText;
const quantity = parseInt(document.getElementById('quantity').value) || 1;
const cartItem = { name, description, price, quantity };
cart.push(cartItem);
alert(`${name} added to cart!`);
closeModal();
}
</script>
""")
# Interactivity
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
# Add the cart output
gr.Row([selected_preference])
gr.Row(menu_output)
gr.Row(cart_output)
gr.Row(modal_window)
return demo
if __name__ == "__main__":
demo = app()
demo.launch()
|