Rammohan0504 commited on
Commit
85b0020
·
verified ·
1 Parent(s): e8866d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -47
app.py CHANGED
@@ -12,13 +12,13 @@ menu_data = [
12
  "description": "Paneer cooked in a rich and creamy tomato-based gravy."}
13
  ]
14
 
15
- # Filter dishes by category
16
  def filter_dishes(category):
17
  if category == "ALL":
18
  return menu_data
19
  return [dish for dish in menu_data if dish["category"] == category]
20
 
21
- # Generate HTML for dish display
22
  def display_dishes(category):
23
  filtered_dishes = filter_dishes(category)
24
  html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
@@ -26,7 +26,8 @@ def display_dishes(category):
26
  html_content += f"""
27
  <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
28
  padding: 10px; border-radius: 10px; width: 200px; background-color: #f9f9f9;'
29
- onclick='selectDish("{dish['name']}")'>
 
30
  <img src='{dish['image']}' alt='{dish['name']}'
31
  style='width: 180px; height: 120px; object-fit: cover; border-radius: 10px;'>
32
  <h4 style='margin-top: 10px; font-size: 16px; color: #444;'>{dish['name']}</h4>
@@ -35,77 +36,60 @@ def display_dishes(category):
35
  html_content += "</div>"
36
  return html_content
37
 
38
- # Fetch selected dish details
39
  def get_dish_details(dish_name):
40
  for dish in menu_data:
41
  if dish["name"] == dish_name:
42
  return f"""
43
- <div style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
44
- background-color: white; padding: 20px; border-radius: 10px;
45
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center;">
46
  <img src="{dish['image']}" alt="{dish['name']}"
47
  style="width: 250px; height: 150px; object-fit: cover; border-radius: 10px;">
48
  <h2 style="margin-top: 20px; color: #444;">{dish['name']}</h2>
49
  <p style="margin-top: 10px; font-size: 16px; color: #666;">{dish['description']}</p>
50
- <button onclick="closePopup()"
51
- style="margin-top: 10px; padding: 10px 20px; background-color: #28a745; color: white;
52
- border: none; border-radius: 5px; cursor: pointer;">Close</button>
 
 
53
  </div>
54
  """
55
- return "Dish not found!"
56
 
57
  # Main Gradio App
58
  with gr.Blocks() as demo:
59
  gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Biryani Hub Menu 🍛</h1>")
60
 
61
- # Category Buttons
62
  with gr.Row():
63
  btn_all = gr.Button("ALL")
64
  btn_vegan = gr.Button("VEGAN")
65
  btn_halal = gr.Button("HALAL")
66
 
67
- # Dish display
68
  dish_display = gr.HTML(value=display_dishes("ALL"))
69
 
70
- # Hidden pop-up for dish details
71
  popup = gr.HTML(visible=False)
72
 
73
- # Event handlers
74
- def update_dishes(category):
75
- return gr.update(value=display_dishes(category))
76
 
77
- def show_dish_details(dish_name):
78
- return gr.update(visible=True, value=get_dish_details(dish_name))
79
 
80
- def hide_popup():
81
- return gr.update(visible=False)
 
 
82
 
83
- # Button actions
84
- btn_all.click(fn=lambda: update_dishes("ALL"), outputs=dish_display)
85
- btn_vegan.click(fn=lambda: update_dishes("VEGAN"), outputs=dish_display)
86
- btn_halal.click(fn=lambda: update_dishes("HALAL"), outputs=dish_display)
 
 
87
 
88
- # JavaScript for dish selection
89
- gr.HTML("""
90
- <script>
91
- function selectDish(dishName) {
92
- let input = document.querySelector('input[aria-label="popup"]');
93
- input.value = dishName;
94
- input.dispatchEvent(new Event('input', { bubbles: true }));
95
- }
96
- function closePopup() {
97
- let closeButton = document.querySelector('button[aria-label="close_popup"]');
98
- closeButton.click();
99
- }
100
- </script>
101
- """)
102
-
103
- # Hidden input and close button
104
- dish_name_input = gr.Textbox(visible=False, label="popup")
105
- close_button = gr.Button("Close Popup", visible=False)
106
-
107
- # Link events
108
- dish_name_input.change(fn=show_dish_details, inputs=dish_name_input, outputs=popup)
109
- close_button.click(fn=hide_popup, outputs=popup)
110
 
111
  demo.launch()
 
12
  "description": "Paneer cooked in a rich and creamy tomato-based gravy."}
13
  ]
14
 
15
+ # Function to filter dishes by category
16
  def filter_dishes(category):
17
  if category == "ALL":
18
  return menu_data
19
  return [dish for dish in menu_data if dish["category"] == category]
20
 
21
+ # Function to display dish cards
22
  def display_dishes(category):
23
  filtered_dishes = filter_dishes(category)
24
  html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
 
26
  html_content += f"""
27
  <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
28
  padding: 10px; border-radius: 10px; width: 200px; background-color: #f9f9f9;'
29
+ onclick="document.querySelector('input[aria-label=\\'selected_dish\\']').value='{dish['name']}';
30
+ document.querySelector('input[aria-label=\\'selected_dish\\']').dispatchEvent(new Event('input'));">
31
  <img src='{dish['image']}' alt='{dish['name']}'
32
  style='width: 180px; height: 120px; object-fit: cover; border-radius: 10px;'>
33
  <h4 style='margin-top: 10px; font-size: 16px; color: #444;'>{dish['name']}</h4>
 
36
  html_content += "</div>"
37
  return html_content
38
 
39
+ # Function to fetch dish details
40
  def get_dish_details(dish_name):
41
  for dish in menu_data:
42
  if dish["name"] == dish_name:
43
  return f"""
44
+ <div style="text-align: center; padding: 20px; font-family: Arial, sans-serif;">
 
 
45
  <img src="{dish['image']}" alt="{dish['name']}"
46
  style="width: 250px; height: 150px; object-fit: cover; border-radius: 10px;">
47
  <h2 style="margin-top: 20px; color: #444;">{dish['name']}</h2>
48
  <p style="margin-top: 10px; font-size: 16px; color: #666;">{dish['description']}</p>
49
+ <button style="padding: 10px 20px; background-color: #28a745; color: white; border: none;
50
+ border-radius: 5px; cursor: pointer;"
51
+ onclick="document.querySelector('button[aria-label=\\'close_popup\\']').click();">
52
+ Close
53
+ </button>
54
  </div>
55
  """
56
+ return "<p>Dish not found!</p>"
57
 
58
  # Main Gradio App
59
  with gr.Blocks() as demo:
60
  gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Biryani Hub Menu 🍛</h1>")
61
 
62
+ # Buttons for category filtering
63
  with gr.Row():
64
  btn_all = gr.Button("ALL")
65
  btn_vegan = gr.Button("VEGAN")
66
  btn_halal = gr.Button("HALAL")
67
 
68
+ # Display dish cards
69
  dish_display = gr.HTML(value=display_dishes("ALL"))
70
 
71
+ # Pop-up for dish details
72
  popup = gr.HTML(visible=False)
73
 
74
+ # Hidden input for selected dish
75
+ selected_dish = gr.Textbox(visible=False, label="selected_dish")
 
76
 
77
+ # Close button (hidden) for the pop-up
78
+ close_popup_btn = gr.Button("Close Popup", visible=False)
79
 
80
+ # Button events
81
+ btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
82
+ btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
83
+ btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
84
 
85
+ # Event to show pop-up
86
+ selected_dish.change(
87
+ lambda dish_name: gr.update(visible=True, value=get_dish_details(dish_name)),
88
+ inputs=selected_dish,
89
+ outputs=popup,
90
+ )
91
 
92
+ # Event to close pop-up
93
+ close_popup_btn.click(lambda: gr.update(visible=False), outputs=popup)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  demo.launch()