Rammohan0504 commited on
Commit
f4fc2c6
·
verified ·
1 Parent(s): eed148c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
 
3
  import gradio as gr
4
 
@@ -65,7 +65,6 @@ menu_data = [
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
@@ -77,10 +76,10 @@ def display_dishes(category):
77
  html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%;'>"
78
  for index, dish in enumerate(filtered_dishes):
79
  html_content += f"""
80
- <div id="item-{index}" 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']}', this)">
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>
@@ -107,12 +106,32 @@ with gr.Blocks() as demo:
107
 
108
  # JavaScript for managing the pop-up
109
  gr.HTML("""
 
 
 
 
 
 
 
 
 
 
110
  <script>
111
- function showPopup(image, title, description) {
 
 
 
 
 
 
112
  document.getElementById('popup-image').src = image;
113
  document.getElementById('popup-title').textContent = title;
114
  document.getElementById('popup-description').textContent = description;
115
- document.getElementById('popup').style.display = 'block';
 
 
 
 
116
  }
117
 
118
  function closePopup() {
@@ -121,24 +140,9 @@ with gr.Blocks() as demo:
121
  </script>
122
  """)
123
 
124
- # Pop-up for dish details
125
- gr.HTML("""
126
- <div id="popup" style="
127
- display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, 150%);
128
- background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
129
- z-index: 1000; text-align: center; width: 300px;">
130
- <img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;">
131
- <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
132
- <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
133
- <button onclick="closePopup()" style="
134
- margin-top: 10px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
135
- border-radius: 5px; cursor: pointer;">Close</button>
136
- </div>
137
- """)
138
-
139
  # Button click events
140
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
141
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
142
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
143
 
144
- demo.launch()
 
1
+
2
 
3
  import gradio as gr
4
 
 
65
  # The rest of the code remains unchanged
66
 
67
 
 
68
  def filter_dishes(category):
69
  if category == "ALL":
70
  return menu_data
 
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>
 
106
 
107
  # JavaScript for managing the pop-up
108
  gr.HTML("""
109
+ <div id="popup" style="
110
+ display: none; 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
 
137
  function closePopup() {
 
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()