akhaliq HF staff commited on
Commit
c6e36bd
·
verified ·
1 Parent(s): 2bc512d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -73
app.py CHANGED
@@ -190,27 +190,6 @@ class PaperManager:
190
  df_sorted = df
191
  elif self.sort_method == "new":
192
  df_sorted = df.sort_values(by='date', ascending=False) # Sort by 'date'
193
- elif self.sort_method == "top":
194
- # Filter based on the selected time frame
195
- now = datetime.datetime.now(timezone.utc)
196
- if self.top_time_frame == "day":
197
- time_threshold = now - datetime.timedelta(days=1)
198
- elif self.top_time_frame == "week":
199
- time_threshold = now - datetime.timedelta(weeks=1)
200
- elif self.top_time_frame == "month":
201
- time_threshold = now - datetime.timedelta(days=30)
202
- elif self.top_time_frame == "year":
203
- time_threshold = now - datetime.timedelta(days=365)
204
- elif self.top_time_frame == "all time":
205
- time_threshold = datetime.datetime.min.replace(tzinfo=timezone.utc)
206
- else:
207
- time_threshold = datetime.datetime.min.replace(tzinfo=timezone.utc)
208
-
209
- # Convert 'date' column to datetime
210
- df_sorted = df.copy()
211
- df_sorted['date_parsed'] = pd.to_datetime(df_sorted['date'], errors='coerce').dt.tz_localize(timezone.utc, ambiguous='NaT', nonexistent='NaT')
212
- df_sorted = df_sorted[df_sorted['date_parsed'] >= time_threshold]
213
- df_sorted = df_sorted.sort_values(by='upvotes', ascending=False).drop(columns=['date_parsed'])
214
  else:
215
  df_sorted = df
216
 
@@ -222,16 +201,12 @@ class PaperManager:
222
 
223
  def set_sort_method(self, method, time_frame=None):
224
  """
225
- Sets the sort method ('hot', 'new', 'top') and re-sorts the papers.
226
- If 'top' is selected, also sets the time frame.
227
  """
228
- if method not in ["hot", "new", "top"]:
229
  method = "hot"
230
  logger.info(f"Setting sort method to: {method}")
231
  self.sort_method = method
232
- if method == "top" and time_frame:
233
- self.top_time_frame = time_frame.lower()
234
- logger.info(f"Setting top time frame to: {self.top_time_frame}")
235
  self.sort_papers()
236
  return True # Assume success
237
 
@@ -385,15 +360,12 @@ logger.info("Scheduler shutdown registered.")
385
 
386
  # --- Gradio Interface Functions ---
387
 
388
- def change_sort_method_ui(method: str, time_frame: str = "all time") -> str:
389
  """
390
- Changes the sort method and, if 'top' is selected, sets the time frame.
391
  """
392
- logger.info(f"Changing sort method to: {method} with time frame: {time_frame}")
393
- if method.lower() == "top":
394
- paper_manager.set_sort_method(method.lower(), time_frame)
395
- else:
396
- paper_manager.set_sort_method(method.lower())
397
  return paper_manager.get_current_page_papers()
398
 
399
 
@@ -440,6 +412,14 @@ table {
440
  text-decoration: none;
441
  }
442
 
 
 
 
 
 
 
 
 
443
  .itemlist .athing {
444
  background-color: #f6f6ef;
445
  }
@@ -477,6 +457,11 @@ table {
477
  font-size: 12pt;
478
  }
479
 
 
 
 
 
 
480
  .storylink {
481
  font-size: 9pt;
482
  }
@@ -509,6 +494,10 @@ table {
509
  color: black;
510
  }
511
 
 
 
 
 
512
  .itemlist .athing {
513
  background-color: #1e1e1e;
514
  }
@@ -556,20 +545,24 @@ with demo:
556
  <b class="hnname"><a href="#">Daily Papers</a></b>
557
  </span>
558
  </td>
559
- <td align="right">
560
- <span class="pagetop">
561
- <a href="#" id="hot_sort">Hot</a> |
562
- <a href="#" id="new_sort">New</a>
563
- </span>
564
  </td>
565
  </tr>
566
  </table>
 
 
 
 
 
 
 
 
567
  """)
568
- # Hidden components for Gradio to capture sort clicks
569
- hot_sort = gr.Button("Hot", visible=False)
570
- new_sort = gr.Button("New", visible=False)
571
- # Sort Options and Time Frame (conditionally visible) - Removed "Top" sort
572
- # Removed the time_frame_dropdown as "Top" sort is removed
573
  # Paper list
574
  paper_list = gr.HTML()
575
  # Navigation Buttons
@@ -587,35 +580,7 @@ with demo:
587
  prev_button.click(paper_manager.prev_page, outputs=[paper_list])
588
  next_button.click(paper_manager.next_page, outputs=[paper_list])
589
 
590
- # Sort option changes via header links
591
- # Since Gradio cannot capture clicks on HTML directly, use hidden buttons and trigger them via JavaScript
592
- # Add JavaScript to trigger Gradio button clicks when header links are clicked
593
- demo.load(
594
- fn=lambda: None,
595
- outputs=[],
596
- _js="""
597
- const hotLink = document.getElementById('hot_sort');
598
- const newLink = document.getElementById('new_sort');
599
- const hotButton = document.querySelector('button#component-__block-0--hot_sort');
600
- const newButton = document.querySelector('button#component-__block-0--new_sort');
601
-
602
- if (hotLink && hotButton) {
603
- hotLink.addEventListener('click', (e) => {
604
- e.preventDefault();
605
- hotButton.click();
606
- });
607
- }
608
-
609
- if (newLink && newButton) {
610
- newLink.addEventListener('click', (e) => {
611
- e.preventDefault();
612
- newButton.click();
613
- });
614
- }
615
- """
616
- )
617
-
618
- # Assign hidden buttons to trigger sort methods
619
  hot_sort.click(
620
  fn=lambda: change_sort_method_ui("hot"),
621
  inputs=[],
 
190
  df_sorted = df
191
  elif self.sort_method == "new":
192
  df_sorted = df.sort_values(by='date', ascending=False) # Sort by 'date'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  else:
194
  df_sorted = df
195
 
 
201
 
202
  def set_sort_method(self, method, time_frame=None):
203
  """
204
+ Sets the sort method ('hot', 'new') and re-sorts the papers.
 
205
  """
206
+ if method not in ["hot", "new"]:
207
  method = "hot"
208
  logger.info(f"Setting sort method to: {method}")
209
  self.sort_method = method
 
 
 
210
  self.sort_papers()
211
  return True # Assume success
212
 
 
360
 
361
  # --- Gradio Interface Functions ---
362
 
363
+ def change_sort_method_ui(method: str) -> str:
364
  """
365
+ Changes the sort method based on user selection.
366
  """
367
+ logger.info(f"Changing sort method to: {method}")
368
+ paper_manager.set_sort_method(method.lower())
 
 
 
369
  return paper_manager.get_current_page_papers()
370
 
371
 
 
412
  text-decoration: none;
413
  }
414
 
415
+ .header-table .sort-links a {
416
+ color: black;
417
+ font-weight: normal;
418
+ font-size: 14pt;
419
+ margin-left: 15px;
420
+ cursor: pointer;
421
+ }
422
+
423
  .itemlist .athing {
424
  background-color: #f6f6ef;
425
  }
 
457
  font-size: 12pt;
458
  }
459
 
460
+ .sort-links a {
461
+ font-size: 12pt;
462
+ margin-left: 10px;
463
+ }
464
+
465
  .storylink {
466
  font-size: 9pt;
467
  }
 
494
  color: black;
495
  }
496
 
497
+ .header-table .sort-links a {
498
+ color: black;
499
+ }
500
+
501
  .itemlist .athing {
502
  background-color: #1e1e1e;
503
  }
 
545
  <b class="hnname"><a href="#">Daily Papers</a></b>
546
  </span>
547
  </td>
548
+ <td align="right" class="sort-links">
549
+ <a href="#" onclick="HotSort()">Hot</a> |
550
+ <a href="#" onclick="NewSort()">New</a>
 
 
551
  </td>
552
  </tr>
553
  </table>
554
+ <script>
555
+ function HotSort() {
556
+ gradioApp().getElementById('hot_sort_button').click();
557
+ }
558
+ function NewSort() {
559
+ gradioApp().getElementById('new_sort_button').click();
560
+ }
561
+ </script>
562
  """)
563
+ # Hidden buttons to trigger sort methods
564
+ hot_sort = gr.Button("Hot Sort", visible=False, elem_id="hot_sort_button")
565
+ new_sort = gr.Button("New Sort", visible=False, elem_id="new_sort_button")
 
 
566
  # Paper list
567
  paper_list = gr.HTML()
568
  # Navigation Buttons
 
580
  prev_button.click(paper_manager.prev_page, outputs=[paper_list])
581
  next_button.click(paper_manager.next_page, outputs=[paper_list])
582
 
583
+ # Hidden buttons trigger sort methods
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584
  hot_sort.click(
585
  fn=lambda: change_sort_method_ui("hot"),
586
  inputs=[],