nagasurendra commited on
Commit
af61c2f
·
verified ·
1 Parent(s): 2146520

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -58
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
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
7
  try:
@@ -9,88 +9,98 @@ def load_menu():
9
  except Exception as e:
10
  raise ValueError(f"Error loading menu file: {e}")
11
 
12
- # Function to generate the popup content dynamically
13
- def generate_popup(item_name):
14
- menu_data = load_menu()
15
- item = menu_data[menu_data['Dish Name'] == item_name].iloc[0]
16
-
17
- # Dynamic HTML for the popup
18
- popup_content = f"""
19
- <div style="background-color: white; padding: 20px; border-radius: 8px; width: 50%; margin: auto; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1000;">
20
- <img src="{item['Image URL']}" alt="{item_name}" style="width: 100%; border-radius: 8px; margin-bottom: 20px;">
21
- <h2>{item_name}</h2>
22
- <p>{item['Description']}</p>
23
- <p style="font-size: 18px; color: #333;"><strong>Price:</strong> ${item['Price ($)']}</p>
24
- <h3>Special Instructions:</h3>
25
- <textarea placeholder="Add any requests here." style="width: 100%; height: 100px;"></textarea>
26
- <h3>Quantity:</h3>
27
- <input type="number" value="1" min="1" style="width: 50px;">
28
- <button style="background-color: purple; color: white; padding: 10px 20px; margin-top: 20px; border: none; border-radius: 5px; cursor: pointer;">Add to Bag</button>
29
- </div>
30
- """
31
- return popup_content
32
-
33
- # Function to filter menu items and include the popup functionality
34
- def filter_menu_with_popup(preference):
35
  menu_data = load_menu()
36
 
37
- # Define filter conditions
38
  if preference == "Halal/Non-Veg":
39
  filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
40
  elif preference == "Vegetarian":
41
  filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
42
  elif preference == "Guilt-Free":
43
  filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
44
- else: # Default to "All"
45
  filtered_data = menu_data
46
 
47
- # Generate HTML for the menu
48
- html_content = ""
49
  for _, item in filtered_data.iterrows():
50
- html_content += f"""
51
- <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);">
52
  <div style="flex: 1; margin-right: 15px;">
53
- <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
54
- <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
55
- <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
56
  </div>
57
- <div style="flex-shrink: 0; text-align: center;">
58
- <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
59
- <button onclick="showPopup('{item['Dish Name']}')" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
 
60
  </div>
61
  </div>
62
  """
63
- return html_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- # Gradio app definition
66
  def app():
67
- with gr.Blocks(title="Dynamic Menu with Filters") as demo:
68
- gr.Markdown("## Dynamic Menu with Preferences")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # Radio button for selecting preference
71
- selected_preference = gr.Radio(
72
- choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
73
- value="All",
74
- label="Choose a Preference",
75
- )
76
 
77
- # Output area for menu items
78
- menu_output = gr.HTML(value=filter_menu_with_popup("All"))
 
79
 
80
- # Popup area
81
- popup_output = gr.HTML()
82
 
83
- # Define interactivity
84
- selected_preference.change(filter_menu_with_popup, inputs=[selected_preference], outputs=[menu_output])
85
 
86
- # Layout
87
- gr.Row([selected_preference])
88
- gr.Row(menu_output)
89
- gr.Row(popup_output)
90
 
91
- return demo
92
 
93
- # Run the app
94
  if __name__ == "__main__":
95
  demo = app()
96
  demo.launch()
 
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
  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
+ menu_html = ""
 
26
  for _, item in filtered_data.iterrows():
27
+ menu_html += f"""
28
+ <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px;">
29
  <div style="flex: 1; margin-right: 15px;">
30
+ <h3 style="margin: 0;">{item['Dish Name']}</h3>
31
+ <p style="margin: 5px 0;">${item['Price ($)']}</p>
32
+ <p>{item['Description']}</p>
33
  </div>
34
+ <div>
35
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover;">
36
+ <button style="background-color: #28a745; color: white; padding: 8px 15px; border: none; cursor: pointer;"
37
+ onclick="return '{item['Dish Name']}'">View Details</button>
38
  </div>
39
  </div>
40
  """
41
+ return menu_html
42
+
43
+ # Dish details view
44
+ def render_dish_details(dish_name):
45
+ menu_data = load_menu()
46
+ dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
47
+ return {
48
+ "image": dish["Image URL"],
49
+ "name": dish_name,
50
+ "description": dish["Description"],
51
+ "price": dish["Price ($)"]
52
+ }
53
+
54
+ # Add to cart
55
+ def add_to_cart(dish_name, spice_level, extras, quantity, special_instructions, cart):
56
+ cart.append({
57
+ "name": dish_name,
58
+ "spice_level": spice_level,
59
+ "extras": extras,
60
+ "quantity": quantity,
61
+ "instructions": special_instructions
62
+ })
63
+ return cart, f"Added {dish_name} to cart!"
64
 
65
+ # Gradio app
66
  def app():
67
+ with gr.Blocks() as demo:
68
+ cart_state = gr.State([])
69
+
70
+ # Menu page
71
+ menu_html = gr.HTML(render_menu("All"))
72
+ detailed_view = gr.Column(visible=False)
73
+ cart_view = gr.Column(visible=False)
74
+
75
+ # Detailed view inputs
76
+ spice_level = gr.Dropdown(choices=["Mild", "Medium", "Spicy"], label="Spice Level")
77
+ extras = gr.CheckboxGroup(choices=["Extra Raita", "Extra Salan", "Extra Onion"], label="Extras")
78
+ quantity = gr.Number(value=1, label="Quantity")
79
+ special_instructions = gr.Textbox(placeholder="Add instructions", label="Special Instructions")
80
+ add_button = gr.Button("Add to Cart")
81
+ back_button = gr.Button("Back to Menu")
82
+
83
+ # Cart view
84
+ cart_html = gr.HTML(value="Your cart is empty.")
85
 
86
+ # Switch to detailed view
87
+ def show_dish_details(dish_name):
88
+ details = render_dish_details(dish_name)
89
+ return gr.update(visible=False), gr.update(visible=True), details["image"], details["name"], details["description"], details["price"]
 
 
90
 
91
+ # Add to cart
92
+ def handle_add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
93
+ return add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart)
94
 
95
+ # Navigation
96
+ menu_html.change(show_dish_details, inputs=["dish_name"], outputs=[menu_html, detailed_view])
97
 
98
+ add_button.click(handle_add_to_cart, inputs=[spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_html])
 
99
 
100
+ back_button.click(lambda: (gr.update(visible=True), gr.update(visible=False)), outputs=[menu_html, detailed_view])
 
 
 
101
 
102
+ return demo
103
 
 
104
  if __name__ == "__main__":
105
  demo = app()
106
  demo.launch()