Spaces:
Build error
Build error
Update app.py
Browse files8 unmodified lines910111213141516171819202122232425262728293031323334353637383940414243441213144849151617181920212226 unmodified lines49505152535455565758596061626364656667686970717273747576777879808182838485867 unmodified lines949596959798991001011021011021031041051068 unmodified lines except Exception as e: raise ValueError(f"Error loading menu file: {e}")
# Function to filter menu items based on preferencedef filter_menu(preference): # Load menu data 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""" <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 onclick="showPopup('{item['Dish Name']}')" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button> </div> </div> """ return html_content
# Function to generate the popup content dynamicallydef generate_popup(item_name): menu_data = load_menu() item = menu_data[menu_data['Dish Name'] == item_name].iloc[0] try: item = menu_data[menu_data['Dish Name'] == item_name].iloc[0] except IndexError: return f"<p style='color: red;'>Item '{item_name}' not found!</p>"
# Dynamic HTML for the popup popup_content = f""" <div style="background-color: white; padding: 20px; border-radius: 8px; width: 80%; margin: auto; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1000;">26 unmodified lines """ return popup_content
# Function to filter menu items and include the popup functionalitydef 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""" <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); position: relative;"> <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 onclick="showPopup('{item['Dish Name']}')" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button> </div> </div> """ return html_content
# Gradio app definitiondef app(): with gr.Blocks(title="Dynamic Menu with Filters") as demo:7 unmodified lines )
# Output area for menu items menu_output = gr.HTML(value=filter_menu("All")) menu_output = gr.HTML(value=filter_menu_with_popup("All"))
# Popup area popup_output = gr.HTML()
# Define interactivity selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output]) menu_output.change(generate_popup, inputs=[], outputs=[popup_output]) selected_preference.change(filter_menu_with_popup, inputs=[selected_preference], outputs=[menu_output])
# Layout gr.Row([selected_preference])app.py+39-38