Update app.py
Browse files
app.py
CHANGED
|
@@ -13,8 +13,14 @@ from utils.api_client import RewardPilotClient
|
|
| 13 |
from utils.formatters import (
|
| 14 |
format_full_recommendation,
|
| 15 |
format_comparison_table,
|
| 16 |
-
format_analytics_metrics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
|
|
|
| 18 |
|
| 19 |
# ===================== Initialize API client =====================
|
| 20 |
client = RewardPilotClient()
|
|
@@ -332,7 +338,7 @@ Get AI-powered credit card recommendations that maximize your rewards based on:
|
|
| 332 |
cache_examples=False
|
| 333 |
)
|
| 334 |
|
| 335 |
-
# ========== Tab 2: Analytics (
|
| 336 |
with gr.Tab("π Analytics"):
|
| 337 |
gr.Markdown("## π― Your Rewards Optimization Dashboard")
|
| 338 |
|
|
@@ -340,7 +346,7 @@ Get AI-powered credit card recommendations that maximize your rewards based on:
|
|
| 340 |
with gr.Row():
|
| 341 |
analytics_user = gr.Dropdown(
|
| 342 |
choices=SAMPLE_USERS,
|
| 343 |
-
value=SAMPLE_USERS[
|
| 344 |
label="π€ View Analytics For User",
|
| 345 |
scale=3
|
| 346 |
)
|
|
@@ -376,6 +382,32 @@ Get AI-powered credit card recommendations that maximize your rewards based on:
|
|
| 376 |
|
| 377 |
gr.Markdown("---")
|
| 378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
# Detailed Analytics (Dynamic)
|
| 380 |
with gr.Row():
|
| 381 |
with gr.Column(scale=1):
|
|
@@ -443,27 +475,70 @@ Get AI-powered credit card recommendations that maximize your rewards based on:
|
|
| 443 |
elem_classes=["status-text"]
|
| 444 |
)
|
| 445 |
|
| 446 |
-
# ===================== Analytics Update Function =====================
|
| 447 |
-
def
|
| 448 |
-
"""Fetch and format analytics for selected user"""
|
| 449 |
try:
|
| 450 |
# Fetch analytics data
|
| 451 |
analytics_data = client.get_user_analytics(user_id)
|
| 452 |
|
| 453 |
-
#
|
| 454 |
-
from utils.formatters import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
metrics_html, table_md, insights_md, forecast_md = format_analytics_metrics(analytics_data)
|
| 456 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
# Status message
|
| 458 |
from datetime import datetime
|
| 459 |
status = f"*Analytics updated for {user_id} at {datetime.now().strftime('%I:%M %p')}*"
|
| 460 |
|
| 461 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
except Exception as e:
|
| 464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
return (
|
| 466 |
"<p>Error loading metrics</p>",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
"Error loading table",
|
| 468 |
"Error loading insights",
|
| 469 |
"Error loading forecast",
|
|
@@ -472,10 +547,15 @@ Get AI-powered credit card recommendations that maximize your rewards based on:
|
|
| 472 |
|
| 473 |
# Connect analytics refresh to button and dropdown
|
| 474 |
refresh_analytics_btn.click(
|
| 475 |
-
fn=
|
| 476 |
inputs=[analytics_user],
|
| 477 |
outputs=[
|
| 478 |
metrics_display,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
spending_table,
|
| 480 |
insights_display,
|
| 481 |
forecast_display,
|
|
@@ -484,10 +564,15 @@ Get AI-powered credit card recommendations that maximize your rewards based on:
|
|
| 484 |
)
|
| 485 |
|
| 486 |
analytics_user.change(
|
| 487 |
-
fn=
|
| 488 |
inputs=[analytics_user],
|
| 489 |
outputs=[
|
| 490 |
metrics_display,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
spending_table,
|
| 492 |
insights_display,
|
| 493 |
forecast_display,
|
|
|
|
| 13 |
from utils.formatters import (
|
| 14 |
format_full_recommendation,
|
| 15 |
format_comparison_table,
|
| 16 |
+
format_analytics_metrics,
|
| 17 |
+
create_spending_chart,
|
| 18 |
+
create_rewards_pie_chart,
|
| 19 |
+
create_optimization_gauge,
|
| 20 |
+
create_trend_line_chart,
|
| 21 |
+
create_card_performance_chart
|
| 22 |
)
|
| 23 |
+
import plotly.graph_objects as go
|
| 24 |
|
| 25 |
# ===================== Initialize API client =====================
|
| 26 |
client = RewardPilotClient()
|
|
|
|
| 338 |
cache_examples=False
|
| 339 |
)
|
| 340 |
|
| 341 |
+
# ========== Tab 2: Analytics with Charts (ENHANCED) ==========
|
| 342 |
with gr.Tab("π Analytics"):
|
| 343 |
gr.Markdown("## π― Your Rewards Optimization Dashboard")
|
| 344 |
|
|
|
|
| 346 |
with gr.Row():
|
| 347 |
analytics_user = gr.Dropdown(
|
| 348 |
choices=SAMPLE_USERS,
|
| 349 |
+
value=SAMPLE_USERS,[object Object],,
|
| 350 |
label="π€ View Analytics For User",
|
| 351 |
scale=3
|
| 352 |
)
|
|
|
|
| 382 |
|
| 383 |
gr.Markdown("---")
|
| 384 |
|
| 385 |
+
# ========== CHARTS SECTION (NEW) ==========
|
| 386 |
+
gr.Markdown("## π Visual Analytics")
|
| 387 |
+
|
| 388 |
+
# Row 1: Spending Chart + Optimization Gauge
|
| 389 |
+
with gr.Row():
|
| 390 |
+
with gr.Column(scale=2):
|
| 391 |
+
spending_chart = gr.Plot(label="Spending vs Rewards")
|
| 392 |
+
with gr.Column(scale=1):
|
| 393 |
+
optimization_gauge = gr.Plot(label="Your Score")
|
| 394 |
+
|
| 395 |
+
# Row 2: Pie Chart + Card Performance
|
| 396 |
+
with gr.Row():
|
| 397 |
+
with gr.Column(scale=1):
|
| 398 |
+
rewards_pie_chart = gr.Plot(label="Rewards Distribution")
|
| 399 |
+
with gr.Column(scale=1):
|
| 400 |
+
card_performance_chart = gr.Plot(label="Top Performing Cards")
|
| 401 |
+
|
| 402 |
+
# Row 3: Trend Line Chart (Full Width)
|
| 403 |
+
with gr.Row():
|
| 404 |
+
trend_chart = gr.Plot(label="12-Month Trends")
|
| 405 |
+
|
| 406 |
+
gr.Markdown("---")
|
| 407 |
+
|
| 408 |
+
# ========== DATA TABLES SECTION ==========
|
| 409 |
+
gr.Markdown("## π Detailed Breakdown")
|
| 410 |
+
|
| 411 |
# Detailed Analytics (Dynamic)
|
| 412 |
with gr.Row():
|
| 413 |
with gr.Column(scale=1):
|
|
|
|
| 475 |
elem_classes=["status-text"]
|
| 476 |
)
|
| 477 |
|
| 478 |
+
# ===================== Analytics Update Function (ENHANCED) =====================
|
| 479 |
+
def update_analytics_with_charts(user_id: str) -> tuple:
|
| 480 |
+
"""Fetch and format analytics with charts for selected user"""
|
| 481 |
try:
|
| 482 |
# Fetch analytics data
|
| 483 |
analytics_data = client.get_user_analytics(user_id)
|
| 484 |
|
| 485 |
+
# Import chart functions
|
| 486 |
+
from utils.formatters import (
|
| 487 |
+
format_analytics_metrics,
|
| 488 |
+
create_spending_chart,
|
| 489 |
+
create_rewards_pie_chart,
|
| 490 |
+
create_optimization_gauge,
|
| 491 |
+
create_trend_line_chart,
|
| 492 |
+
create_card_performance_chart
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
+
# Format text data
|
| 496 |
metrics_html, table_md, insights_md, forecast_md = format_analytics_metrics(analytics_data)
|
| 497 |
|
| 498 |
+
# Generate charts
|
| 499 |
+
spending_fig = create_spending_chart(analytics_data)
|
| 500 |
+
pie_fig = create_rewards_pie_chart(analytics_data)
|
| 501 |
+
gauge_fig = create_optimization_gauge(analytics_data)
|
| 502 |
+
trend_fig = create_trend_line_chart(analytics_data)
|
| 503 |
+
performance_fig = create_card_performance_chart(analytics_data)
|
| 504 |
+
|
| 505 |
# Status message
|
| 506 |
from datetime import datetime
|
| 507 |
status = f"*Analytics updated for {user_id} at {datetime.now().strftime('%I:%M %p')}*"
|
| 508 |
|
| 509 |
+
return (
|
| 510 |
+
metrics_html, # Metric cards
|
| 511 |
+
spending_fig, # Spending bar chart
|
| 512 |
+
gauge_fig, # Optimization gauge
|
| 513 |
+
pie_fig, # Rewards pie chart
|
| 514 |
+
performance_fig, # Card performance chart
|
| 515 |
+
trend_fig, # Trend line chart
|
| 516 |
+
table_md, # Spending table
|
| 517 |
+
insights_md, # Insights text
|
| 518 |
+
forecast_md, # Forecast text
|
| 519 |
+
status # Status message
|
| 520 |
+
)
|
| 521 |
|
| 522 |
except Exception as e:
|
| 523 |
+
import traceback
|
| 524 |
+
error_msg = f"β Error loading analytics: {str(e)}\n\n{traceback.format_exc()}"
|
| 525 |
+
print(error_msg) # Debug print
|
| 526 |
+
|
| 527 |
+
# Return empty/error states
|
| 528 |
+
empty_fig = go.Figure()
|
| 529 |
+
empty_fig.add_annotation(
|
| 530 |
+
text="Error loading chart",
|
| 531 |
+
xref="paper", yref="paper",
|
| 532 |
+
x=0.5, y=0.5, showarrow=False
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
return (
|
| 536 |
"<p>Error loading metrics</p>",
|
| 537 |
+
empty_fig,
|
| 538 |
+
empty_fig,
|
| 539 |
+
empty_fig,
|
| 540 |
+
empty_fig,
|
| 541 |
+
empty_fig,
|
| 542 |
"Error loading table",
|
| 543 |
"Error loading insights",
|
| 544 |
"Error loading forecast",
|
|
|
|
| 547 |
|
| 548 |
# Connect analytics refresh to button and dropdown
|
| 549 |
refresh_analytics_btn.click(
|
| 550 |
+
fn=update_analytics_with_charts,
|
| 551 |
inputs=[analytics_user],
|
| 552 |
outputs=[
|
| 553 |
metrics_display,
|
| 554 |
+
spending_chart,
|
| 555 |
+
optimization_gauge,
|
| 556 |
+
rewards_pie_chart,
|
| 557 |
+
card_performance_chart,
|
| 558 |
+
trend_chart,
|
| 559 |
spending_table,
|
| 560 |
insights_display,
|
| 561 |
forecast_display,
|
|
|
|
| 564 |
)
|
| 565 |
|
| 566 |
analytics_user.change(
|
| 567 |
+
fn=update_analytics_with_charts,
|
| 568 |
inputs=[analytics_user],
|
| 569 |
outputs=[
|
| 570 |
metrics_display,
|
| 571 |
+
spending_chart,
|
| 572 |
+
optimization_gauge,
|
| 573 |
+
rewards_pie_chart,
|
| 574 |
+
card_performance_chart,
|
| 575 |
+
trend_chart,
|
| 576 |
spending_table,
|
| 577 |
insights_display,
|
| 578 |
forecast_display,
|