Spaces:
Running
Running
add scroll to top button
Browse files
app.py
CHANGED
@@ -19,16 +19,51 @@ css_content = open("styles.css").read()
|
|
19 |
app, rt = fast_app(hdrs=(Style(css_content),))
|
20 |
|
21 |
|
22 |
-
md_exts=
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def date_range_inputs(start_date, end_date):
|
26 |
return Div(
|
27 |
-
Input(
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
)
|
31 |
|
|
|
32 |
def search_form(start_date, end_date):
|
33 |
return Form(
|
34 |
Input(type="text", name="query", placeholder="Enter search query"),
|
@@ -36,24 +71,28 @@ def search_form(start_date, end_date):
|
|
36 |
Button("Search", type="submit"),
|
37 |
hx_post="/search",
|
38 |
hx_target="#search-results",
|
39 |
-
hx_trigger="submit"
|
|
|
40 |
)
|
41 |
|
|
|
42 |
def iso_to_unix_timestamp(iso_string):
|
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"
|
52 |
headers = {
|
53 |
"Content-Type": "application/json",
|
54 |
"Authorization": f"Bearer {API_KEY}",
|
55 |
}
|
56 |
-
|
57 |
after_timestamp = iso_to_unix_timestamp(start_date)
|
58 |
before_timestamp = iso_to_unix_timestamp(end_date)
|
59 |
|
@@ -62,36 +101,41 @@ def make_query(query, start_date, end_date, page=1, limit=10):
|
|
62 |
"limit": limit,
|
63 |
"offset": (page - 1) * limit,
|
64 |
"filter": f"comment_updatedAt_timestamp >= {after_timestamp} AND comment_updatedAt_timestamp < {before_timestamp}",
|
65 |
-
"attributesToCrop": [
|
66 |
"cropLength": 30,
|
67 |
"attributesToHighlight": ["comment_text", "discussion_title"],
|
68 |
-
"highlightPreTag":
|
69 |
-
"highlightPostTag": "</span>"
|
70 |
}
|
71 |
|
72 |
response = requests.post(url, headers=headers, json=query)
|
73 |
|
74 |
return response.json()
|
75 |
|
|
|
76 |
def search_results(query, start_date, end_date, page=1):
|
77 |
raw_results = make_query(query, start_date, end_date, page)
|
78 |
|
79 |
return Div(
|
80 |
make_results_bar(raw_results),
|
81 |
Div(*[make_card(r) for r in raw_results["hits"]]),
|
82 |
-
make_pagination(
|
83 |
-
|
|
|
|
|
84 |
)
|
85 |
|
|
|
86 |
def make_results_bar(results):
|
87 |
processing_time = results["processingTimeMs"]
|
88 |
estimated_hits = results["estimatedTotalHits"]
|
89 |
return Div(
|
90 |
Div(f"Processing time: {processing_time}ms"),
|
91 |
Div(f"Estimated total hits: {estimated_hits}"),
|
92 |
-
cls="results-bar"
|
93 |
)
|
94 |
|
|
|
95 |
def make_card(result):
|
96 |
result = result["_formatted"]
|
97 |
url = f"https://hf.co/{result['repo_id']}/discussions/{result['discussion_num']}"
|
@@ -104,35 +148,55 @@ def make_card(result):
|
|
104 |
Div(Span(date)),
|
105 |
A(url, href=url, target="_blank"),
|
106 |
),
|
107 |
-
cls="card-item"
|
108 |
)
|
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 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
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(
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
132 |
)
|
133 |
-
|
134 |
return Div(*children, cls="pagination")
|
135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
@rt("/")
|
137 |
def get():
|
138 |
end_date = datetime.now()
|
@@ -142,12 +206,16 @@ def get():
|
|
142 |
Div(
|
143 |
search_form(start_date, end_date),
|
144 |
Div(id="search-results"),
|
145 |
-
|
146 |
-
|
|
|
|
|
147 |
)
|
148 |
|
|
|
149 |
@rt("/search")
|
150 |
def post(query: str, start_date: str, end_date: str, page: int = 1):
|
151 |
return search_results(query, start_date, end_date, page)
|
152 |
|
|
|
153 |
serve()
|
|
|
19 |
app, rt = fast_app(hdrs=(Style(css_content),))
|
20 |
|
21 |
|
22 |
+
md_exts = "codehilite", "smarty", "extra", "sane_lists"
|
23 |
+
|
24 |
+
|
25 |
+
def Markdown(s, exts=md_exts, **kw):
|
26 |
+
return Div(NotStr(markdown(s, extensions=exts)), **kw)
|
27 |
+
|
28 |
+
|
29 |
+
scroll_script = Script("""
|
30 |
+
document.addEventListener('DOMContentLoaded', function() {
|
31 |
+
var scrollButton = document.getElementById('scroll-top-btn');
|
32 |
+
|
33 |
+
window.onscroll = function() {
|
34 |
+
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
|
35 |
+
scrollButton.style.display = "block";
|
36 |
+
} else {
|
37 |
+
scrollButton.style.display = "none";
|
38 |
+
}
|
39 |
+
};
|
40 |
+
|
41 |
+
scrollButton.onclick = function() {
|
42 |
+
document.body.scrollTop = 0; // For Safari
|
43 |
+
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
|
44 |
+
};
|
45 |
+
});
|
46 |
+
""")
|
47 |
+
|
48 |
|
49 |
def date_range_inputs(start_date, end_date):
|
50 |
return Div(
|
51 |
+
Input(
|
52 |
+
type="date",
|
53 |
+
name="start_date",
|
54 |
+
value=start_date.strftime("%Y-%m-%d"),
|
55 |
+
title="Start date",
|
56 |
+
),
|
57 |
+
Input(
|
58 |
+
type="date",
|
59 |
+
name="end_date",
|
60 |
+
value=end_date.strftime("%Y-%m-%d"),
|
61 |
+
title="End date",
|
62 |
+
),
|
63 |
+
cls="date-range",
|
64 |
)
|
65 |
|
66 |
+
|
67 |
def search_form(start_date, end_date):
|
68 |
return Form(
|
69 |
Input(type="text", name="query", placeholder="Enter search query"),
|
|
|
71 |
Button("Search", type="submit"),
|
72 |
hx_post="/search",
|
73 |
hx_target="#search-results",
|
74 |
+
hx_trigger="submit",
|
75 |
+
id="search-form",
|
76 |
)
|
77 |
|
78 |
+
|
79 |
def iso_to_unix_timestamp(iso_string):
|
80 |
dt = datetime.fromisoformat(iso_string)
|
81 |
return int(dt.timestamp())
|
82 |
|
83 |
+
|
84 |
def unix_timestamp_to_nice_format(timestamp):
|
85 |
dt = datetime.fromtimestamp(timestamp)
|
86 |
return dt.strftime("%b %d, %Y")
|
87 |
|
88 |
+
|
89 |
def make_query(query, start_date, end_date, page=1, limit=10):
|
90 |
url = f"{API_URL}/indexes/comments/search"
|
91 |
headers = {
|
92 |
"Content-Type": "application/json",
|
93 |
"Authorization": f"Bearer {API_KEY}",
|
94 |
}
|
95 |
+
|
96 |
after_timestamp = iso_to_unix_timestamp(start_date)
|
97 |
before_timestamp = iso_to_unix_timestamp(end_date)
|
98 |
|
|
|
101 |
"limit": limit,
|
102 |
"offset": (page - 1) * limit,
|
103 |
"filter": f"comment_updatedAt_timestamp >= {after_timestamp} AND comment_updatedAt_timestamp < {before_timestamp}",
|
104 |
+
"attributesToCrop": ["comment_text"],
|
105 |
"cropLength": 30,
|
106 |
"attributesToHighlight": ["comment_text", "discussion_title"],
|
107 |
+
"highlightPreTag": '<span class="highlight">',
|
108 |
+
"highlightPostTag": "</span>",
|
109 |
}
|
110 |
|
111 |
response = requests.post(url, headers=headers, json=query)
|
112 |
|
113 |
return response.json()
|
114 |
|
115 |
+
|
116 |
def search_results(query, start_date, end_date, page=1):
|
117 |
raw_results = make_query(query, start_date, end_date, page)
|
118 |
|
119 |
return Div(
|
120 |
make_results_bar(raw_results),
|
121 |
Div(*[make_card(r) for r in raw_results["hits"]]),
|
122 |
+
make_pagination(
|
123 |
+
query, start_date, end_date, page, raw_results["estimatedTotalHits"]
|
124 |
+
),
|
125 |
+
id="search-results",
|
126 |
)
|
127 |
|
128 |
+
|
129 |
def make_results_bar(results):
|
130 |
processing_time = results["processingTimeMs"]
|
131 |
estimated_hits = results["estimatedTotalHits"]
|
132 |
return Div(
|
133 |
Div(f"Processing time: {processing_time}ms"),
|
134 |
Div(f"Estimated total hits: {estimated_hits}"),
|
135 |
+
cls="results-bar",
|
136 |
)
|
137 |
|
138 |
+
|
139 |
def make_card(result):
|
140 |
result = result["_formatted"]
|
141 |
url = f"https://hf.co/{result['repo_id']}/discussions/{result['discussion_num']}"
|
|
|
148 |
Div(Span(date)),
|
149 |
A(url, href=url, target="_blank"),
|
150 |
),
|
151 |
+
cls="card-item",
|
152 |
)
|
153 |
|
154 |
+
|
155 |
def make_pagination(query, start_date, end_date, current_page, total_hits, limit=10):
|
156 |
total_pages = -(-total_hits // limit) # Ceiling division
|
157 |
|
158 |
children = []
|
159 |
+
|
160 |
if current_page > 1:
|
161 |
children.append(
|
162 |
+
Button(
|
163 |
+
"Previous",
|
164 |
+
hx_post=f"/search?page={current_page-1}",
|
165 |
+
hx_target="#search-results",
|
166 |
+
hx_include="[name='query'], [name='start_date'], [name='end_date']",
|
167 |
+
)
|
168 |
+
)
|
169 |
+
|
170 |
children.append(Span(f"Page {current_page} of {total_pages}"))
|
171 |
+
|
172 |
if current_page < total_pages:
|
173 |
children.append(
|
174 |
+
Button(
|
175 |
+
"Next",
|
176 |
+
hx_post=f"/search?page={current_page+1}",
|
177 |
+
hx_target="#search-results",
|
178 |
+
hx_include="[name='query'], [name='start_date'], [name='end_date']",
|
179 |
+
)
|
180 |
)
|
181 |
+
|
182 |
return Div(*children, cls="pagination")
|
183 |
|
184 |
+
scroll_button = Button("Scroll to Top",
|
185 |
+
id="scroll-top-btn",
|
186 |
+
style="""
|
187 |
+
position: fixed;
|
188 |
+
bottom: 20px;
|
189 |
+
right: 20px;
|
190 |
+
display: none;
|
191 |
+
background-color: #007bff;
|
192 |
+
color: white;
|
193 |
+
border: none;
|
194 |
+
border-radius: 5px;
|
195 |
+
padding: 10px 15px;
|
196 |
+
cursor: pointer;
|
197 |
+
"""
|
198 |
+
)
|
199 |
+
|
200 |
@rt("/")
|
201 |
def get():
|
202 |
end_date = datetime.now()
|
|
|
206 |
Div(
|
207 |
search_form(start_date, end_date),
|
208 |
Div(id="search-results"),
|
209 |
+
scroll_button,
|
210 |
+
scroll_script,
|
211 |
+
cls="container",
|
212 |
+
),
|
213 |
)
|
214 |
|
215 |
+
|
216 |
@rt("/search")
|
217 |
def post(query: str, start_date: str, end_date: str, page: int = 1):
|
218 |
return search_results(query, start_date, end_date, page)
|
219 |
|
220 |
+
|
221 |
serve()
|