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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -23
app.py CHANGED
@@ -111,7 +111,7 @@ df['url'] = 'https://huggingface.co/spaces/' + df['id']
111
 
112
  # ------------------------------------------------------
113
 
114
- def search_spaces(search_text="", category="All Categories", offset=0, limit=30):
115
  # Filter spaces
116
  if category == "All Categories":
117
  spaces_df = df
@@ -124,6 +124,8 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=30)
124
  # Sort by likes and get total count
125
  spaces_df = spaces_df.sort_values('likes', ascending=False)
126
  total_spaces = len(spaces_df)
 
 
127
 
128
  # Get the current page of spaces
129
  spaces = spaces_df.iloc[offset:offset + limit][['title', 'likes', 'url', 'category']]
@@ -134,6 +136,7 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=30)
134
  <div style='margin-bottom: 20px; padding: 10px; background-color: var(--color-background-primary);
135
  border: 1px solid var(--color-border-primary); border-radius: 5px;'>
136
  <h3 style='color: var(--color-text-primary);'>Statistics:</h3>
 
137
  <p style='color: var(--color-text-primary);'>Showing {offset + 1}-{min(offset + limit, total_spaces)} of {total_spaces} Spaces</p>
138
  <p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
139
  </div>
@@ -186,8 +189,9 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=30)
186
 
187
  has_more = offset + limit < total_spaces
188
  remaining = total_spaces - (offset + limit) if has_more else 0
 
189
 
190
- return html_content, has_more, remaining
191
 
192
  def create_app():
193
  with gr.Blocks(title="Hugging Face Spaces Explorer", theme=gr.themes.Soft()) as app:
@@ -213,51 +217,66 @@ def create_app():
213
  )
214
 
215
  spaces_display = gr.HTML()
216
- load_more_button = gr.Button("Load More", visible=False)
217
- remaining_count = gr.Markdown(visible=False)
218
 
219
- def load_more(search_text, category, current_offset):
220
- new_offset = current_offset + 30
221
- content, has_more, remaining = search_spaces(search_text, category, new_offset)
 
 
 
 
 
 
222
  return {
223
  spaces_display: content,
224
- load_more_button: gr.update(visible=has_more),
225
- remaining_count: gr.update(visible=has_more, value=f"*{remaining} more spaces available*"),
226
- offset: new_offset
 
 
 
 
227
  }
228
 
 
 
 
 
 
 
 
229
  def reset_and_search(search_text, category):
230
- content, has_more, remaining = search_spaces(search_text, category, 0)
231
- return {
232
- spaces_display: content,
233
- load_more_button: gr.update(visible=has_more),
234
- remaining_count: gr.update(visible=has_more, value=f"*{remaining} more spaces available*"),
235
- offset: 0
236
- }
237
 
238
  # Initial load
239
  app.load(
240
  fn=lambda: reset_and_search("", "All Categories"),
241
- outputs=[spaces_display, load_more_button, remaining_count, offset]
242
  )
243
 
244
  # Event handlers
245
  category_dropdown.change(
246
  fn=reset_and_search,
247
  inputs=[search_input, category_dropdown],
248
- outputs=[spaces_display, load_more_button, remaining_count, offset]
249
  )
250
 
251
  search_input.change(
252
  fn=reset_and_search,
253
  inputs=[search_input, category_dropdown],
254
- outputs=[spaces_display, load_more_button, remaining_count, offset]
 
 
 
 
 
 
255
  )
256
 
257
- load_more_button.click(
258
- fn=load_more,
259
  inputs=[search_input, category_dropdown, offset],
260
- outputs=[spaces_display, load_more_button, remaining_count, offset]
261
  )
262
 
263
  return app
 
111
 
112
  # ------------------------------------------------------
113
 
114
+ def search_spaces(search_text="", category="All Categories", offset=0, limit=100):
115
  # Filter spaces
116
  if category == "All Categories":
117
  spaces_df = df
 
124
  # Sort by likes and get total count
125
  spaces_df = spaces_df.sort_values('likes', ascending=False)
126
  total_spaces = len(spaces_df)
127
+ total_pages = (total_spaces + limit - 1) // limit
128
+ current_page = (offset // limit) + 1
129
 
130
  # Get the current page of spaces
131
  spaces = spaces_df.iloc[offset:offset + limit][['title', 'likes', 'url', 'category']]
 
136
  <div style='margin-bottom: 20px; padding: 10px; background-color: var(--color-background-primary);
137
  border: 1px solid var(--color-border-primary); border-radius: 5px;'>
138
  <h3 style='color: var(--color-text-primary);'>Statistics:</h3>
139
+ <p style='color: var(--color-text-primary);'>Page {current_page} of {total_pages}</p>
140
  <p style='color: var(--color-text-primary);'>Showing {offset + 1}-{min(offset + limit, total_spaces)} of {total_spaces} Spaces</p>
141
  <p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
142
  </div>
 
189
 
190
  has_more = offset + limit < total_spaces
191
  remaining = total_spaces - (offset + limit) if has_more else 0
192
+ can_go_back = offset > 0
193
 
194
+ return html_content, has_more, remaining, can_go_back, current_page, total_pages
195
 
196
  def create_app():
197
  with gr.Blocks(title="Hugging Face Spaces Explorer", theme=gr.themes.Soft()) as app:
 
217
  )
218
 
219
  spaces_display = gr.HTML()
 
 
220
 
221
+ with gr.Row():
222
+ prev_button = gr.Button("← Previous Page", visible=False)
223
+ page_info = gr.Markdown("", visible=False)
224
+ next_button = gr.Button("Next Page →", visible=False)
225
+
226
+ def load_page(search_text, category, current_offset):
227
+ content, has_more, remaining, can_go_back, current_page, total_pages = search_spaces(
228
+ search_text, category, current_offset
229
+ )
230
  return {
231
  spaces_display: content,
232
+ next_button: gr.update(visible=has_more),
233
+ prev_button: gr.update(visible=can_go_back),
234
+ page_info: gr.update(
235
+ visible=True,
236
+ value=f"*Page {current_page} of {total_pages} ({remaining} more spaces available)*"
237
+ ),
238
+ offset: current_offset
239
  }
240
 
241
+ def next_page(search_text, category, current_offset):
242
+ return load_page(search_text, category, current_offset + 100)
243
+
244
+ def prev_page(search_text, category, current_offset):
245
+ new_offset = max(0, current_offset - 100)
246
+ return load_page(search_text, category, new_offset)
247
+
248
  def reset_and_search(search_text, category):
249
+ return load_page(search_text, category, 0)
 
 
 
 
 
 
250
 
251
  # Initial load
252
  app.load(
253
  fn=lambda: reset_and_search("", "All Categories"),
254
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
255
  )
256
 
257
  # Event handlers
258
  category_dropdown.change(
259
  fn=reset_and_search,
260
  inputs=[search_input, category_dropdown],
261
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
262
  )
263
 
264
  search_input.change(
265
  fn=reset_and_search,
266
  inputs=[search_input, category_dropdown],
267
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
268
+ )
269
+
270
+ next_button.click(
271
+ fn=next_page,
272
+ inputs=[search_input, category_dropdown, offset],
273
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
274
  )
275
 
276
+ prev_button.click(
277
+ fn=prev_page,
278
  inputs=[search_input, category_dropdown, offset],
279
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
280
  )
281
 
282
  return app