jkorstad commited on
Commit
5aaa481
·
verified ·
1 Parent(s): 75255f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -1
app.py CHANGED
@@ -111,6 +111,44 @@ df['url'] = 'https://huggingface.co/spaces/' + df['id']
111
 
112
  # ------------------------------------------------------
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  def search_spaces(search_text="", category="All Categories", offset=0, limit=100):
115
  # Filter spaces
116
  if category == "All Categories":
@@ -141,6 +179,9 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=100
141
  <p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
142
  </div>
143
  """
 
 
 
144
 
145
  # Add grid container
146
  html_content += """
@@ -184,7 +225,20 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=100
184
  </p>
185
  </div>
186
  """
187
-
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  html_content += "</div></div>"
189
 
190
  has_more = offset + limit < total_spaces
@@ -217,6 +271,33 @@ def create_app():
217
  )
218
 
219
  spaces_display = gr.HTML()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
  with gr.Row():
222
  prev_button = gr.Button("← Previous Page", visible=False)
 
111
 
112
  # ------------------------------------------------------
113
 
114
+ def create_pagination_html(current_page, total_pages):
115
+ """Create HTML for pagination links"""
116
+ html = "<div style='text-align: center; margin: 20px 0;'>"
117
+
118
+ # Calculate which page numbers to show
119
+ if total_pages <= 7:
120
+ pages_to_show = range(1, total_pages + 1)
121
+ else:
122
+ if current_page <= 4:
123
+ pages_to_show = list(range(1, 6)) + ['...', total_pages]
124
+ elif current_page >= total_pages - 3:
125
+ pages_to_show = [1, '...'] + list(range(total_pages - 4, total_pages + 1))
126
+ else:
127
+ pages_to_show = [1, '...'] + list(range(current_page - 1, current_page + 2)) + ['...', total_pages]
128
+
129
+ # Generate the HTML for page links
130
+ for page in pages_to_show:
131
+ if page == '...':
132
+ html += f"<span style='margin: 0 5px;'>...</span>"
133
+ else:
134
+ if page == current_page:
135
+ html += f"""
136
+ <span style='margin: 0 5px; padding: 5px 10px;
137
+ background-color: var(--color-accent-soft);
138
+ border-radius: 5px; font-weight: bold;'>{page}</span>
139
+ """
140
+ else:
141
+ html += f"""
142
+ <button onclick='handlePageClick({page-1})'
143
+ style='margin: 0 5px; padding: 5px 10px;
144
+ border: 1px solid var(--color-border-primary);
145
+ border-radius: 5px; cursor: pointer;
146
+ background: none;'>{page}</button>
147
+ """
148
+
149
+ html += "</div>"
150
+ return html
151
+
152
  def search_spaces(search_text="", category="All Categories", offset=0, limit=100):
153
  # Filter spaces
154
  if category == "All Categories":
 
179
  <p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
180
  </div>
181
  """
182
+
183
+ # Add pagination at the top
184
+ html_content += create_pagination_html(current_page, total_pages)
185
 
186
  # Add grid container
187
  html_content += """
 
225
  </p>
226
  </div>
227
  """
228
+
229
+ # Add pagination at the bottom
230
+ html_content += create_pagination_html(current_page, total_pages)
231
+
232
+ # Add JavaScript for handling page clicks
233
+ html_content += """
234
+ <script>
235
+ function handlePageClick(page) {
236
+ const pageEvent = new CustomEvent('pagination', { detail: { page: page } });
237
+ document.dispatchEvent(pageEvent);
238
+ }
239
+ </script>
240
+ """
241
+ # Possibly Delete *Check Here*
242
  html_content += "</div></div>"
243
 
244
  has_more = offset + limit < total_spaces
 
271
  )
272
 
273
  spaces_display = gr.HTML()
274
+ #____________________________________________________________________________________
275
+ page_info = gr.Markdown("", visible=False)
276
+
277
+ def go_to_page(search_text, category, page_num):
278
+ new_offset = page_num * 100
279
+ return load_page(search_text, category, new_offset)
280
+
281
+ # Add JavaScript event listener for pagination
282
+ app.load(js="""
283
+ function addPaginationListener() {
284
+ document.addEventListener('pagination', function(e) {
285
+ const page = e.detail.page;
286
+ // Call the Python function through Gradio's API
287
+ document.querySelector('gradio-app').querySelector('#go_to_page_btn').click();
288
+ });
289
+ }
290
+ addPaginationListener();
291
+ """)
292
+
293
+ # Hidden button for JavaScript to trigger
294
+ go_to_page_btn = gr.Button(visible=False, elem_id="go_to_page_btn")
295
+ go_to_page_btn.click(
296
+ fn=go_to_page,
297
+ inputs=[search_input, category_dropdown, gr.State(lambda evt: evt.detail.page)],
298
+ outputs=[spaces_display, page_info, offset]
299
+ )
300
+ #____________________________________________________________________________________
301
 
302
  with gr.Row():
303
  prev_button = gr.Button("← Previous Page", visible=False)