ps1811 commited on
Commit
22fc55e
·
verified ·
1 Parent(s): bc99da9

updated dashboard.py

Browse files
Files changed (1) hide show
  1. app/ui/dashboard.py +9 -38
app/ui/dashboard.py CHANGED
@@ -2,24 +2,17 @@ import gradio as gr
2
  import pandas as pd
3
  from app.controller.session_loader import load_google_ads_data
4
 
5
- # DATA LOADER
6
- # -------------------------
7
- def load_dashboard():
8
  dfs = load_google_ads_data()
9
  df = dfs["campaigns"].copy()
10
 
11
  if df.empty:
12
- return (
13
- 0, 0, 0, 0,
14
- pd.DataFrame(columns=[
15
- "Campaign", "Spend", "Leads", "CPL", "CTR"
16
- ])
17
- )
18
 
19
- # derive missing fields safely
20
- df["leads"] = df["conversions"] if "conversions" in df.columns else 0
21
  df["cpl"] = df["cost"] / df["leads"].replace(0, 1)
22
- df["ctr"] = df["ctr"]
23
 
24
  formatted = pd.DataFrame({
25
  "Campaign": df["name"],
@@ -37,33 +30,11 @@ def load_dashboard():
37
  formatted
38
  )
39
 
40
- # UI BUILDER
41
- # -------------------------
42
- def build_dashboard():
43
- gr.Markdown("## Campaign Dashboard")
44
-
45
- with gr.Row():
46
- total_spend = gr.Number(label="Total Spend")
47
- total_leads = gr.Number(label="Total Leads")
48
- average_cpl = gr.Number(label="Average CPL")
49
- active_campaigns = gr.Number(label="Active Campaigns")
50
 
 
 
51
  campaign_table = gr.Dataframe(
52
  label="Campaign Performance",
53
- interactive=True # IMPORTANT: needed for row click
54
  )
55
-
56
- refresh_btn = gr.Button("Refresh Dashboard")
57
- refresh_btn.click(
58
- fn=load_dashboard,
59
- outputs=[
60
- total_spend,
61
- total_leads,
62
- average_cpl,
63
- active_campaigns,
64
- campaign_table,
65
- ],
66
- )
67
-
68
- # ✅ IMPORTANT FIX: return table so main.py can attach .select()
69
- return campaign_table
 
2
  import pandas as pd
3
  from app.controller.session_loader import load_google_ads_data
4
 
5
+
6
+ def get_dashboard_data():
 
7
  dfs = load_google_ads_data()
8
  df = dfs["campaigns"].copy()
9
 
10
  if df.empty:
11
+ return 0, 0, 0, 0, pd.DataFrame(columns=["Campaign", "Spend", "Leads", "CPL", "CTR"])
 
 
 
 
 
12
 
13
+ df["leads"] = df.get("conversions", 0)
 
14
  df["cpl"] = df["cost"] / df["leads"].replace(0, 1)
15
+ df["ctr"] = df.get("ctr", 0)
16
 
17
  formatted = pd.DataFrame({
18
  "Campaign": df["name"],
 
30
  formatted
31
  )
32
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ def build_dashboard():
35
+ # Retained exactly for backend structural lookups if referenced elsewhere
36
  campaign_table = gr.Dataframe(
37
  label="Campaign Performance",
38
+ interactive=True
39
  )
40
+ return campaign_table