import gradio as gr
import pandas as pd
# Function to load the menu data from Excel
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}")
# Function to generate the popup content dynamically
def generate_popup(item_name):
menu_data = load_menu()
try:
item = menu_data[menu_data['Dish Name'] == item_name].iloc[0]
except IndexError:
return f"
Item '{item_name}' not found!
"
# Dynamic HTML for the popup
popup_content = f"""
{item_name}
{item['Description']}
Price: ${item['Price ($)']}
Choose a Spice Level:
Biryani Extras:
Special Instructions:
Quantity:
"""
return popup_content
# Function to filter menu items and include the popup functionality
def filter_menu_with_popup(preference):
menu_data = load_menu()
# Define filter conditions
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: # Default to "All"
filtered_data = menu_data
# Generate HTML for the menu
html_content = ""
for _, item in filtered_data.iterrows():
html_content += f"""
{item['Dish Name']}
${item['Price ($)']}
{item['Description']}
"""
return html_content
# Gradio app definition
def app():
with gr.Blocks(title="Dynamic Menu with Filters") 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_with_popup("All"))
# Popup area
popup_output = gr.HTML()
# Define interactivity
selected_preference.change(filter_menu_with_popup, inputs=[selected_preference], outputs=[menu_output])
# Layout
gr.Row([selected_preference])
gr.Row(menu_output)
gr.Row(popup_output)
return demo
# Run the app
if __name__ == "__main__":
demo = app()
demo.launch()