jkorstad commited on
Commit
c2ac70f
·
verified ·
1 Parent(s): aed7a0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -54
app.py CHANGED
@@ -111,7 +111,8 @@ df['url'] = 'https://huggingface.co/spaces/' + df['id']
111
 
112
  # ------------------------------------------------------
113
 
114
- def search_spaces(search_text="", category="All Categories", offset=0, page_size=30):
 
115
  if category == "All Categories":
116
  spaces_df = df
117
  else:
@@ -120,22 +121,25 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
120
  if search_text:
121
  spaces_df = spaces_df[spaces_df['title'].str.lower().str.contains(search_text.lower())]
122
 
 
 
123
  total_spaces = len(spaces_df)
124
- spaces = spaces_df.nlargest(total_spaces, 'likes')[['title', 'likes', 'url', 'category']]
 
125
 
126
- # Get category stats
 
127
  total_likes = spaces_df['likes'].sum()
128
 
129
- # Format the results as HTML with clickable links and stats
130
  html_content = f"""
131
  <div style='margin-bottom: 20px; padding: 10px; background-color: var(--color-background-primary);
132
  border: 1px solid var(--color-border-primary); border-radius: 5px;'>
133
  <h3 style='color: var(--color-text-primary);'>Statistics:</h3>
134
- <p style='color: var(--color-text-primary);'>Showing {min(offset + page_size, total_spaces)} of {total_spaces} Spaces</p>
 
135
  <p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
136
  </div>
137
- <div style='max-height: 800px; overflow-y: auto;' id='spaces-container'>
138
- <div style='display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; padding: 10px;'>
139
  """
140
 
141
  # Only show the spaces from offset to offset + page_size
@@ -180,18 +184,15 @@ def search_spaces(search_text="", category="All Categories", offset=0, page_size
180
 
181
  html_content += "</div></div>"
182
 
183
- has_more = offset + page_size < total_spaces
184
- return html_content, has_more, total_spaces - (offset + page_size)
 
 
 
185
 
186
  def create_app():
187
- with gr.Blocks(
188
- title="Hugging Face Spaces Explorer",
189
- theme=gr.themes.Soft(
190
- primary_hue="blue",
191
- secondary_hue="blue",
192
- )
193
- ) as app:
194
- current_offset = gr.State(value=0)
195
 
196
  gr.Markdown("""
197
  # 🤗 Hugging Face Spaces Explorer
@@ -202,65 +203,81 @@ def create_app():
202
  """)
203
 
204
  with gr.Row():
205
- with gr.Column(scale=1):
206
- category_dropdown = gr.Dropdown(
207
- choices=["All Categories"] + sorted(df['category'].unique()),
208
- label="Select Category",
209
- value="All Categories"
210
- )
211
- search_input = gr.Textbox(
212
- label="Search Spaces",
213
- placeholder="Enter search terms..."
214
- )
215
 
216
  spaces_display = gr.HTML()
217
- load_more_btn = gr.Button("Load More", visible=False)
218
- remaining_text = gr.Markdown(visible=False)
219
 
220
- def update_display(search_text, category, offset):
221
- content, has_more, remaining = search_spaces(search_text, category, offset)
 
 
 
 
 
 
 
222
  return {
223
  spaces_display: content,
224
- load_more_btn: gr.update(visible=has_more), # Changed from gr.Button.update
225
- remaining_text: gr.update( # Changed from gr.Markdown.update
226
- visible=has_more,
227
- value=f"*{remaining} more spaces available*"
228
- )
 
 
229
  }
230
 
231
- def load_more(search_text, category, offset):
232
- new_offset = offset + 30
233
- return update_display(search_text, category, new_offset) | {'current_offset': new_offset}
 
 
 
 
 
 
234
 
235
  # Initial load
236
  app.load(
237
- fn=lambda: update_display("", "All Categories", 0),
238
- outputs=[spaces_display, load_more_btn, remaining_text]
239
  )
240
 
241
- # Update display when category or search changes
242
  category_dropdown.change(
243
- fn=lambda x, y, _: update_display(x, y, 0) | {'current_offset': 0},
244
- inputs=[search_input, category_dropdown, current_offset],
245
- outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
246
  )
247
 
248
  search_input.change(
249
- fn=lambda x, y, _: update_display(x, y, 0) | {'current_offset': 0},
250
- inputs=[search_input, category_dropdown, current_offset],
251
- outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
 
 
 
 
 
 
252
  )
253
 
254
- # Load More button handler
255
- load_more_btn.click(
256
- fn=load_more,
257
- inputs=[search_input, category_dropdown, current_offset],
258
- outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
259
  )
260
 
261
  return app
262
 
263
-
264
  # Launch the app
265
  app = create_app()
266
  app.launch(share=True)
 
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
118
  else:
 
121
  if search_text:
122
  spaces_df = spaces_df[spaces_df['title'].str.lower().str.contains(search_text.lower())]
123
 
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']]
132
  total_likes = spaces_df['likes'].sum()
133
 
134
+ # Generate HTML content
135
  html_content = f"""
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>
 
 
143
  """
144
 
145
  # Only show the spaces from offset to offset + page_size
 
184
 
185
  html_content += "</div></div>"
186
 
187
+ has_more = offset + limit < total_spaces
188
+ remaining = total_spaces - (offset + limit) if has_more else 0
189
+ can_go_back = offset > 0
190
+
191
+ return html_content, has_more, remaining, can_go_back, current_page, total_pages
192
 
193
  def create_app():
194
+ with gr.Blocks(title="Hugging Face Spaces Explorer", theme=gr.themes.Soft()) as app:
195
+ offset = gr.State(value=0)
 
 
 
 
 
 
196
 
197
  gr.Markdown("""
198
  # 🤗 Hugging Face Spaces Explorer
 
203
  """)
204
 
205
  with gr.Row():
206
+ category_dropdown = gr.Dropdown(
207
+ choices=["All Categories"] + sorted(df['category'].unique()),
208
+ label="Select Category",
209
+ value="All Categories"
210
+ )
211
+ search_input = gr.Textbox(
212
+ label="Search Spaces",
213
+ placeholder="Enter search terms..."
214
+ )
 
215
 
216
  spaces_display = gr.HTML()
 
 
217
 
218
+ with gr.Row():
219
+ prev_button = gr.Button("← Previous Page", visible=False)
220
+ page_info = gr.Markdown("", visible=False)
221
+ next_button = gr.Button("Next Page →", visible=False)
222
+
223
+ def load_page(search_text, category, current_offset):
224
+ content, has_more, remaining, can_go_back, current_page, total_pages = search_spaces(
225
+ search_text, category, current_offset
226
+ )
227
  return {
228
  spaces_display: content,
229
+ next_button: gr.update(visible=has_more),
230
+ prev_button: gr.update(visible=can_go_back),
231
+ page_info: gr.update(
232
+ visible=True,
233
+ value=f"*Page {current_page} of {total_pages} ({remaining} more spaces available)*"
234
+ ),
235
+ offset: current_offset
236
  }
237
 
238
+ def next_page(search_text, category, current_offset):
239
+ return load_page(search_text, category, current_offset + 100)
240
+
241
+ def prev_page(search_text, category, current_offset):
242
+ new_offset = max(0, current_offset - 100)
243
+ return load_page(search_text, category, new_offset)
244
+
245
+ def reset_and_search(search_text, category):
246
+ return load_page(search_text, category, 0)
247
 
248
  # Initial load
249
  app.load(
250
+ fn=lambda: reset_and_search("", "All Categories"),
251
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
252
  )
253
 
254
+ # Event handlers
255
  category_dropdown.change(
256
+ fn=reset_and_search,
257
+ inputs=[search_input, category_dropdown],
258
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
259
  )
260
 
261
  search_input.change(
262
+ fn=reset_and_search,
263
+ inputs=[search_input, category_dropdown],
264
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
265
+ )
266
+
267
+ next_button.click(
268
+ fn=next_page,
269
+ inputs=[search_input, category_dropdown, offset],
270
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
271
  )
272
 
273
+ prev_button.click(
274
+ fn=prev_page,
275
+ inputs=[search_input, category_dropdown, offset],
276
+ outputs=[spaces_display, next_button, prev_button, page_info, offset]
 
277
  )
278
 
279
  return app
280
 
 
281
  # Launch the app
282
  app = create_app()
283
  app.launch(share=True)