nagasurendra commited on
Commit
0f06d66
·
verified ·
1 Parent(s): 10e9c4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -87
app.py CHANGED
@@ -8,118 +8,90 @@ def load_menu():
8
  except Exception as e:
9
  raise ValueError(f"Error loading menu file: {e}")
10
 
11
- # Generate the menu content
12
  def filter_menu(preference):
13
  menu_data = load_menu()
14
- # Filter logic remains the same
15
- # Generating menu HTML
 
 
 
 
 
 
 
 
16
  html_content = ""
17
- for _, item in menu_data.iterrows():
18
  html_content += f"""
19
- <div class="menu-item">
20
- <div class="menu-details">
21
  <h3>{item['Dish Name']}</h3>
22
  <p>{item['Description']}</p>
23
  <p><strong>${item['Price ($)']}</strong></p>
24
  </div>
25
- <div class="menu-actions">
26
- <img src="{item['Image URL']}" alt="{item['Dish Name']}" class="menu-img">
27
- <button class="add-button" onclick="showModal('{item['Dish Name']}')">Add</button>
28
  </div>
29
  </div>
30
  """
31
  return html_content
32
 
33
- # Get dish details for the modal
34
- def get_dish_details(dish_name):
35
- menu_data = load_menu()
36
- try:
37
- dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
38
- return (
39
- dish["Image URL"],
40
- dish["Dish Name"],
41
- dish["Description"],
42
- f"${dish['Price ($)']}"
43
- )
44
- except IndexError:
45
- raise ValueError(f"Dish '{dish_name}' not found!")
46
-
47
- # Gradio App
48
  def app():
49
  with gr.Blocks(css="style.css") as demo:
50
- # Menu Filtering Section
 
51
  preference_selector = gr.Radio(
52
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
53
  value="All",
54
- label="Filter Menu"
55
  )
56
  menu_output = gr.HTML(value=filter_menu("All"))
57
 
58
- # Modal Window (initially hidden)
59
- modal = gr.Column(visible=False, elem_classes=["modal"])
60
- modal_image = gr.Image(label="Dish Image")
61
- modal_name = gr.Markdown()
62
- modal_description = gr.Markdown()
63
- modal_price = gr.Markdown()
64
- spice_level = gr.Radio(choices=["Mild", "Medium", "Spicy"], label="Choose Spice Level")
65
- extras = gr.CheckboxGroup(choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"], label="Choose Extras")
66
- quantity = gr.Number(value=1, label="Quantity", interactive=True)
67
- special_instructions = gr.Textbox(label="Special Instructions", placeholder="Add any requests...")
68
- add_to_cart_button = gr.Button("Add to Cart")
69
- close_modal_button = gr.Button("Close")
70
-
71
- # Cart Section
72
- cart_state = gr.State([])
73
- cart_output = gr.HTML(value="Your cart is empty.")
74
-
75
- # Handlers
76
- def show_modal(dish_name):
77
- img, name, desc, price = get_dish_details(dish_name)
78
- return (
79
- gr.update(visible=True),
80
- img,
81
- f"### {name}",
82
- desc,
83
- price
84
- )
85
-
86
- def close_modal():
87
- return gr.update(visible=False)
88
-
89
- def add_to_cart(name, spice, extras, qty, instructions, cart):
90
- cart.append({
91
- "name": name,
92
- "spice_level": spice,
93
- "extras": extras,
94
- "quantity": qty,
95
- "instructions": instructions
96
- })
97
- cart_html = "<br>".join(
98
- [f"{item['quantity']}x {item['name']} ({item['spice_level']})" for item in cart]
99
- )
100
- return cart, cart_html
101
-
102
- # Events
103
- preference_selector.change(filter_menu, inputs=[preference_selector], outputs=[menu_output])
104
- close_modal_button.click(close_modal, outputs=[modal])
105
- add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])
106
 
107
- # Layout
 
 
108
  with gr.Row():
109
  menu_output
110
- with modal:
111
- modal_image
112
- modal_name
113
- modal_description
114
- modal_price
115
- spice_level
116
- extras
117
- quantity
118
- special_instructions
119
- add_to_cart_button
120
- close_modal_button
121
  with gr.Row():
122
- cart_output
123
 
124
  return demo
125
 
 
8
  except Exception as e:
9
  raise ValueError(f"Error loading menu file: {e}")
10
 
11
+ # Function to filter and display menu items
12
  def filter_menu(preference):
13
  menu_data = load_menu()
14
+ if preference == "Halal/Non-Veg":
15
+ filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
16
+ elif preference == "Vegetarian":
17
+ filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
18
+ elif preference == "Guilt-Free":
19
+ filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
20
+ else: # Default to "All"
21
+ filtered_data = menu_data
22
+
23
+ # Generate HTML for the menu
24
  html_content = ""
25
+ for _, item in filtered_data.iterrows():
26
  html_content += f"""
27
+ <div class="menu-card">
28
+ <div class="menu-card-content">
29
  <h3>{item['Dish Name']}</h3>
30
  <p>{item['Description']}</p>
31
  <p><strong>${item['Price ($)']}</strong></p>
32
  </div>
33
+ <div class="menu-card-actions">
34
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" class="menu-card-image">
35
+ <button class="add-button" onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')">Add</button>
36
  </div>
37
  </div>
38
  """
39
  return html_content
40
 
41
+ # Main Gradio app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def app():
43
  with gr.Blocks(css="style.css") as demo:
44
+ # Menu Filters
45
+ gr.Markdown("## Dynamic Menu with Preferences")
46
  preference_selector = gr.Radio(
47
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
48
  value="All",
49
+ label="Choose a Preference"
50
  )
51
  menu_output = gr.HTML(value=filter_menu("All"))
52
 
53
+ # Modal Logic via HTML
54
+ modal_logic = gr.HTML("""
55
+ <div id="modal" class="modal">
56
+ <div class="modal-content">
57
+ <span class="close-button" onclick="closeModal()">&times;</span>
58
+ <img id="modal-image" class="modal-image" />
59
+ <h2 id="modal-name"></h2>
60
+ <p id="modal-description"></p>
61
+ <p id="modal-price"></p>
62
+ <label for="spice-level">Choose Spice Level:</label>
63
+ <select id="spice-level">
64
+ <option value="Mild">Mild</option>
65
+ <option value="Medium">Medium</option>
66
+ <option value="Spicy">Spicy</option>
67
+ </select>
68
+ <label for="quantity">Quantity:</label>
69
+ <input type="number" id="quantity" value="1" min="1" />
70
+ <textarea id="special-instructions" placeholder="Add special instructions here..."></textarea>
71
+ <button class="modal-add-button">Add to Cart</button>
72
+ </div>
73
+ </div>
74
+ <script>
75
+ function openModal(name, image, description, price) {
76
+ document.getElementById("modal").style.display = "block";
77
+ document.getElementById("modal-image").src = image;
78
+ document.getElementById("modal-name").innerText = name;
79
+ document.getElementById("modal-description").innerText = description;
80
+ document.getElementById("modal-price").innerText = price;
81
+ }
82
+ function closeModal() {
83
+ document.getElementById("modal").style.display = "none";
84
+ }
85
+ </script>
86
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
+ # App Layout
89
+ with gr.Row():
90
+ preference_selector.change(filter_menu, inputs=[preference_selector], outputs=[menu_output])
91
  with gr.Row():
92
  menu_output
 
 
 
 
 
 
 
 
 
 
 
93
  with gr.Row():
94
+ modal_logic
95
 
96
  return demo
97