nbroad HF staff commited on
Commit
eafa97a
1 Parent(s): e172836

fix timestamp, pagination

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -3,6 +3,7 @@ from datetime import datetime, timedelta
3
  import requests
4
  from datetime import datetime
5
  import json
 
6
 
7
  from dotenv import load_dotenv
8
 
@@ -17,10 +18,14 @@ css_content = open("styles.css").read()
17
 
18
  app, rt = fast_app(hdrs=(Style(css_content),))
19
 
 
 
 
 
20
  def date_range_inputs(start_date, end_date):
21
  return Div(
22
- Input(type="date", name="start_date", value=start_date.strftime("%Y-%m-%d")),
23
- Input(type="date", name="end_date", value=end_date.strftime("%Y-%m-%d")),
24
  cls="date-range"
25
  )
26
 
@@ -38,9 +43,9 @@ def iso_to_unix_timestamp(iso_string):
38
  dt = datetime.fromisoformat(iso_string)
39
  return int(dt.timestamp())
40
 
41
- def unix_timestamp_to_iso(timestamp):
42
  dt = datetime.fromtimestamp(timestamp)
43
- return dt.isoformat()
44
 
45
  def make_query(query, start_date, end_date, page=1, limit=10):
46
  url = f"{API_URL}/indexes/comments/search"
@@ -90,13 +95,13 @@ def make_results_bar(results):
90
  def make_card(result):
91
  result = result["_formatted"]
92
  url = f"https://hf.co/{result['repo_id']}/discussions/{result['discussion_num']}"
93
- date = unix_timestamp_to_iso(int(result["comment_updatedAt_timestamp"]))
94
 
95
  return Div(
96
  Div(
97
  Strong(NotStr(result["discussion_title"])),
98
  P(NotStr(result["comment_text"]), cls="comment-text"),
99
- Span(date),
100
  A(url, href=url, target="_blank"),
101
  ),
102
  cls="card-item"
@@ -104,30 +109,29 @@ def make_card(result):
104
 
105
  def make_pagination(query, start_date, end_date, current_page, total_hits, limit=10):
106
  total_pages = -(-total_hits // limit) # Ceiling division
107
-
108
- pagination = Div(cls="pagination")
109
 
110
  if current_page > 1:
111
- pagination.children += tuple(
112
  Button("Previous",
113
  hx_post=f"/search?page={current_page-1}",
114
  hx_target="#search-results",
115
  hx_include="[name='query'], [name='start_date'], [name='end_date']")
116
  )
117
 
118
- pagination.children += tuple(Span(f"Page {current_page} of {total_pages}"))
119
 
120
  if current_page < total_pages:
121
- pagination.children += tuple(
122
  Button("Next",
123
  hx_post=f"/search?page={current_page+1}",
124
  hx_target="#search-results",
125
  hx_include="[name='query'], [name='start_date'], [name='end_date']",
126
-
127
  )
128
  )
129
 
130
- return pagination
131
 
132
  @rt("/")
133
  def get():
 
3
  import requests
4
  from datetime import datetime
5
  import json
6
+ from markdown import markdown
7
 
8
  from dotenv import load_dotenv
9
 
 
18
 
19
  app, rt = fast_app(hdrs=(Style(css_content),))
20
 
21
+
22
+ md_exts='codehilite', 'smarty', 'extra', 'sane_lists'
23
+ def Markdown(s, exts=md_exts, **kw): return Div(NotStr(markdown(s, extensions=exts)), **kw)
24
+
25
  def date_range_inputs(start_date, end_date):
26
  return Div(
27
+ Input(type="date", name="start_date", value=start_date.strftime("%Y-%m-%d"), title="Start date"),
28
+ Input(type="date", name="end_date", value=end_date.strftime("%Y-%m-%d"), title="End date"),
29
  cls="date-range"
30
  )
31
 
 
43
  dt = datetime.fromisoformat(iso_string)
44
  return int(dt.timestamp())
45
 
46
+ def unix_timestamp_to_nice_format(timestamp):
47
  dt = datetime.fromtimestamp(timestamp)
48
+ return dt.strftime("%b %d, %Y")
49
 
50
  def make_query(query, start_date, end_date, page=1, limit=10):
51
  url = f"{API_URL}/indexes/comments/search"
 
95
  def make_card(result):
96
  result = result["_formatted"]
97
  url = f"https://hf.co/{result['repo_id']}/discussions/{result['discussion_num']}"
98
+ date = unix_timestamp_to_nice_format(int(result["comment_updatedAt_timestamp"]))
99
 
100
  return Div(
101
  Div(
102
  Strong(NotStr(result["discussion_title"])),
103
  P(NotStr(result["comment_text"]), cls="comment-text"),
104
+ Div(Span(date)),
105
  A(url, href=url, target="_blank"),
106
  ),
107
  cls="card-item"
 
109
 
110
  def make_pagination(query, start_date, end_date, current_page, total_hits, limit=10):
111
  total_pages = -(-total_hits // limit) # Ceiling division
112
+
113
+ children = []
114
 
115
  if current_page > 1:
116
+ children.append(
117
  Button("Previous",
118
  hx_post=f"/search?page={current_page-1}",
119
  hx_target="#search-results",
120
  hx_include="[name='query'], [name='start_date'], [name='end_date']")
121
  )
122
 
123
+ children.append(Span(f"Page {current_page} of {total_pages}"))
124
 
125
  if current_page < total_pages:
126
+ children.append(
127
  Button("Next",
128
  hx_post=f"/search?page={current_page+1}",
129
  hx_target="#search-results",
130
  hx_include="[name='query'], [name='start_date'], [name='end_date']",
 
131
  )
132
  )
133
 
134
+ return Div(*children, cls="pagination")
135
 
136
  @rt("/")
137
  def get():