Rammohan0504 commited on
Commit
e49f366
Β·
verified Β·
1 Parent(s): ff4e9fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -51
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
 
3
- # Menu data
4
  menu_data = [
5
  {"name": "Veg Burger", "category": "VEGAN",
6
  "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg",
@@ -61,19 +60,30 @@ menu_data = [
61
  "description": "A refreshing salad made with chickpeas, fresh vegetables, and herbs."}
62
  ]
63
 
64
- # Function to generate row-style dish cards
 
 
 
 
 
 
 
 
65
  def display_dishes(category):
66
- filtered_dishes = [dish for dish in menu_data if category == "ALL" or dish["category"] == category]
67
- html_content = "<div style='display: flex; flex-wrap: wrap; gap: 15px; justify-content: center;'>"
68
  for dish in filtered_dishes:
69
  html_content += f"""
70
- <div style='width: 220px; padding: 10px; border: 1px solid #ddd; border-radius: 10px;
71
- background-color: #f9f9f9; text-align: center; cursor: pointer;'
72
- onclick="showPopup(event, '{dish['image']}', '{dish['name']}', '{dish['description']}')">
 
 
 
 
 
73
  <img src='{dish['image']}' alt='{dish['name']}'
74
- style='width: 100%; height: 140px; object-fit: cover; border-radius: 10px;'>
75
- <h4 style='margin: 10px 0; color: #444;'>{dish['name']}</h4>
76
- <p style='color: #666; font-size: 14px;'>{dish['description']}</p>
77
  </div>
78
  """
79
  html_content += "</div>"
@@ -81,22 +91,25 @@ def display_dishes(category):
81
 
82
  # Main Gradio App
83
  with gr.Blocks() as demo:
84
- gr.HTML("<h1 style='text-align: center; color: #333;'>πŸ› Restaurant Menu πŸ›</h1>")
85
 
86
- # Category Buttons
87
  with gr.Row():
88
  btn_all = gr.Button("ALL")
89
  btn_vegan = gr.Button("VEGAN")
90
  btn_halal = gr.Button("HALAL")
91
 
92
- # Dish display area
93
  dish_display = gr.HTML(value=display_dishes("ALL"))
94
 
95
- # Pop-up HTML and JavaScript
96
  gr.HTML("""
97
- <div id="popup" style="display: none; position: fixed; justify-content:center; align-items:center; transform: translate(-50%, 50%);
98
- background-color: white; padding: 20px; border-radius: 10px;
99
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 300px;">
 
 
 
100
  <img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;">
101
  <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
102
  <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
@@ -104,49 +117,25 @@ with gr.Blocks() as demo:
104
  color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button>
105
  </div>
106
  <script>
107
- // Show the pop-up at the cursor's position
108
- function showPopup(event, image, title, description) {
109
- const popup = document.getElementById('popup');
110
- const popupImage = document.getElementById('popup-image');
111
- const popupTitle = document.getElementById('popup-title');
112
- const popupDescription = document.getElementById('popup-description');
113
-
114
- // Update the pop-up content
115
- popupImage.src = image;
116
- popupTitle.textContent = title;
117
- popupDescription.textContent = description;
118
 
119
- // Set the pop-up position based on cursor location
120
- const popupWidth = 300; // Width of the pop-up
121
- const xOffset = 10; // Offset to avoid overlapping cursor
122
- const yOffset = 20; // Offset to place pop-up below the cursor
123
- let xPos = event.pageX + xOffset;
124
- let yPos = event.pageY + yOffset;
125
-
126
- // Prevent the pop-up from going off the screen
127
- if (xPos + popupWidth > window.innerWidth) {
128
- xPos = window.innerWidth - popupWidth - xOffset;
129
- }
130
- if (yPos + popup.offsetHeight > window.innerHeight) {
131
- yPos = window.innerHeight - popup.offsetHeight - yOffset;
132
- }
133
-
134
- popup.style.left = `${xPos}px`;
135
- popup.style.top = `${yPos}px`;
136
  popup.style.display = 'block';
137
  }
138
-
139
- // Close the pop-up
140
  function closePopup() {
141
- const popup = document.getElementById('popup');
142
- popup.style.display = 'none';
143
  }
144
  </script>
145
  """)
146
 
147
- # Button actions for filtering
148
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
149
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
150
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
151
 
152
- demo.launch()
 
1
  import gradio as gr
2
 
 
3
  menu_data = [
4
  {"name": "Veg Burger", "category": "VEGAN",
5
  "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg",
 
60
  "description": "A refreshing salad made with chickpeas, fresh vegetables, and herbs."}
61
  ]
62
 
63
+ # The rest of the code remains unchanged
64
+
65
+
66
+ def filter_dishes(category):
67
+ if category == "ALL":
68
+ return menu_data
69
+ return [dish for dish in menu_data if dish["category"] == category]
70
+
71
+ # Function to generate dish cards
72
  def display_dishes(category):
73
+ filtered_dishes = filter_dishes(category)
74
+ html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%;'>"
75
  for dish in filtered_dishes:
76
  html_content += f"""
77
+ <div style='margin: 10px; display: flex; align-items: center; justify-content: space-between;
78
+ width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px;
79
+ background-color: #f9f9f9; cursor: pointer;'
80
+ onclick="showPopup('{dish['image']}', '{dish['name']}', '{dish['description']}')">
81
+ <div style='flex: 1; margin-right: 20px;'>
82
+ <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4>
83
+ <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p>
84
+ </div>
85
  <img src='{dish['image']}' alt='{dish['name']}'
86
+ style='width: 120px; height: 80px; object-fit: cover; border-radius: 10px;'>
 
 
87
  </div>
88
  """
89
  html_content += "</div>"
 
91
 
92
  # Main Gradio App
93
  with gr.Blocks() as demo:
94
+ gr.HTML("<h1 style='text-align: center; color: #333;'>πŸ› Biryani Hub Menu πŸ›</h1>")
95
 
96
+ # Buttons for category selection
97
  with gr.Row():
98
  btn_all = gr.Button("ALL")
99
  btn_vegan = gr.Button("VEGAN")
100
  btn_halal = gr.Button("HALAL")
101
 
102
+ # Dish display
103
  dish_display = gr.HTML(value=display_dishes("ALL"))
104
 
105
+ # JavaScript for managing the pop-up
106
  gr.HTML("""
107
+ <div id="popup" style="display: none; position: fixed;cursor: pointer;-webkit-user-select: none;
108
+ -moz-user-select: none;
109
+ -ms-user-select: none;
110
+ user-select: none; left: 50%; transform: translate(-50%, 100%);
111
+ background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
112
+ z-index: 1000; text-align: center; width: 300px;">
113
  <img id="popup-image" src="" alt="" style="width: 100%; height: auto; border-radius: 10px;">
114
  <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
115
  <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
 
117
  color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button>
118
  </div>
119
  <script>
120
+ function showPopup(image, title, description) {
121
+ document.getElementById('popup-image').src = image;
122
+ document.getElementById('popup-title').textContent = title;
123
+ document.getElementById('popup-description').textContent = description;
 
 
 
 
 
 
 
124
 
125
+ // Adjust the popup to always appear at the top of the viewport
126
+ const popup = document.getElementById('popup');
127
+ popup.style.top = window.pageYOffset + 'px';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  popup.style.display = 'block';
129
  }
 
 
130
  function closePopup() {
131
+ document.getElementById('popup').style.display = 'none';
 
132
  }
133
  </script>
134
  """)
135
 
136
+ # Button click events
137
  btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
138
  btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
139
  btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
140
 
141
+ demo.launch()