Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| # Menu data | |
| menu_data = [ | |
| {"name": "Vegetable Biryani", "category": "VEGAN", | |
| "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg", | |
| "description": "An Indian dish made to be thoroughly enjoyed by anyone who eats it. Made with the best basmati rice and a perfect blend of spices, cooked in original dum style.", | |
| "price": "$13.99"}, | |
| ] | |
| # Spice levels and extras | |
| spice_levels = ["American Mild", "American Medium", "American Spicy", | |
| "Indian Mild", "Indian Medium", "Indian Spicy", "Indian Very Spicy"] | |
| extras = [ | |
| {"name": "Extra Raitha 4oz", "price": "$1.00"}, | |
| {"name": "Extra Raitha 8oz", "price": "$2.00"}, | |
| {"name": "Extra Salan 4oz", "price": "$1.00"}, | |
| {"name": "Extra Salan 8oz", "price": "$2.00"}, | |
| {"name": "Extra Onion", "price": "$1.00"}, | |
| {"name": "Extra Onion & Lemon", "price": "$2.00"}, | |
| {"name": "Extra Fried Onion 4oz", "price": "$2.00"}, | |
| ] | |
| # Filter dishes by category | |
| def filter_dishes(category): | |
| if category == "ALL": | |
| return menu_data | |
| return [dish for dish in menu_data if dish["category"] == category] | |
| # Generate dish cards with "Add" button | |
| def display_dishes(category): | |
| filtered_dishes = filter_dishes(category) | |
| html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%;'>" | |
| for dish in filtered_dishes: | |
| html_content += f""" | |
| <div style='margin: 10px; display: flex; align-items: center; justify-content: space-between; | |
| width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px; | |
| background-color: #f9f9f9;'> | |
| <div style='flex: 1; margin-right: 20px; text-align: left;'> | |
| <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4> | |
| <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p> | |
| <p style='font-size: 16px; color: #444; margin: 0;'>Price: {dish['price']}</p> | |
| </div> | |
| <div style='text-align: center;'> | |
| <img src='{dish['image']}' alt='{dish['name']}' | |
| style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'> | |
| <button onclick="showPopup('{dish['image']}', '{dish['name']}', '{dish['description']}', '{dish['price']}')" | |
| style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none; | |
| border-radius: 5px; cursor: pointer;">Add</button> | |
| </div> | |
| </div> | |
| """ | |
| html_content += "</div>" | |
| return html_content | |
| # Main Gradio App | |
| with gr.Blocks() as demo: | |
| gr.HTML("<h1 style='text-align: center; color: #333;'>π Biryani Hub Menu π</h1>") | |
| # Buttons for category selection | |
| with gr.Row(): | |
| btn_all = gr.Button("ALL") | |
| btn_vegan = gr.Button("VEGAN") | |
| btn_halal = gr.Button("HALAL") | |
| # Dish display | |
| dish_display = gr.HTML(value=display_dishes("ALL")) | |
| # JavaScript for managing the pop-up | |
| gr.HTML(f""" | |
| <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); | |
| background-color: white; padding: 20px; border-radius: 10px; | |
| box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;"> | |
| <img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;"> | |
| <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2> | |
| <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p> | |
| <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p> | |
| <h4 style="text-align: left; margin-top: 20px;">Choose a spice level</h4> | |
| <div id="spice-levels-container" style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;"> | |
| """ + "".join([f'<label style="margin-right: 10px; display: block;"><input type="radio" name="spice" value="{spice}" style="margin-right: 5px;">{spice}</label>' for spice in spice_levels]) + """ | |
| </div> | |
| <h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4> | |
| <div id="extras-container" style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;"> | |
| """ + "".join([f'<label style="margin-right: 10px; display: block;"><input type="checkbox" name="extras" value="{extra["name"]}" style="margin-right: 5px;">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """ | |
| </div> | |
| <button onclick="addToCart()" | |
| style="margin-top: 20px; padding: 10px 20px; background-color: #007bff; color: white; border: none; | |
| border-radius: 5px; cursor: pointer;">Add to Cart</button> | |
| <button onclick="closePopup()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; | |
| color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button> | |
| </div> | |
| <script> | |
| let selectedSpice = null; | |
| let selectedExtras = []; | |
| function showPopup(image, title, description, price) { | |
| document.getElementById('popup-image').src = image; | |
| document.getElementById('popup-title').textContent = title; | |
| document.getElementById('popup-description').textContent = description; | |
| document.getElementById('popup-price').textContent = "Price: " + price; | |
| document.getElementById('popup').style.display = 'block'; | |
| } | |
| function closePopup() { | |
| document.getElementById('popup').style.display = 'none'; | |
| } | |
| function addToCart() { | |
| const spice = document.querySelector('input[name=spice]:checked')?.value || "No spice selected"; | |
| const extras = Array.from(document.querySelectorAll('input[name=extras]:checked')).map(input => input.value); | |
| alert(Added with spice level: ${spice}, and extras: ${extras.join(", ")}); | |
| closePopup(); | |
| } | |
| </script> | |
| """) | |
| # Button click events | |
| btn_all.click( | |
| lambda: gr.update(value=display_dishes("ALL")), | |
| outputs=dish_display | |
| ) | |
| btn_vegan.click( | |
| lambda: gr.update(value=display_dishes("VEGAN")), | |
| outputs=dish_display | |
| ) | |
| btn_halal.click( | |
| lambda: gr.update(value=display_dishes("HALAL")), | |
| outputs=dish_display | |
| ) | |
| demo.launch() | |
| ChatGPT said: | |
| ChatGPT |