nagasurendra commited on
Commit
37e70db
·
verified ·
1 Parent(s): b73bac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -99
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import pandas as pd
3
 
4
- # Load menu data from Excel
5
  def load_menu():
6
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
7
  try:
@@ -9,63 +9,74 @@ def load_menu():
9
  except Exception as e:
10
  raise ValueError(f"Error loading menu file: {e}")
11
 
12
- # Main menu view
13
- def render_menu(preference):
 
14
  menu_data = load_menu()
15
 
 
16
  if preference == "Halal/Non-Veg":
17
  filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
18
  elif preference == "Vegetarian":
19
  filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
20
  elif preference == "Guilt-Free":
21
  filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
22
- else:
23
  filtered_data = menu_data
24
 
25
- # Generate menu as a list of tuples (dish_name, button_label)
26
- return [{"label": f"{row['Dish Name']} - ${row['Price ($)']}", "value": row['Dish Name']} for _, row in filtered_data.iterrows()]
27
-
28
- # Dish details view
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def render_dish_details(dish_name):
30
  menu_data = load_menu()
31
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
32
- return (
33
- dish["Image URL"],
34
- dish_name,
35
- dish["Description"],
36
- dish["Price ($)"]
37
- )
38
-
39
- # Add to cart function
40
- def add_to_cart(dish_name, spice_level, extras, quantity, special_instructions, cart):
41
- cart.append({
42
- "name": dish_name,
43
- "spice_level": spice_level,
44
- "extras": extras,
45
- "quantity": quantity,
46
- "instructions": special_instructions
47
- })
48
- return cart, f"Added {dish_name} to cart!"
49
-
50
- # Gradio app
51
- def app():
52
- with gr.Blocks() as demo:
53
- cart_state = gr.State([])
54
 
55
- # Menu page
56
- menu_dropdown = gr.Dropdown(
57
- label="Select a dish to view details",
58
- choices=[],
59
- interactive=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  )
61
- cart_view = gr.HTML(value="Your cart is empty.") # Fixed line
62
 
63
- # Dish details view
 
 
 
64
  detailed_view = gr.Column(visible=False)
65
- dish_image = gr.Image()
66
- dish_name = gr.Markdown()
67
- dish_description = gr.Markdown()
68
- dish_price = gr.Markdown()
69
  spice_level = gr.Radio(
70
  choices=["Mild", "Medium", "Spicy"],
71
  label="Choose Spice Level",
@@ -77,91 +88,79 @@ def app():
77
  quantity = gr.Number(label="Quantity", value=1, interactive=True)
78
  special_instructions = gr.Textbox(
79
  label="Special Instructions",
80
- placeholder="Add any special instructions here",
81
  )
82
- add_button = gr.Button("Add to Cart")
83
  back_button = gr.Button("Back to Menu")
84
 
85
- # Update menu with dish options
 
 
 
 
86
  def update_menu(preference):
87
- menu_choices = render_menu(preference)
88
- return gr.update(choices=[choice["label"] for choice in menu_choices])
89
 
90
- # Show dish details
91
  def show_dish_details(dish_name):
92
- img, name, desc, price = render_dish_details(dish_name)
93
- return (
94
- gr.update(visible=False),
95
- gr.update(visible=True),
96
- img,
97
- f"## {name}",
98
- desc,
99
- f"**Price:** ${price}",
100
- )
101
-
102
- # Add to cart
103
- def handle_add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
104
- updated_cart, message = add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart)
105
  cart_html = "<br>".join(
106
- [f"{item['quantity']}x {item['name']} - {item['spice_level']}" for item in updated_cart]
107
  )
108
- return updated_cart, cart_html
109
 
110
- # Switch back to menu
111
- def back_to_menu():
112
- return gr.update(visible=True), gr.update(visible=False)
 
 
 
113
 
114
- # Interactions
115
- menu_dropdown.change(
116
  show_dish_details,
117
- inputs=[menu_dropdown],
118
- outputs=[
119
- menu_dropdown,
120
- detailed_view,
121
- dish_image,
122
- dish_name,
123
- dish_description,
124
- dish_price,
125
- ],
126
  )
127
- add_button.click(
128
- handle_add_to_cart,
129
- inputs=[
130
- dish_name,
131
- spice_level,
132
- extras,
133
- quantity,
134
- special_instructions,
135
- cart_state,
136
- ],
137
- outputs=[cart_state, cart_view],
138
  )
 
139
  back_button.click(
140
- back_to_menu,
141
- outputs=[menu_dropdown, detailed_view],
142
  )
143
 
144
- # Layout for Gradio app
145
  with gr.Row():
146
- gr.Markdown("## Menu")
147
- menu_dropdown
148
- cart_view
149
 
150
  with detailed_view:
151
- dish_image
152
- dish_name
153
- dish_description
154
- dish_price
155
  spice_level
156
  extras
157
  quantity
158
  special_instructions
159
- add_button
160
  back_button
161
 
162
- return demo
 
163
 
 
164
 
 
165
  if __name__ == "__main__":
166
  demo = app()
167
  demo.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
 
4
+ # Function to load the menu data from Excel
5
  def load_menu():
6
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
7
  try:
 
9
  except Exception as e:
10
  raise ValueError(f"Error loading menu file: {e}")
11
 
12
+ # Function to filter menu items based on preference
13
+ def filter_menu(preference):
14
+ # Load menu data
15
  menu_data = load_menu()
16
 
17
+ # Define filter conditions
18
  if preference == "Halal/Non-Veg":
19
  filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
20
  elif preference == "Vegetarian":
21
  filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
22
  elif preference == "Guilt-Free":
23
  filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
24
+ else: # Default to "All"
25
  filtered_data = menu_data
26
 
27
+ # Generate HTML for the menu
28
+ html_content = ""
29
+ for _, item in filtered_data.iterrows():
30
+ html_content += f"""
31
+ <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);">
32
+ <div style="flex: 1; margin-right: 15px;">
33
+ <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
34
+ <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
35
+ <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
36
+ </div>
37
+ <div style="flex-shrink: 0; text-align: center;">
38
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
39
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;"
40
+ onclick="return '{item['Dish Name']}'">Add</button>
41
+ </div>
42
+ </div>
43
+ """
44
+ return html_content
45
+
46
+ # Function to render detailed view of a dish
47
  def render_dish_details(dish_name):
48
  menu_data = load_menu()
49
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ # Build detailed view HTML
52
+ details_html = f"""
53
+ <div style="text-align: left;">
54
+ <img src="{dish['Image URL']}" alt="{dish_name}" style="width: 100%; max-height: 300px; object-fit: cover; margin-bottom: 10px;">
55
+ <h2 style="margin: 10px 0;">{dish_name}</h2>
56
+ <p style="font-size: 16px; color: #555;">{dish['Description']}</p>
57
+ <h4>${dish['Price ($)']}</h4>
58
+ </div>
59
+ """
60
+ return details_html
61
+
62
+ # Gradio app definition
63
+ def app():
64
+ with gr.Blocks(title="Dynamic Menu with Filters") as demo:
65
+ gr.Markdown("## Dynamic Menu with Preferences")
66
+
67
+ # Radio button for selecting preference
68
+ selected_preference = gr.Radio(
69
+ choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
70
+ value="All",
71
+ label="Choose a Preference",
72
  )
 
73
 
74
+ # Output area for menu items
75
+ menu_output = gr.HTML(value=filter_menu("All"))
76
+
77
+ # Detailed view
78
  detailed_view = gr.Column(visible=False)
79
+ dish_details = gr.HTML()
 
 
 
80
  spice_level = gr.Radio(
81
  choices=["Mild", "Medium", "Spicy"],
82
  label="Choose Spice Level",
 
88
  quantity = gr.Number(label="Quantity", value=1, interactive=True)
89
  special_instructions = gr.Textbox(
90
  label="Special Instructions",
91
+ placeholder="Add any requests here",
92
  )
93
+ add_to_bag_button = gr.Button("Add to Bag")
94
  back_button = gr.Button("Back to Menu")
95
 
96
+ # Cart
97
+ cart_state = gr.State([])
98
+ cart_output = gr.HTML(value="Your cart is empty.")
99
+
100
+ # Define interactivity
101
  def update_menu(preference):
102
+ return filter_menu(preference)
 
103
 
 
104
  def show_dish_details(dish_name):
105
+ details_html = render_dish_details(dish_name)
106
+ return gr.update(visible=False), gr.update(visible=True), details_html
107
+
108
+ def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
109
+ cart.append({
110
+ "name": dish_name,
111
+ "spice_level": spice_level,
112
+ "extras": extras,
113
+ "quantity": quantity,
114
+ "instructions": instructions
115
+ })
 
 
116
  cart_html = "<br>".join(
117
+ [f"{item['quantity']}x {item['name']} - {item['spice_level']} (Extras: {', '.join(item['extras'])})" for item in cart]
118
  )
119
+ return cart, cart_html
120
 
121
+ # Event handlers
122
+ selected_preference.change(
123
+ update_menu,
124
+ inputs=[selected_preference],
125
+ outputs=[menu_output],
126
+ )
127
 
128
+ menu_output.change(
 
129
  show_dish_details,
130
+ inputs=[menu_output],
131
+ outputs=[menu_output, detailed_view, dish_details],
 
 
 
 
 
 
 
132
  )
133
+
134
+ add_to_bag_button.click(
135
+ add_to_cart,
136
+ inputs=[menu_output, spice_level, extras, quantity, special_instructions, cart_state],
137
+ outputs=[cart_state, cart_output],
 
 
 
 
 
 
138
  )
139
+
140
  back_button.click(
141
+ lambda: (gr.update(visible=True), gr.update(visible=False)),
142
+ outputs=[menu_output, detailed_view],
143
  )
144
 
145
+ # Layout
146
  with gr.Row():
147
+ menu_output
 
 
148
 
149
  with detailed_view:
150
+ dish_details
 
 
 
151
  spice_level
152
  extras
153
  quantity
154
  special_instructions
155
+ add_to_bag_button
156
  back_button
157
 
158
+ with gr.Row():
159
+ cart_output
160
 
161
+ return demo
162
 
163
+ # Run the app
164
  if __name__ == "__main__":
165
  demo = app()
166
  demo.launch()