akhaliq HF staff commited on
Commit
4681e49
·
verified ·
1 Parent(s): e800f6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -37
app.py CHANGED
@@ -11,7 +11,7 @@ class PaperManager:
11
  self.papers = []
12
  self.total_pages = 1
13
 
14
- def fetch_initial_papers(self):
15
  try:
16
  response = requests.get(f"{API_URL}?limit=50")
17
  response.raise_for_status()
@@ -19,6 +19,7 @@ class PaperManager:
19
  # Sort papers by upvotes (descending order)
20
  self.papers = sorted(data, key=lambda x: x.get('paper', {}).get('upvotes', 0), reverse=True)
21
  self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
 
22
  return True
23
  except requests.RequestException as e:
24
  print(f"Error fetching papers: {e}")
@@ -33,12 +34,17 @@ class PaperManager:
33
  published_time = datetime.fromisoformat(paper.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00'))
34
  time_ago = (datetime.now(timezone.utc) - published_time).days
35
 
36
- return f"""<div style='border-bottom: 1px solid #eee; padding: 10px 0;'>
37
- <a href='{url}' target='_blank' style='color: #000; text-decoration: none; font-weight: bold;'>{title}</a>
38
- <div style='font-size: 0.8em; color: #666; margin-top: 5px;'>
39
- {upvotes} upvotes | by {authors} | {time_ago} days ago | {comments} comments
 
 
 
 
 
40
  </div>
41
- </div>"""
42
 
43
  def render_papers(self):
44
  start = (self.current_page - 1) * self.papers_per_page
@@ -46,7 +52,7 @@ class PaperManager:
46
  current_papers = self.papers[start:end]
47
 
48
  if not current_papers:
49
- return "<div>No papers available for this page.</div>"
50
 
51
  return "".join([self.format_paper(paper) for paper in current_papers])
52
 
@@ -69,63 +75,98 @@ class PaperManager:
69
  return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
70
 
71
  css = """
72
- html, body {
73
- height: 100%;
 
74
  margin: 0;
75
  padding: 0;
76
- display: flex;
77
- justify-content: center;
78
- align-items: center;
79
- background-color: #f0f0f0;
80
  }
81
  .container {
82
- font-family: Arial, sans-serif;
83
  max-width: 800px;
84
- width: 100%;
85
  background-color: white;
86
- padding: 20px;
87
- border-radius: 10px;
88
- box-shadow: 0 0 10px rgba(0,0,0,0.1);
89
  }
90
- .paper-list {
91
- max-height: 400px;
92
- overflow-y: auto;
93
- border: 1px solid #eee;
94
- border-radius: 5px;
95
- padding: 10px;
96
- margin-bottom: 10px;
97
  }
98
  .search-row {
99
  display: flex;
100
- gap: 10px;
101
- margin-bottom: 20px;
102
  }
103
- .title {
104
- text-align: center;
105
- color: #333;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  }
107
  .footer {
108
  display: flex;
109
  justify-content: space-between;
110
  align-items: center;
111
- margin-top: 10px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  }
113
  """
114
 
115
  paper_manager = PaperManager()
116
 
117
  def initialize_app():
118
- if paper_manager.fetch_initial_papers():
119
  return paper_manager.render_papers(), f"Page {paper_manager.current_page} of {paper_manager.total_pages}"
120
  else:
121
- return "<div>Failed to fetch papers. Please try again later.</div>", "Error"
122
 
123
  def refresh_papers():
124
- paper_manager.current_page = 1
125
- if paper_manager.fetch_initial_papers():
126
  return paper_manager.render_papers(), f"Page {paper_manager.current_page} of {paper_manager.total_pages}"
127
  else:
128
- return "<div>Failed to refresh papers. Please try again later.</div>", "Error"
129
 
130
  demo = gr.Blocks(css=css)
131
 
@@ -134,7 +175,7 @@ with demo:
134
  gr.Markdown("# Daily Papers - HackerNews Style", elem_classes=["title"])
135
  with gr.Row(elem_classes=["search-row"]):
136
  search_input = gr.Textbox(label="Search papers", placeholder="Enter search term...")
137
- refresh_button = gr.Button("Refresh")
138
  paper_list = gr.HTML(elem_classes=["paper-list"])
139
 
140
  with gr.Row(elem_classes=["footer"]):
 
11
  self.papers = []
12
  self.total_pages = 1
13
 
14
+ def fetch_papers(self):
15
  try:
16
  response = requests.get(f"{API_URL}?limit=50")
17
  response.raise_for_status()
 
19
  # Sort papers by upvotes (descending order)
20
  self.papers = sorted(data, key=lambda x: x.get('paper', {}).get('upvotes', 0), reverse=True)
21
  self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
22
+ self.current_page = 1 # Reset to first page after fetching
23
  return True
24
  except requests.RequestException as e:
25
  print(f"Error fetching papers: {e}")
 
34
  published_time = datetime.fromisoformat(paper.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00'))
35
  time_ago = (datetime.now(timezone.utc) - published_time).days
36
 
37
+ return f"""
38
+ <div class="paper-item">
39
+ <h3><a href="{url}" target="_blank">{title}</a></h3>
40
+ <p class="paper-meta">
41
+ <span class="upvotes">{upvotes} upvotes</span> |
42
+ <span class="authors">by {authors}</span> |
43
+ <span class="time-ago">{time_ago} days ago</span> |
44
+ <span class="comments">{comments} comments</span>
45
+ </p>
46
  </div>
47
+ """
48
 
49
  def render_papers(self):
50
  start = (self.current_page - 1) * self.papers_per_page
 
52
  current_papers = self.papers[start:end]
53
 
54
  if not current_papers:
55
+ return "<div class='no-papers'>No papers available for this page.</div>"
56
 
57
  return "".join([self.format_paper(paper) for paper in current_papers])
58
 
 
75
  return self.render_papers(), f"Page {self.current_page} of {self.total_pages}"
76
 
77
  css = """
78
+ body {
79
+ font-family: 'Roboto', sans-serif;
80
+ background-color: #f0f2f5;
81
  margin: 0;
82
  padding: 0;
 
 
 
 
83
  }
84
  .container {
 
85
  max-width: 800px;
86
+ margin: 2rem auto;
87
  background-color: white;
88
+ border-radius: 8px;
89
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
90
+ padding: 2rem;
91
  }
92
+ .title {
93
+ text-align: center;
94
+ color: #1a202c;
95
+ font-size: 2rem;
96
+ margin-bottom: 1.5rem;
 
 
97
  }
98
  .search-row {
99
  display: flex;
100
+ gap: 1rem;
101
+ margin-bottom: 1.5rem;
102
  }
103
+ .paper-list {
104
+ max-height: 500px;
105
+ overflow-y: auto;
106
+ border: 1px solid #e2e8f0;
107
+ border-radius: 8px;
108
+ padding: 1rem;
109
+ }
110
+ .paper-item {
111
+ border-bottom: 1px solid #e2e8f0;
112
+ padding: 1rem 0;
113
+ }
114
+ .paper-item:last-child {
115
+ border-bottom: none;
116
+ }
117
+ .paper-item h3 {
118
+ margin: 0 0 0.5rem 0;
119
+ }
120
+ .paper-item a {
121
+ color: #2b6cb0;
122
+ text-decoration: none;
123
+ font-weight: 600;
124
+ }
125
+ .paper-item a:hover {
126
+ text-decoration: underline;
127
+ }
128
+ .paper-meta {
129
+ font-size: 0.875rem;
130
+ color: #4a5568;
131
  }
132
  .footer {
133
  display: flex;
134
  justify-content: space-between;
135
  align-items: center;
136
+ margin-top: 1rem;
137
+ }
138
+ button {
139
+ background-color: #4299e1;
140
+ color: white;
141
+ border: none;
142
+ padding: 0.5rem 1rem;
143
+ border-radius: 4px;
144
+ cursor: pointer;
145
+ transition: background-color 0.3s;
146
+ }
147
+ button:hover {
148
+ background-color: #3182ce;
149
+ }
150
+ .no-papers {
151
+ text-align: center;
152
+ color: #718096;
153
+ padding: 1rem;
154
  }
155
  """
156
 
157
  paper_manager = PaperManager()
158
 
159
  def initialize_app():
160
+ if paper_manager.fetch_papers():
161
  return paper_manager.render_papers(), f"Page {paper_manager.current_page} of {paper_manager.total_pages}"
162
  else:
163
+ return "<div class='no-papers'>Failed to fetch papers. Please try again later.</div>", "Error"
164
 
165
  def refresh_papers():
166
+ if paper_manager.fetch_papers():
 
167
  return paper_manager.render_papers(), f"Page {paper_manager.current_page} of {paper_manager.total_pages}"
168
  else:
169
+ return "<div class='no-papers'>Failed to refresh papers. Please try again later.</div>", "Error"
170
 
171
  demo = gr.Blocks(css=css)
172
 
 
175
  gr.Markdown("# Daily Papers - HackerNews Style", elem_classes=["title"])
176
  with gr.Row(elem_classes=["search-row"]):
177
  search_input = gr.Textbox(label="Search papers", placeholder="Enter search term...")
178
+ refresh_button = gr.Button("Refresh", variant="primary")
179
  paper_list = gr.HTML(elem_classes=["paper-list"])
180
 
181
  with gr.Row(elem_classes=["footer"]):