Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -149,7 +149,7 @@ def create_pagination_html(current_page, total_pages):
|
|
149 |
html += "</div>"
|
150 |
return html
|
151 |
|
152 |
-
def search_spaces(search_text="", category="All Categories",
|
153 |
# Filter spaces
|
154 |
if category == "All Categories":
|
155 |
spaces_df = df
|
@@ -162,8 +162,8 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=100
|
|
162 |
# Sort by likes and get total count
|
163 |
spaces_df = spaces_df.sort_values('likes', ascending=False)
|
164 |
total_spaces = len(spaces_df)
|
165 |
-
total_pages = (total_spaces + limit - 1) // limit
|
166 |
-
|
167 |
|
168 |
# Get the current page of spaces
|
169 |
spaces = spaces_df.iloc[offset:offset + limit][['title', 'likes', 'url', 'category']]
|
@@ -174,19 +174,38 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=100
|
|
174 |
<div style='margin-bottom: 20px; padding: 10px; background-color: var(--color-background-primary);
|
175 |
border: 1px solid var(--color-border-primary); border-radius: 5px;'>
|
176 |
<h3 style='color: var(--color-text-primary);'>Statistics:</h3>
|
177 |
-
<p style='color: var(--color-text-primary);'>Page {
|
178 |
<p style='color: var(--color-text-primary);'>Showing {offset + 1}-{min(offset + limit, total_spaces)} of {total_spaces} Spaces</p>
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
html_content += """
|
188 |
<div style='max-height: 800px; overflow-y: auto;'>
|
189 |
-
<div style='display: grid;
|
|
|
|
|
|
|
|
|
190 |
"""
|
191 |
|
192 |
# Add space cards
|
@@ -199,8 +218,10 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=100
|
|
199 |
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
200 |
display: flex;
|
201 |
flex-direction: column;
|
202 |
-
height: 100%;
|
203 |
-
|
|
|
|
|
204 |
<a href='{row['url']}' target='_blank'
|
205 |
style='color: #2196F3;
|
206 |
text-decoration: none;
|
@@ -213,7 +234,8 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=100
|
|
213 |
<span style='background-color: var(--color-accent-soft);
|
214 |
padding: 2px 8px;
|
215 |
border-radius: 12px;
|
216 |
-
font-size: 0.9em;
|
|
|
217 |
{row['category']}
|
218 |
</span>
|
219 |
</p>
|
@@ -225,31 +247,24 @@ def search_spaces(search_text="", category="All Categories", offset=0, limit=100
|
|
225 |
</p>
|
226 |
</div>
|
227 |
"""
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
# Add JavaScript for handling page clicks
|
233 |
html_content += """
|
234 |
<script>
|
235 |
-
function
|
236 |
-
const pageEvent = new CustomEvent('
|
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
|
245 |
-
remaining = total_spaces - (offset + limit) if has_more else 0
|
246 |
-
can_go_back = offset > 0
|
247 |
|
248 |
-
return html_content
|
249 |
|
250 |
def create_app():
|
251 |
with gr.Blocks(title="Hugging Face Spaces Explorer", theme=gr.themes.Soft()) as app:
|
252 |
-
|
253 |
|
254 |
gr.Markdown("""
|
255 |
# 🤗 Hugging Face Spaces Explorer
|
@@ -271,93 +286,39 @@ def create_app():
|
|
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)
|
304 |
-
page_info = gr.Markdown("", visible=False)
|
305 |
-
next_button = gr.Button("Next Page →", visible=False)
|
306 |
-
|
307 |
-
def load_page(search_text, category, current_offset):
|
308 |
-
content, has_more, remaining, can_go_back, current_page, total_pages = search_spaces(
|
309 |
-
search_text, category, current_offset
|
310 |
-
)
|
311 |
-
return {
|
312 |
-
spaces_display: content,
|
313 |
-
next_button: gr.update(visible=has_more),
|
314 |
-
prev_button: gr.update(visible=can_go_back),
|
315 |
-
page_info: gr.update(
|
316 |
-
visible=True,
|
317 |
-
value=f"*Page {current_page} of {total_pages} ({remaining} more spaces available)*"
|
318 |
-
),
|
319 |
-
offset: current_offset
|
320 |
-
}
|
321 |
-
|
322 |
-
def next_page(search_text, category, current_offset):
|
323 |
-
return load_page(search_text, category, current_offset + 100)
|
324 |
|
325 |
-
def
|
326 |
-
|
327 |
-
return load_page(search_text, category, new_offset)
|
328 |
-
|
329 |
-
def reset_and_search(search_text, category):
|
330 |
-
return load_page(search_text, category, 0)
|
331 |
-
|
332 |
-
# Initial load
|
333 |
-
app.load(
|
334 |
-
fn=lambda: reset_and_search("", "All Categories"),
|
335 |
-
outputs=[spaces_display, next_button, prev_button, page_info, offset]
|
336 |
-
)
|
337 |
|
338 |
# Event handlers
|
339 |
category_dropdown.change(
|
340 |
-
fn=
|
341 |
-
inputs=[search_input, category_dropdown],
|
342 |
-
outputs=
|
343 |
)
|
344 |
|
345 |
search_input.change(
|
346 |
-
fn=
|
347 |
-
inputs=[search_input, category_dropdown],
|
348 |
-
outputs=
|
349 |
)
|
350 |
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
|
|
361 |
)
|
362 |
|
363 |
return app
|
|
|
149 |
html += "</div>"
|
150 |
return html
|
151 |
|
152 |
+
def search_spaces(search_text="", category="All Categories", page=1, limit=100):
|
153 |
# Filter spaces
|
154 |
if category == "All Categories":
|
155 |
spaces_df = df
|
|
|
162 |
# Sort by likes and get total count
|
163 |
spaces_df = spaces_df.sort_values('likes', ascending=False)
|
164 |
total_spaces = len(spaces_df)
|
165 |
+
total_pages = max(1, (total_spaces + limit - 1) // limit)
|
166 |
+
offset = (page - 1) * limit
|
167 |
|
168 |
# Get the current page of spaces
|
169 |
spaces = spaces_df.iloc[offset:offset + limit][['title', 'likes', 'url', 'category']]
|
|
|
174 |
<div style='margin-bottom: 20px; padding: 10px; background-color: var(--color-background-primary);
|
175 |
border: 1px solid var(--color-border-primary); border-radius: 5px;'>
|
176 |
<h3 style='color: var(--color-text-primary);'>Statistics:</h3>
|
177 |
+
<p style='color: var(--color-text-primary);'>Page {page} of {total_pages}</p>
|
178 |
<p style='color: var(--color-text-primary);'>Showing {offset + 1}-{min(offset + limit, total_spaces)} of {total_spaces} Spaces</p>
|
179 |
<p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
|
180 |
</div>
|
181 |
+
|
182 |
+
<div style='display: flex; justify-content: center; gap: 10px; margin: 20px 0;'>
|
183 |
"""
|
|
|
|
|
|
|
184 |
|
185 |
+
# Add pagination buttons
|
186 |
+
if page > 1:
|
187 |
+
html_content += f"<button onclick='page_change({page-1})' class='page-btn'>Previous</button>"
|
188 |
+
|
189 |
+
# Add numeric page buttons
|
190 |
+
for p in range(max(1, page-2), min(total_pages+1, page+3)):
|
191 |
+
if p == page:
|
192 |
+
html_content += f"<button onclick='page_change({p})' class='page-btn active'>{p}</button>"
|
193 |
+
else:
|
194 |
+
html_content += f"<button onclick='page_change({p})' class='page-btn'>{p}</button>"
|
195 |
+
|
196 |
+
if page < total_pages:
|
197 |
+
html_content += f"<button onclick='page_change({page+1})' class='page-btn'>Next</button>"
|
198 |
+
|
199 |
+
html_content += "</div>"
|
200 |
+
|
201 |
+
# Add grid container with responsive design
|
202 |
html_content += """
|
203 |
<div style='max-height: 800px; overflow-y: auto;'>
|
204 |
+
<div style='display: grid;
|
205 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
206 |
+
gap: 15px;
|
207 |
+
padding: 10px;
|
208 |
+
max-width: 100%;'>
|
209 |
"""
|
210 |
|
211 |
# Add space cards
|
|
|
218 |
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
219 |
display: flex;
|
220 |
flex-direction: column;
|
221 |
+
height: 100%;
|
222 |
+
min-width: 0;
|
223 |
+
word-wrap: break-word;'>
|
224 |
+
<h3 style='margin-top: 0; margin-bottom: 10px; overflow: hidden; text-overflow: ellipsis;'>
|
225 |
<a href='{row['url']}' target='_blank'
|
226 |
style='color: #2196F3;
|
227 |
text-decoration: none;
|
|
|
234 |
<span style='background-color: var(--color-accent-soft);
|
235 |
padding: 2px 8px;
|
236 |
border-radius: 12px;
|
237 |
+
font-size: 0.9em;
|
238 |
+
display: inline-block;'>
|
239 |
{row['category']}
|
240 |
</span>
|
241 |
</p>
|
|
|
247 |
</p>
|
248 |
</div>
|
249 |
"""
|
250 |
+
|
251 |
+
html_content += "</div></div>"
|
252 |
+
|
253 |
+
# Add JavaScript for page navigation
|
|
|
254 |
html_content += """
|
255 |
<script>
|
256 |
+
function page_change(page) {
|
257 |
+
const pageEvent = new CustomEvent('page_change', { detail: page });
|
258 |
document.dispatchEvent(pageEvent);
|
259 |
}
|
260 |
</script>
|
261 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
+
return html_content
|
264 |
|
265 |
def create_app():
|
266 |
with gr.Blocks(title="Hugging Face Spaces Explorer", theme=gr.themes.Soft()) as app:
|
267 |
+
current_page = gr.State(value=1)
|
268 |
|
269 |
gr.Markdown("""
|
270 |
# 🤗 Hugging Face Spaces Explorer
|
|
|
286 |
)
|
287 |
|
288 |
spaces_display = gr.HTML()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
289 |
|
290 |
+
def update_spaces(search_text, category, page):
|
291 |
+
return search_spaces(search_text, category, page)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
|
293 |
# Event handlers
|
294 |
category_dropdown.change(
|
295 |
+
fn=update_spaces,
|
296 |
+
inputs=[search_input, category_dropdown, gr.State(1)],
|
297 |
+
outputs=spaces_display
|
298 |
)
|
299 |
|
300 |
search_input.change(
|
301 |
+
fn=update_spaces,
|
302 |
+
inputs=[search_input, category_dropdown, gr.State(1)],
|
303 |
+
outputs=spaces_display
|
304 |
)
|
305 |
|
306 |
+
# Add page change handler
|
307 |
+
app.load(js="""
|
308 |
+
function setupPageChangeListener() {
|
309 |
+
document.addEventListener('page_change', function(e) {
|
310 |
+
const page = e.detail;
|
311 |
+
gradioApp().querySelector('#update_page').click();
|
312 |
+
});
|
313 |
+
}
|
314 |
+
setupPageChangeListener();
|
315 |
+
""")
|
316 |
|
317 |
+
update_page = gr.Button(visible=False, elem_id="update_page")
|
318 |
+
update_page.click(
|
319 |
+
fn=update_spaces,
|
320 |
+
inputs=[search_input, category_dropdown, current_page],
|
321 |
+
outputs=spaces_display
|
322 |
)
|
323 |
|
324 |
return app
|