Spaces:
Running
Running
Improve analytics display and fix page load refresh
Browse files- Improve chart formatting with better visibility (14 days, rotated labels)
- Add demo.load() to refresh analytics on page load
- Add api_name=False to prevent lambda from being exposed as MCP tool
- Ensures users see current request counts without needing to search first
- analytics.py +9 -2
- app.py +17 -14
analytics.py
CHANGED
|
@@ -33,6 +33,13 @@ def last_n_days_df(n: int = 30) -> pd.DataFrame:
|
|
| 33 |
data = _load()
|
| 34 |
records = []
|
| 35 |
for i in range(n):
|
| 36 |
-
day = (now - timedelta(days=n - 1 - i))
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return pd.DataFrame(records)
|
|
|
|
| 33 |
data = _load()
|
| 34 |
records = []
|
| 35 |
for i in range(n):
|
| 36 |
+
day = (now - timedelta(days=n - 1 - i))
|
| 37 |
+
day_str = day.strftime("%Y-%m-%d")
|
| 38 |
+
# Format date for display (MMM DD)
|
| 39 |
+
display_date = day.strftime("%b %d")
|
| 40 |
+
records.append({
|
| 41 |
+
"date": display_date,
|
| 42 |
+
"count": data.get(day_str, 0),
|
| 43 |
+
"full_date": day_str # Keep full date for tooltip
|
| 44 |
+
})
|
| 45 |
return pd.DataFrame(records)
|
app.py
CHANGED
|
@@ -188,7 +188,7 @@ async def search_web(
|
|
| 188 |
|
| 189 |
async def search_and_log(query, search_type, num_results):
|
| 190 |
text = await search_web(query, search_type, num_results)
|
| 191 |
-
chart_df = last_n_days_df()
|
| 192 |
return text, chart_df
|
| 193 |
|
| 194 |
|
|
@@ -267,17 +267,6 @@ with gr.Blocks(title="Web Search MCP Server") as demo:
|
|
| 267 |
info="The extracted article content will appear here",
|
| 268 |
)
|
| 269 |
|
| 270 |
-
# ββββββββββββββββββββββββββββ NEW ββββββββββββββββββββββββββββ
|
| 271 |
-
requests_plot = gr.BarPlot(
|
| 272 |
-
value=last_n_days_df(), # initial empty/old data
|
| 273 |
-
x="date",
|
| 274 |
-
y="count",
|
| 275 |
-
title="Requests per Day (Last 30 days)",
|
| 276 |
-
tooltip="all",
|
| 277 |
-
height=320, # tweak to taste
|
| 278 |
-
)
|
| 279 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 280 |
-
|
| 281 |
# Add examples
|
| 282 |
gr.Examples(
|
| 283 |
examples=[
|
|
@@ -293,12 +282,26 @@ with gr.Blocks(title="Web Search MCP Server") as demo:
|
|
| 293 |
cache_examples=False,
|
| 294 |
)
|
| 295 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
search_button.click(
|
| 297 |
-
fn=search_and_log,
|
| 298 |
inputs=[query_input, search_type_input, num_results_input],
|
| 299 |
-
outputs=[output, requests_plot],
|
| 300 |
)
|
| 301 |
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
if __name__ == "__main__":
|
| 304 |
# Launch with MCP server enabled
|
|
|
|
| 188 |
|
| 189 |
async def search_and_log(query, search_type, num_results):
|
| 190 |
text = await search_web(query, search_type, num_results)
|
| 191 |
+
chart_df = last_n_days_df(14) # Show last 14 days
|
| 192 |
return text, chart_df
|
| 193 |
|
| 194 |
|
|
|
|
| 267 |
info="The extracted article content will appear here",
|
| 268 |
)
|
| 269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
# Add examples
|
| 271 |
gr.Examples(
|
| 272 |
examples=[
|
|
|
|
| 282 |
cache_examples=False,
|
| 283 |
)
|
| 284 |
|
| 285 |
+
requests_plot = gr.BarPlot(
|
| 286 |
+
value=last_n_days_df(14), # Show only last 14 days for better visibility
|
| 287 |
+
x="date",
|
| 288 |
+
y="count",
|
| 289 |
+
title="Daily Community Request Count",
|
| 290 |
+
tooltip=["date", "count"],
|
| 291 |
+
height=280,
|
| 292 |
+
x_label_angle=-45, # Rotate labels to prevent overlap
|
| 293 |
+
container=False,
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
search_button.click(
|
| 297 |
+
fn=search_and_log, # wrapper
|
| 298 |
inputs=[query_input, search_type_input, num_results_input],
|
| 299 |
+
outputs=[output, requests_plot], # update both
|
| 300 |
)
|
| 301 |
|
| 302 |
+
# Load fresh analytics data when the page loads
|
| 303 |
+
demo.load(fn=lambda: last_n_days_df(14), outputs=requests_plot, api_name=False)
|
| 304 |
+
|
| 305 |
|
| 306 |
if __name__ == "__main__":
|
| 307 |
# Launch with MCP server enabled
|