nagasurendra commited on
Commit
d9fe1ff
·
verified ·
1 Parent(s): c6f513e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -1,7 +1,7 @@
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
  try:
7
  return pd.read_excel("menu.xlsx") # Ensure menu.xlsx is in the same directory
@@ -29,21 +29,22 @@ def filter_menu(preference):
29
  <h3>{item['Dish Name']}</h3>
30
  <p>{item['Description']}</p>
31
  <p><strong>${item['Price ($)']}</strong></p>
32
- <button value="{item['Dish Name']}" onclick="showModal('{item['Dish Name']}')">Add</button>
33
  </div>
34
  """
35
  return html_content
36
 
37
- # Function to render detailed view of a dish
38
- def render_dish_details(dish_name):
39
  menu_data = load_menu()
40
  try:
41
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
42
  return (
43
  dish["Image URL"],
44
- dish_name,
45
  dish["Description"],
46
- f"${dish['Price ($)']}"
 
47
  )
48
  except IndexError:
49
  raise ValueError(f"Dish '{dish_name}' not found!")
@@ -51,9 +52,9 @@ def render_dish_details(dish_name):
51
  # Main Gradio app
52
  def app():
53
  with gr.Blocks(css="style.css") as demo:
54
- gr.Markdown("## Dynamic Menu with Modal Window")
55
 
56
- # Menu page
57
  preference_selector = gr.Radio(
58
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
59
  value="All",
@@ -61,8 +62,8 @@ def app():
61
  )
62
  menu_output = gr.HTML(value=filter_menu("All"))
63
 
64
- # Modal window (hidden by default)
65
- modal_window = gr.Column(visible=False, elem_classes=["popup"])
66
  modal_image = gr.Image(label="Dish Image")
67
  modal_name = gr.Markdown()
68
  modal_description = gr.Markdown()
@@ -71,26 +72,26 @@ def app():
71
  extras = gr.CheckboxGroup(choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"], label="Choose Extras")
72
  quantity = gr.Number(value=1, label="Quantity", interactive=True)
73
  special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...")
74
- add_to_cart_button = gr.Button("Add to Bag")
75
  close_modal_button = gr.Button("Close")
76
 
77
- # Cart
78
  cart_state = gr.State([])
79
  cart_output = gr.HTML(value="Your cart is empty.")
80
 
81
- # Handlers
82
  def update_menu(preference):
83
  return filter_menu(preference)
84
 
85
  def show_modal(dish_name):
86
- try:
87
- img, name, desc, price = render_dish_details(dish_name)
88
- return (
89
- gr.update(visible=True),
90
- img, name, desc, price
91
- )
92
- except ValueError as e:
93
- return gr.update(value=f"Error: {str(e)}")
94
 
95
  def close_modal():
96
  return gr.update(visible=False)
@@ -109,16 +110,16 @@ def app():
109
  )
110
  return cart, cart_html
111
 
112
- # Events
113
  preference_selector.change(update_menu, inputs=[preference_selector], outputs=[menu_output])
114
- menu_output.change(show_modal, inputs=[menu_output], outputs=[modal_window, modal_image, modal_name, modal_description, modal_price])
115
- close_modal_button.click(close_modal, outputs=[modal_window])
116
  add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])
117
 
118
  # Layout
119
  with gr.Row():
120
  menu_output
121
- with modal_window:
122
  modal_image
123
  modal_name
124
  modal_description
 
1
  import gradio as gr
2
  import pandas as pd
3
 
4
+ # Load menu data from Excel
5
  def load_menu():
6
  try:
7
  return pd.read_excel("menu.xlsx") # Ensure menu.xlsx is in the same directory
 
29
  <h3>{item['Dish Name']}</h3>
30
  <p>{item['Description']}</p>
31
  <p><strong>${item['Price ($)']}</strong></p>
32
+ <button onclick="showModal('{item['Dish Name']}')" class="add-button">Add</button>
33
  </div>
34
  """
35
  return html_content
36
 
37
+ # Function to get dish details for the modal
38
+ def get_dish_details(dish_name):
39
  menu_data = load_menu()
40
  try:
41
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
42
  return (
43
  dish["Image URL"],
44
+ dish["Dish Name"],
45
  dish["Description"],
46
+ dish["Price ($)"],
47
+ dish["Ingredients"]
48
  )
49
  except IndexError:
50
  raise ValueError(f"Dish '{dish_name}' not found!")
 
52
  # Main Gradio app
53
  def app():
54
  with gr.Blocks(css="style.css") as demo:
55
+ gr.Markdown("## Dynamic Menu with Modal Suggestion Page")
56
 
57
+ # Menu section
58
  preference_selector = gr.Radio(
59
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
60
  value="All",
 
62
  )
63
  menu_output = gr.HTML(value=filter_menu("All"))
64
 
65
+ # Modal window (suggestion page, hidden by default)
66
+ modal = gr.Column(visible=False, elem_classes=["popup"])
67
  modal_image = gr.Image(label="Dish Image")
68
  modal_name = gr.Markdown()
69
  modal_description = gr.Markdown()
 
72
  extras = gr.CheckboxGroup(choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"], label="Choose Extras")
73
  quantity = gr.Number(value=1, label="Quantity", interactive=True)
74
  special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...")
75
+ add_to_cart_button = gr.Button("Add to Cart")
76
  close_modal_button = gr.Button("Close")
77
 
78
+ # Cart section
79
  cart_state = gr.State([])
80
  cart_output = gr.HTML(value="Your cart is empty.")
81
 
82
+ # Event handlers
83
  def update_menu(preference):
84
  return filter_menu(preference)
85
 
86
  def show_modal(dish_name):
87
+ img, name, desc, price, ingredients = get_dish_details(dish_name)
88
+ return (
89
+ gr.update(visible=True),
90
+ img,
91
+ f"### {name}",
92
+ f"**Description:** {desc}\n\n**Ingredients:** {ingredients}",
93
+ f"**Price:** ${price}"
94
+ )
95
 
96
  def close_modal():
97
  return gr.update(visible=False)
 
110
  )
111
  return cart, cart_html
112
 
113
+ # Event wiring
114
  preference_selector.change(update_menu, inputs=[preference_selector], outputs=[menu_output])
115
+ menu_output.change(show_modal, inputs=[menu_output], outputs=[modal, modal_image, modal_name, modal_description, modal_price])
116
+ close_modal_button.click(close_modal, outputs=[modal])
117
  add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])
118
 
119
  # Layout
120
  with gr.Row():
121
  menu_output
122
+ with modal:
123
  modal_image
124
  modal_name
125
  modal_description