IAMTFRMZA commited on
Commit
268cc18
Β·
verified Β·
1 Parent(s): fc5dce8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -48
app.py CHANGED
@@ -2,7 +2,7 @@ import pandas as pd
2
  import gspread
3
  import gradio as gr
4
  from oauth2client.service_account import ServiceAccountCredentials
5
- from datetime import datetime
6
 
7
  # ------------------ AUTH ------------------
8
  VALID_USERS = {
@@ -18,39 +18,77 @@ creds = ServiceAccountCredentials.from_json_keyfile_name("deep-mile-461309-t8-0e
18
  client = gspread.authorize(creds)
19
  sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
20
 
21
- # ------------------ SHEET REFRESH FUNCTIONS ------------------
22
  def load_sheet(sheet_name):
23
  sheet = client.open_by_url(sheet_url).worksheet(sheet_name)
24
- data = sheet.get_all_records()
25
- df = pd.DataFrame(data)
26
  return df
27
 
28
- # ------------------ REPORTS TAB ------------------
29
- def filter_calls_by_date(date):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  df = load_sheet("Calls")
31
- df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date
32
- return df[df['Date'] == date]
 
 
 
 
 
 
 
 
33
 
34
- def filter_appointments_by_date(date):
35
  df = load_sheet("Appointments")
36
- df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date
37
- return df[df['Date'] == date]
38
-
39
- # ------------------ APPOINTED LEADS ------------------
40
- def appointed_leads_table():
41
- df = load_sheet("Appointed Leads")
42
- grouped = df.groupby('Rep')['Customer Name'].apply(list).reset_index()
43
- return grouped
44
-
45
- # ------------------ INTERACTIVE QUERY VIEW ------------------
46
- def search_table(sheet_name, field, keyword):
47
- df = load_sheet(sheet_name)
48
- if field not in df.columns:
49
- return pd.DataFrame(), "Field not found."
50
- results = df[df[field].astype(str).str.contains(keyword, case=False, na=False)]
51
- return results, f"Found {len(results)} results."
52
-
53
- # ------------------ GRADIO UI ------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  with gr.Blocks() as app:
55
  with gr.Row():
56
  with gr.Column(visible=True) as login_ui:
@@ -61,31 +99,28 @@ with gr.Blocks() as app:
61
  login_msg = gr.Markdown("")
62
 
63
  with gr.Column(visible=False) as main_ui:
64
- gr.Markdown("## πŸ—‚οΈ Graffiti Admin Dashboard")
65
 
66
- with gr.Tab("πŸ“… Calls Report"):
 
67
  calls_date = gr.Date(label="Select Date")
68
- calls_table = gr.Dataframe()
69
- calls_date.change(lambda d: filter_calls_by_date(d), inputs=calls_date, outputs=calls_table)
 
70
 
71
  with gr.Tab("πŸ“… Appointments Report"):
 
72
  appt_date = gr.Date(label="Select Date")
73
- appt_table = gr.Dataframe()
74
- appt_date.change(lambda d: filter_appointments_by_date(d), inputs=appt_date, outputs=appt_table)
75
-
76
- with gr.Tab("πŸ§‘ Appointed Leads"):
77
- leads_btn = gr.Button("View Appointed Leads")
78
- leads_output = gr.Dataframe()
79
- leads_btn.click(fn=appointed_leads_table, outputs=leads_output)
80
-
81
- with gr.Tab("πŸ” Query Live Sheets"):
82
- sheet_choice = gr.Dropdown(choices=["LiveQuotes", "LiveCustomer", "LiveJobBags"], label="Select Sheet")
83
- field_input = gr.Textbox(label="Field (column name)")
84
- keyword_input = gr.Textbox(label="Keyword to search")
85
- query_btn = gr.Button("Search")
86
- query_table = gr.Dataframe()
87
- query_info = gr.Markdown()
88
- query_btn.click(fn=search_table, inputs=[sheet_choice, field_input, keyword_input], outputs=[query_table, query_info])
89
 
90
  def do_login(user, pw):
91
  if VALID_USERS.get(user) == pw:
@@ -95,4 +130,4 @@ with gr.Blocks() as app:
95
 
96
  login_btn.click(fn=do_login, inputs=[email, password], outputs=[login_ui, main_ui, login_msg])
97
 
98
- app.launch()
 
2
  import gspread
3
  import gradio as gr
4
  from oauth2client.service_account import ServiceAccountCredentials
5
+ from datetime import datetime, timedelta
6
 
7
  # ------------------ AUTH ------------------
8
  VALID_USERS = {
 
18
  client = gspread.authorize(creds)
19
  sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
20
 
21
+ # ------------------ SHEET REFRESH ------------------
22
  def load_sheet(sheet_name):
23
  sheet = client.open_by_url(sheet_url).worksheet(sheet_name)
24
+ df = pd.DataFrame(sheet.get_all_records())
 
25
  return df
26
 
27
+ # ------------------ REPORT HELPERS ------------------
28
+ def week_range(date):
29
+ start = date - timedelta(days=date.weekday())
30
+ end = start + timedelta(days=6)
31
+ return start, end
32
+
33
+ def month_range(date):
34
+ start = date.replace(day=1)
35
+ if start.month == 12:
36
+ end = start.replace(year=start.year+1, month=1, day=1) - timedelta(days=1)
37
+ else:
38
+ end = start.replace(month=start.month+1, day=1) - timedelta(days=1)
39
+ return start, end
40
+
41
+ # ------------------ REPORT FILTERS ------------------
42
+ def filter_calls(date, period="Daily"):
43
  df = load_sheet("Calls")
44
+ df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
45
+ if period == "Weekly":
46
+ start, end = week_range(date)
47
+ df = df[(df['Date'] >= start) & (df['Date'] <= end)]
48
+ elif period == "Monthly":
49
+ start, end = month_range(date)
50
+ df = df[(df['Date'] >= start) & (df['Date'] <= end)]
51
+ else:
52
+ df = df[df['Date'] == date]
53
+ return df
54
 
55
+ def filter_appointments(date, period="Daily"):
56
  df = load_sheet("Appointments")
57
+ df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
58
+ if period == "Weekly":
59
+ start, end = week_range(date)
60
+ df = df[(df['Date'] >= start) & (df['Date'] <= end)]
61
+ elif period == "Monthly":
62
+ start, end = month_range(date)
63
+ df = df[(df['Date'] >= start) & (df['Date'] <= end)]
64
+ else:
65
+ df = df[df['Date'] == date]
66
+ return df
67
+
68
+ # ------------------ USERS & METRICS ------------------
69
+ def get_rep_metrics(date, period="Weekly"):
70
+ calls = filter_calls(date, period)
71
+ appts = filter_appointments(date, period)
72
+ users = load_sheet("Users")
73
+
74
+ call_counts = calls['Rep'].value_counts().to_dict()
75
+ appt_counts = appts['Rep'].value_counts().to_dict()
76
+
77
+ results = []
78
+ for _, row in users.iterrows():
79
+ rep = row['Rep']
80
+ calls_done = call_counts.get(rep, 0)
81
+ appts_done = appt_counts.get(rep, 0)
82
+ results.append({
83
+ "Rep": rep,
84
+ "Weekly Target": row.get('Weekly Target', 0),
85
+ "Calls Made": calls_done,
86
+ "Appointments Booked": appts_done,
87
+ "Gap to Target": max(0, row.get('Weekly Target', 0) - calls_done)
88
+ })
89
+ return pd.DataFrame(results)
90
+
91
+ # ------------------ UI ------------------
92
  with gr.Blocks() as app:
93
  with gr.Row():
94
  with gr.Column(visible=True) as login_ui:
 
99
  login_msg = gr.Markdown("")
100
 
101
  with gr.Column(visible=False) as main_ui:
102
+ gr.Markdown("## πŸ“Š Graffiti Admin Dashboard")
103
 
104
+ with gr.Tab("πŸ“ž Calls Report"):
105
+ calls_period = gr.Radio(choices=["Daily", "Weekly", "Monthly"], value="Daily", label="Report Period")
106
  calls_date = gr.Date(label="Select Date")
107
+ calls_output = gr.Dataframe()
108
+ calls_date.change(fn=filter_calls, inputs=[calls_date, calls_period], outputs=calls_output)
109
+ calls_period.change(fn=filter_calls, inputs=[calls_date, calls_period], outputs=calls_output)
110
 
111
  with gr.Tab("πŸ“… Appointments Report"):
112
+ appt_period = gr.Radio(choices=["Daily", "Weekly", "Monthly"], value="Daily", label="Report Period")
113
  appt_date = gr.Date(label="Select Date")
114
+ appt_output = gr.Dataframe()
115
+ appt_date.change(fn=filter_appointments, inputs=[appt_date, appt_period], outputs=appt_output)
116
+ appt_period.change(fn=filter_appointments, inputs=[appt_date, appt_period], outputs=appt_output)
117
+
118
+ with gr.Tab("🎯 Rep Metrics vs Targets"):
119
+ metric_date = gr.Date(label="Select Any Date in the Week/Month")
120
+ metric_period = gr.Radio(choices=["Weekly", "Monthly"], value="Weekly", label="Metric Period")
121
+ metric_table = gr.Dataframe()
122
+ metric_date.change(fn=get_rep_metrics, inputs=[metric_date, metric_period], outputs=metric_table)
123
+ metric_period.change(fn=get_rep_metrics, inputs=[metric_date, metric_period], outputs=metric_table)
 
 
 
 
 
 
124
 
125
  def do_login(user, pw):
126
  if VALID_USERS.get(user) == pw:
 
130
 
131
  login_btn.click(fn=do_login, inputs=[email, password], outputs=[login_ui, main_ui, login_msg])
132
 
133
+ app.launch()