Spaces:
Sleeping
Sleeping
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() | |