File size: 3,976 Bytes
a691ebc
 
 
8edd0c3
 
 
f3efaf1
8edd0c3
 
f3efaf1
8edd0c3
 
f3efaf1
8edd0c3
a691ebc
 
 
 
f3efaf1
a691ebc
9b46be7
 
 
 
f3efaf1
9b46be7
f3efaf1
 
 
 
9b46be7
f3efaf1
9b46be7
 
 
a691ebc
f3efaf1
a691ebc
f3efaf1
a691ebc
f3efaf1
 
 
 
 
8edd0c3
f3efaf1
 
 
 
 
 
 
a691ebc
 
 
f3efaf1
a691ebc
 
 
9b46be7
f3efaf1
9b46be7
 
f3efaf1
 
 
 
a691ebc
f3efaf1
bbab687
 
 
 
f3efaf1
bbab687
a691ebc
 
 
bbab687
f3efaf1
bbab687
f3efaf1
a691ebc
bbab687
a691ebc
3e88fb2
f3efaf1
a691ebc
f3efaf1
 
 
 
 
 
 
 
 
bbab687
f3efaf1
 
 
 
 
 
a691ebc
 
f3efaf1
3e88fb2
f3efaf1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import gradio as gr
import pandas as pd

# Load menu from Excel file
def load_menu(file_path="menu.xlsx"):
    """
    Load menu data from an Excel file.
    """
    menu_df = pd.read_excel(file_path)
    menu = menu_df.to_dict(orient="records")
    return menu

# Initialize menu and cart
menu_data = load_menu()
cart = []

def display_menu():
    """
    Function to render the menu as HTML.
    """
    menu_html = ""
    for item in menu_data:
        menu_html += f"""
        <div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
            <img src="{item['Image']}" alt="{item['Name']}" style="width: 100px; height: 100px; margin-right: 15px; border-radius: 5px;">
            <div style="flex-grow: 1;">
                <h4 style="margin: 0;">{item['Name']} - ${item['Price']}</h4>
                <p style="margin: 5px 0; font-size: 12px;">{item['Description']}</p>
                <label for="quantity-{item['Name']}" style="margin-right: 10px;">Quantity:</label>
                <input type="number" id="quantity-{item['Name']}" value="1" min="1" style="width: 50px;">
            </div>
            <button onclick="add_to_cart('{item['Name']}', document.getElementById('quantity-{item['Name']}').value)" style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer;">Add</button>
        </div>
        """
    return menu_html

def add_to_cart(item_name, quantity):
    """
    Add an item to the cart and return success message with cart count.
    """
    quantity = int(quantity)
    # Check if the item already exists in the cart
    for item in cart:
        if item["name"] == item_name:
            item["quantity"] += quantity
            break
    else:
        # Add new item to cart
        for item in menu_data:
            if item["Name"] == item_name:
                cart.append({"name": item_name, "price": item["Price"], "quantity": quantity})
                break
    return f"{item_name} added to cart! Total items: {len(cart)}"

def view_cart():
    """
    Render the cart contents and calculate the total price.
    """
    if not cart:
        return "Your cart is empty."
    
    cart_html = "<h3>Cart Summary:</h3><ul>"
    total_price = 0
    for item in cart:
        cart_html += f"<li>{item['name']} (x{item['quantity']}) - ${item['price'] * item['quantity']}</li>"
        total_price += item["price"] * item["quantity"]
    cart_html += f"</ul><h4>Total Price: ${total_price}</h4>"
    return cart_html

# Gradio Interface
def menu_page():
    return gr.update(value=display_menu(), visible=True), gr.update(visible=False)

def cart_page():
    return gr.update(value=view_cart(), visible=True), gr.update(visible=False)

with gr.Blocks() as demo:
    gr.Markdown("# Dynamic Food Menu")
    
    # Menu and Cart Views
    menu_display = gr.HTML(value=display_menu(), visible=True)
    cart_display = gr.HTML(visible=False)
    notification = gr.Textbox(label="Notification", value="", interactive=False)
    
    # Navigation Buttons
    with gr.Row():
        menu_button = gr.Button("Menu")
        cart_button = gr.Button("View Cart")
    
    # Add item interaction
    food_item_input = gr.Textbox(visible=False)  # Hidden input to capture item name
    quantity_input = gr.Number(visible=False)   # Hidden input to capture quantity
    add_button = gr.Button("Add to Cart", visible=False)  # Simulated button for functionality

    # Notification when item is added
    add_button.click(add_to_cart, inputs=[food_item_input, quantity_input], outputs=notification)
    
    # Navigation between Menu and Cart pages
    menu_button.click(menu_page, outputs=[menu_display, cart_display])
    cart_button.click(cart_page, outputs=[cart_display, menu_display])
    
    # Render menu and cart
    demo += menu_display
    demo += cart_display
    demo += notification

demo.launch()