Rammohan0504 commited on
Commit
3d2cf5e
Β·
verified Β·
1 Parent(s): 54bd4da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -32
app.py CHANGED
@@ -65,6 +65,7 @@ menu_data = [
65
  # The rest of the code remains unchanged
66
 
67
 
 
68
  def filter_dishes(category):
69
  if category == "ALL":
70
  return menu_data
@@ -74,12 +75,12 @@ def filter_dishes(category):
74
  def display_dishes(category):
75
  filtered_dishes = filter_dishes(category)
76
  html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%;'>"
77
- for index, dish in enumerate(filtered_dishes):
78
  html_content += f"""
79
  <div style='margin: 10px; display: flex; align-items: center; justify-content: space-between;
80
  width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px;
81
  background-color: #f9f9f9; cursor: pointer;'
82
- onclick="showPopup(event, '{dish['image']}', '{dish['name']}', '{dish['description']}')">
83
  <div style='flex: 1; margin-right: 20px;'>
84
  <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4>
85
  <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p>
@@ -95,42 +96,37 @@ def display_dishes(category):
95
  with gr.Blocks() as demo:
96
  gr.HTML("<h1 style='text-align: center; color: #333;'>πŸ› Biryani Hub Menu πŸ›</h1>")
97
 
98
- # Buttons for category selection
99
- with gr.Row():
100
- btn_all = gr.Button("ALL")
101
- btn_vegan = gr.Button("VEGAN")
102
- btn_halal = gr.Button("HALAL")
103
-
104
  # Dish display
105
  dish_display = gr.HTML(value=display_dishes("ALL"))
106
 
107
- # JavaScript for managing the pop-up
108
  gr.HTML("""
109
- <div id="popup" style="
110
- display: flex; position: absolute; background-color: white; padding: 20px; border-radius: 10px;
111
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 300px;">
112
- <img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;">
113
- <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
114
- <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
115
- <button onclick="closePopup()" style="
116
- margin-top: 10px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
117
- border-radius: 5px; cursor: pointer;">Close</button>
118
  </div>
119
  <script>
120
- function showPopup(event, image, title, description) {
121
- // Get cursor position from event
122
- const cursorX = event.clientX;
123
- const cursorY = event.clientY;
 
 
 
 
 
 
 
124
 
125
- // Set pop-up content
126
- const popup = document.getElementById('popup');
127
  document.getElementById('popup-image').src = image;
128
  document.getElementById('popup-title').textContent = title;
129
  document.getElementById('popup-description').textContent = description;
130
 
131
- // Position the pop-up near the cursor
132
- popup.style.top = window.scrollY + cursorY + 'px';
133
- popup.style.left = cursorX + 'px';
134
  popup.style.display = 'block';
135
  }
136
 
@@ -140,9 +136,27 @@ with gr.Blocks() as demo:
140
  </script>
141
  """)
142
 
143
- # Button click events
144
- btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
145
- btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
146
- btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
- demo.launch()
 
65
  # The rest of the code remains unchanged
66
 
67
 
68
+ # Function to filter dishes by category
69
  def filter_dishes(category):
70
  if category == "ALL":
71
  return menu_data
 
75
  def display_dishes(category):
76
  filtered_dishes = filter_dishes(category)
77
  html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%;'>"
78
+ for dish in filtered_dishes:
79
  html_content += f"""
80
  <div style='margin: 10px; display: flex; align-items: center; justify-content: space-between;
81
  width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px;
82
  background-color: #f9f9f9; cursor: pointer;'
83
+ onclick="showPopup('{dish['image']}', '{dish['name']}', '{dish['description']}')">
84
  <div style='flex: 1; margin-right: 20px;'>
85
  <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4>
86
  <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p>
 
96
  with gr.Blocks() as demo:
97
  gr.HTML("<h1 style='text-align: center; color: #333;'>πŸ› Biryani Hub Menu πŸ›</h1>")
98
 
 
 
 
 
 
 
99
  # Dish display
100
  dish_display = gr.HTML(value=display_dishes("ALL"))
101
 
102
+ # JavaScript for managing the selected button and pop-up
103
  gr.HTML("""
104
+ <div style="text-align: center; margin-bottom: 20px;">
105
+ <button id="btn-all" class="category-btn" onclick="filterCategory('ALL', this)">ALL</button>
106
+ <button id="btn-vegan" class="category-btn" onclick="filterCategory('VEGAN', this)">VEGAN</button>
107
+ <button id="btn-halal" class="category-btn" onclick="filterCategory('HALAL', this)">HALAL</button>
 
 
 
 
 
108
  </div>
109
  <script>
110
+ function filterCategory(category, button) {
111
+ const input = document.querySelector('input[aria-label="dish_display"]');
112
+ input.value = category;
113
+ input.dispatchEvent(new Event('input'));
114
+
115
+ // Highlight the selected button
116
+ const buttons = document.querySelectorAll('.category-btn');
117
+ buttons.forEach(btn => btn.style.backgroundColor = '');
118
+ button.style.backgroundColor = 'green';
119
+ button.style.color = 'white';
120
+ }
121
 
122
+ function showPopup(image, title, description) {
 
123
  document.getElementById('popup-image').src = image;
124
  document.getElementById('popup-title').textContent = title;
125
  document.getElementById('popup-description').textContent = description;
126
 
127
+ // Adjust the popup to always appear in the middle
128
+ const popup = document.getElementById('popup');
129
+ popup.style.top = window.pageYOffset + 'px';
130
  popup.style.display = 'block';
131
  }
132
 
 
136
  </script>
137
  """)
138
 
139
+ # Pop-up for dish details
140
+ gr.HTML("""
141
+ <div id="popup" style="
142
+ display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
143
+ background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
144
+ z-index: 1000; text-align: center; width: 300px;">
145
+ <img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;">
146
+ <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
147
+ <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
148
+ <button onclick="closePopup()" style="
149
+ margin-top: 10px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
150
+ border-radius: 5px; cursor: pointer;">Close</button>
151
+ </div>
152
+ """)
153
+
154
+ # Dish display input for Gradio interaction
155
+ gr.Textbox(visible=False, label="dish_display", interactive=False)
156
+
157
+ # Link button clicks to Gradio logic
158
+ btn_all = gr.Button("ALL").click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
159
+ btn_vegan = gr.Button("VEGAN").click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
160
+ btn_halal = gr.Button("HALAL").click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
161
 
162
+ demo.launch()