nagasurendra commited on
Commit
4b578a2
·
verified ·
1 Parent(s): 0d58d8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -8
app.py CHANGED
@@ -34,7 +34,7 @@ def filter_menu(preference):
34
  </div>
35
  <div style="flex-shrink: 0; text-align: center;">
36
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
37
- <button value="{item['Dish Name']}" onclick="selectDish('{item['Dish Name']}')" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
38
  </div>
39
  </div>
40
  """
@@ -42,13 +42,14 @@ def filter_menu(preference):
42
 
43
  # Function to render dish details for the suggestion page
44
  def render_dish_details(dish_name):
45
- print(f"Dish name received: {dish_name}") # Debugging line
46
  menu_data = load_menu()
47
  try:
48
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
 
49
  return (
50
  dish["Image URL"],
51
- dish_name,
52
  dish["Description"],
53
  f"${dish['Price ($)']}"
54
  )
@@ -58,11 +59,12 @@ def render_dish_details(dish_name):
58
  # Gradio app definition
59
  def app():
60
  with gr.Blocks() as demo:
61
- gr.Markdown("## Menu with Dynamic Filters")
62
 
63
- selected_dish = gr.State("") # State for the selected dish name
 
64
 
65
- # Menu components
66
  preference_selector = gr.Radio(
67
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
68
  value="All",
@@ -70,12 +72,28 @@ def app():
70
  )
71
  menu_html = gr.HTML(value=filter_menu("All"))
72
 
73
- # Suggestion page components
74
  suggestion_page = gr.Column(visible=False)
75
  dish_image = gr.Image(label="Dish Image")
76
  dish_name = gr.Markdown()
77
  dish_description = gr.Markdown()
78
  dish_price = gr.Markdown()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Functions
81
  def update_menu(preference):
@@ -88,7 +106,7 @@ def app():
88
  gr.update(visible=False),
89
  gr.update(visible=True),
90
  img,
91
- f"## {name}",
92
  desc,
93
  price
94
  )
@@ -102,6 +120,20 @@ def app():
102
  ""
103
  )
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  # Event Handlers
106
  preference_selector.change(
107
  update_menu,
@@ -115,6 +147,17 @@ def app():
115
  outputs=[menu_html, suggestion_page, dish_image, dish_name, dish_description, dish_price]
116
  )
117
 
 
 
 
 
 
 
 
 
 
 
 
118
  # Layout
119
  with gr.Row():
120
  menu_html
@@ -124,6 +167,15 @@ def app():
124
  dish_name
125
  dish_description
126
  dish_price
 
 
 
 
 
 
 
 
 
127
 
128
  return demo
129
 
 
34
  </div>
35
  <div style="flex-shrink: 0; text-align: center;">
36
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
37
+ <button value="{item['Dish Name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
38
  </div>
39
  </div>
40
  """
 
42
 
43
  # Function to render dish details for the suggestion page
44
  def render_dish_details(dish_name):
45
+ print(f"Dish name received: {dish_name}") # Debugging
46
  menu_data = load_menu()
47
  try:
48
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
49
+ print(f"Dish details: {dish}") # Debugging
50
  return (
51
  dish["Image URL"],
52
+ f"## {dish_name}",
53
  dish["Description"],
54
  f"${dish['Price ($)']}"
55
  )
 
59
  # Gradio app definition
60
  def app():
61
  with gr.Blocks() as demo:
62
+ gr.Markdown("## Dynamic Menu with Preferences")
63
 
64
+ # Track selected dish
65
+ selected_dish = gr.State("")
66
 
67
+ # Menu page
68
  preference_selector = gr.Radio(
69
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
70
  value="All",
 
72
  )
73
  menu_html = gr.HTML(value=filter_menu("All"))
74
 
75
+ # Suggestion page (hidden by default)
76
  suggestion_page = gr.Column(visible=False)
77
  dish_image = gr.Image(label="Dish Image")
78
  dish_name = gr.Markdown()
79
  dish_description = gr.Markdown()
80
  dish_price = gr.Markdown()
81
+ spice_level = gr.Radio(
82
+ choices=["Mild", "Medium", "Spicy"],
83
+ label="Choose Spice Level"
84
+ )
85
+ extras = gr.CheckboxGroup(
86
+ choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"],
87
+ label="Choose Extras"
88
+ )
89
+ quantity = gr.Number(value=1, label="Quantity", interactive=True)
90
+ special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...")
91
+ add_to_cart_button = gr.Button("Add to Bag")
92
+ back_to_menu_button = gr.Button("Back to Menu")
93
+
94
+ # Cart state and display
95
+ cart_state = gr.State([])
96
+ cart_output = gr.HTML(value="Your cart is empty.")
97
 
98
  # Functions
99
  def update_menu(preference):
 
106
  gr.update(visible=False),
107
  gr.update(visible=True),
108
  img,
109
+ name,
110
  desc,
111
  price
112
  )
 
120
  ""
121
  )
122
 
123
+ def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
124
+ cart.append({
125
+ "name": dish_name,
126
+ "spice_level": spice_level,
127
+ "extras": extras,
128
+ "quantity": quantity,
129
+ "instructions": instructions
130
+ })
131
+ cart_html = "<br>".join(
132
+ [f"{item['quantity']}x {item['name']} - {item['spice_level']} (Extras: {', '.join(item['extras'])})"
133
+ for item in cart]
134
+ )
135
+ return cart, cart_html
136
+
137
  # Event Handlers
138
  preference_selector.change(
139
  update_menu,
 
147
  outputs=[menu_html, suggestion_page, dish_image, dish_name, dish_description, dish_price]
148
  )
149
 
150
+ add_to_cart_button.click(
151
+ add_to_cart,
152
+ inputs=[dish_name, spice_level, extras, quantity, special_instructions, cart_state],
153
+ outputs=[cart_state, cart_output]
154
+ )
155
+
156
+ back_to_menu_button.click(
157
+ lambda: (gr.update(visible=True), gr.update(visible=False)),
158
+ outputs=[menu_html, suggestion_page]
159
+ )
160
+
161
  # Layout
162
  with gr.Row():
163
  menu_html
 
167
  dish_name
168
  dish_description
169
  dish_price
170
+ spice_level
171
+ extras
172
+ quantity
173
+ special_instructions
174
+ add_to_cart_button
175
+ back_to_menu_button
176
+
177
+ with gr.Row():
178
+ cart_output
179
 
180
  return demo
181