Spaces:
Build error
Build error
Update app.py
Browse files1234456721 unmodified lines2930313232333435363738373839404142434444454646474849501 unmodified line52535454555656575859601 unmodified line626364646565666768692 unmodified lines7273747475767777787980818182838485868687888990919293878889909192939495969712 unmodified lines110111112112113114114115115116117118119120121121122123124125import gradio as grimport pandas as pd
# Function to load the menu data from Excel# Load menu data from Exceldef load_menu(): try: return pd.read_excel("menu.xlsx") # Ensure menu.xlsx is in the same directory21 unmodified lines <h3>{item['Dish Name']}</h3> <p>{item['Description']}</p> <p><strong>${item['Price ($)']}</strong></p> <button value="{item['Dish Name']}" onclick="showModal('{item['Dish Name']}')">Add</button> <button onclick="showModal('{item['Dish Name']}')" class="add-button">Add</button> </div> """ return html_content
# Function to render detailed view of a dishdef render_dish_details(dish_name):# Function to get dish details for the modaldef get_dish_details(dish_name): menu_data = load_menu() try: dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0] return ( dish["Image URL"], dish_name, dish["Dish Name"], dish["Description"], f"${dish['Price ($)']}" dish["Price ($)"], dish["Ingredients"] ) except IndexError: raise ValueError(f"Dish '{dish_name}' not found!")1 unmodified line# Main Gradio appdef app(): with gr.Blocks(css="style.css") as demo: gr.Markdown("## Dynamic Menu with Modal Window") gr.Markdown("## Dynamic Menu with Modal Suggestion Page")
# Menu page # Menu section preference_selector = gr.Radio( choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"], value="All",1 unmodified line ) menu_output = gr.HTML(value=filter_menu("All"))
# Modal window (hidden by default) modal_window = gr.Column(visible=False, elem_classes=["popup"]) # Modal window (suggestion page, hidden by default) modal = gr.Column(visible=False, elem_classes=["popup"]) modal_image = gr.Image(label="Dish Image") modal_name = gr.Markdown() modal_description = gr.Markdown()2 unmodified lines extras = gr.CheckboxGroup(choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"], label="Choose Extras") quantity = gr.Number(value=1, label="Quantity", interactive=True) special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...") add_to_cart_button = gr.Button("Add to Bag") add_to_cart_button = gr.Button("Add to Cart") close_modal_button = gr.Button("Close")
# Cart # Cart section cart_state = gr.State([]) cart_output = gr.HTML(value="Your cart is empty.")
# Handlers # Event handlers def update_menu(preference): return filter_menu(preference)
def show_modal(dish_name): try: img, name, desc, price = render_dish_details(dish_name) return ( gr.update(visible=True), img, name, desc, price ) except ValueError as e: return gr.update(value=f"Error: {str(e)}") img, name, desc, price, ingredients = get_dish_details(dish_name) return ( gr.update(visible=True), img, f"### {name}", f"**Description:** {desc}\n\n**Ingredients:** {ingredients}", f"**Price:** ${price}" )
def close_modal(): return gr.update(visible=False)12 unmodified lines ) return cart, cart_html
# Events # Event wiring preference_selector.change(update_menu, inputs=[preference_selector], outputs=[menu_output]) menu_output.change(show_modal, inputs=[menu_output], outputs=[modal_window, modal_image, modal_name, modal_description, modal_price]) close_modal_button.click(close_modal, outputs=[modal_window]) menu_output.change(show_modal, inputs=[menu_output], outputs=[modal, modal_image, modal_name, modal_description, modal_price]) close_modal_button.click(close_modal, outputs=[modal]) add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])
# Layout with gr.Row(): menu_output with modal_window: with modal: modal_image modal_name modal_descriptionapp.py+26-25