IAMTFRMZA commited on
Commit
93be3f3
·
verified ·
1 Parent(s): 70a3386

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -70
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import pandas as pd
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,48 +18,31 @@ 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 ACCESS UTILS ------------------
22
  def load_sheet(sheet_name):
23
  sheet = client.open_by_url(sheet_url).worksheet(sheet_name)
24
- data = sheet.get_all_records()
25
- return pd.DataFrame(data)
26
-
27
- # ------------------ REPORT UTILS ------------------
28
- def parse_date(date_str):
29
- try:
30
- return pd.to_datetime(date_str, errors='coerce').date()
31
- except:
32
- return None
33
-
34
- def get_week_range(start_date):
35
- start = start_date - timedelta(days=start_date.weekday())
36
- end = start + timedelta(days=6)
37
- return start, end
38
 
39
- def get_month_range(date_obj):
40
- start = date_obj.replace(day=1)
41
- next_month = start + pd.DateOffset(months=1)
42
- end = (next_month - timedelta(days=1)).date()
43
- return start.date(), end
44
-
45
- # ------------------ CALLS ------------------
46
- def filter_calls_by_date_range(start, end):
47
- df = load_sheet("Calls")
48
- df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date
49
- return df[(df['Date'] >= start) & (df['Date'] <= end)]
50
 
51
- # ------------------ APPOINTMENTS ------------------
52
- def filter_appointments_by_date_range(start, end):
53
- df = load_sheet("Appointments")
54
  df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date
55
- return df[(df['Date'] >= start) & (df['Date'] <= end)]
56
 
57
  # ------------------ APPOINTED LEADS ------------------
58
  def appointed_leads_table():
59
- df = load_sheet("Appointed Leads")
60
- return df.groupby('Rep')['Customer Name'].apply(list).reset_index()
 
61
 
62
- # ------------------ INTERACTIVE QUERY ------------------
63
  def search_table(sheet_name, field, keyword):
64
  df = load_sheet(sheet_name)
65
  if field not in df.columns:
@@ -67,55 +50,58 @@ def search_table(sheet_name, field, keyword):
67
  results = df[df[field].astype(str).str.contains(keyword, case=False, na=False)]
68
  return results, f"Found {len(results)} results."
69
 
70
- # ------------------ UI ------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  with gr.Blocks() as app:
72
  with gr.Row():
73
  with gr.Column(visible=True) as login_ui:
74
- gr.Markdown("## \ud83d\udd10 Login Required")
75
  email = gr.Textbox(label="Email")
76
  password = gr.Textbox(label="Password", type="password")
77
  login_btn = gr.Button("Login")
78
- login_msg = gr.Markdown("")
79
 
80
  with gr.Column(visible=False) as main_ui:
81
- gr.Markdown("## \ud83d\udcc2 Graffiti Admin Dashboard")
82
 
83
- with gr.Tab("\ud83d\udcc5 Calls Report"):
84
- report_type_call = gr.Radio(["Weekly", "Monthly"], label="Report Type")
85
- date_input_call = gr.Textbox(label="Enter Start Date (YYYY-MM-DD)")
86
  calls_table = gr.Dataframe()
87
-
88
- def generate_call_report(rt, date_str):
89
- date = parse_date(date_str)
90
- if not date:
91
- return pd.DataFrame()
92
- start, end = get_week_range(date) if rt == "Weekly" else get_month_range(date)
93
- return filter_calls_by_date_range(start, end)
94
-
95
- report_type_call.change(generate_call_report, inputs=[report_type_call, date_input_call], outputs=calls_table)
96
- date_input_call.change(generate_call_report, inputs=[report_type_call, date_input_call], outputs=calls_table)
97
-
98
- with gr.Tab("\ud83d\udcc5 Appointments Report"):
99
- report_type_appt = gr.Radio(["Weekly", "Monthly"], label="Report Type")
100
- date_input_appt = gr.Textbox(label="Enter Start Date (YYYY-MM-DD)")
101
- appt_table = gr.Dataframe()
102
-
103
- def generate_appt_report(rt, date_str):
104
- date = parse_date(date_str)
105
- if not date:
106
- return pd.DataFrame()
107
- start, end = get_week_range(date) if rt == "Weekly" else get_month_range(date)
108
- return filter_appointments_by_date_range(start, end)
109
 
110
- report_type_appt.change(generate_appt_report, inputs=[report_type_appt, date_input_appt], outputs=appt_table)
111
- date_input_appt.change(generate_appt_report, inputs=[report_type_appt, date_input_appt], outputs=appt_table)
 
 
112
 
113
- with gr.Tab("\ud83e\uddcd Appointed Leads"):
114
- leads_btn = gr.Button("View Appointed Leads")
115
  leads_output = gr.Dataframe()
 
116
  leads_btn.click(fn=appointed_leads_table, outputs=leads_output)
117
 
118
- with gr.Tab("\ud83d\udd0d Query Live Sheets"):
119
  sheet_choice = gr.Dropdown(choices=["LiveQuotes", "LiveCustomer", "LiveJobBags"], label="Select Sheet")
120
  field_input = gr.Textbox(label="Field (column name)")
121
  keyword_input = gr.Textbox(label="Keyword to search")
@@ -124,12 +110,17 @@ with gr.Blocks() as app:
124
  query_info = gr.Markdown()
125
  query_btn.click(fn=search_table, inputs=[sheet_choice, field_input, keyword_input], outputs=[query_table, query_info])
126
 
 
 
 
 
 
127
  def do_login(user, pw):
128
  if VALID_USERS.get(user) == pw:
129
  return gr.update(visible=False), gr.update(visible=True), ""
130
  else:
131
- return gr.update(visible=True), gr.update(visible=False), "\u274c Invalid email or password."
132
 
133
  login_btn.click(fn=do_login, inputs=[email, password], outputs=[login_ui, main_ui, login_msg])
134
 
135
- app.launch()
 
1
  import pandas as pd
2
  import gspread
3
  import gradio as gr
 
4
  from datetime import datetime, timedelta
5
+ from oauth2client.service_account import ServiceAccountCredentials
6
 
7
  # ------------------ AUTH ------------------
8
  VALID_USERS = {
 
18
  client = gspread.authorize(creds)
19
  sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
20
 
 
21
  def load_sheet(sheet_name):
22
  sheet = client.open_by_url(sheet_url).worksheet(sheet_name)
23
+ df = pd.DataFrame(sheet.get_all_records())
24
+ return df
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # ------------------ WEEK HELPERS ------------------
27
+ def get_current_week_range():
28
+ today = datetime.today()
29
+ start = today - timedelta(days=today.weekday())
30
+ end = start + timedelta(days=6)
31
+ return start.date(), end.date()
 
 
 
 
 
32
 
33
+ # ------------------ CALLS & APPOINTMENTS REPORT ------------------
34
+ def filter_by_date_range(sheet_name, start_date, end_date):
35
+ df = load_sheet(sheet_name)
36
  df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date
37
+ return df[(df['Date'] >= start_date) & (df['Date'] <= end_date)]
38
 
39
  # ------------------ APPOINTED LEADS ------------------
40
  def appointed_leads_table():
41
+ df = load_sheet("AllocatedLeads")
42
+ grouped = df.groupby('Rep')['Customer Name'].apply(list).reset_index()
43
+ return grouped
44
 
45
+ # ------------------ LIVE SHEET SEARCH ------------------
46
  def search_table(sheet_name, field, keyword):
47
  df = load_sheet(sheet_name)
48
  if field not in df.columns:
 
50
  results = df[df[field].astype(str).str.contains(keyword, case=False, na=False)]
51
  return results, f"Found {len(results)} results."
52
 
53
+ # ------------------ USER TARGET COMPARISON ------------------
54
+ def generate_user_metrics():
55
+ users_df = load_sheet("Users")
56
+ calls_df = load_sheet("Calls")
57
+ appt_df = load_sheet("Appointments")
58
+
59
+ calls_df['Date'] = pd.to_datetime(calls_df['Date'], errors='coerce').dt.date
60
+ appt_df['Date'] = pd.to_datetime(appt_df['Date'], errors='coerce').dt.date
61
+
62
+ start, end = get_current_week_range()
63
+ week_calls = calls_df[(calls_df['Date'] >= start) & (calls_df['Date'] <= end)]
64
+ week_appt = appt_df[(appt_df['Date'] >= start) & (appt_df['Date'] <= end)]
65
+
66
+ call_count = week_calls.groupby("Rep Name").size().reset_index(name="Calls This Week")
67
+ appt_count = week_appt.groupby("Rep Name").size().reset_index(name="Appointments This Week")
68
+
69
+ merged = users_df.merge(call_count, how='left', left_on='Name', right_on='Rep Name')
70
+ merged = merged.merge(appt_count, how='left', left_on='Name', right_on='Rep Name')
71
+ merged = merged.fillna(0)
72
+
73
+ return merged[['Name', 'Weekly Target', 'Calls This Week', 'Appointments This Week']]
74
+
75
+ # ------------------ GRADIO UI ------------------
76
  with gr.Blocks() as app:
77
  with gr.Row():
78
  with gr.Column(visible=True) as login_ui:
79
+ gr.Markdown("## Login Required")
80
  email = gr.Textbox(label="Email")
81
  password = gr.Textbox(label="Password", type="password")
82
  login_btn = gr.Button("Login")
83
+ login_msg = gr.Markdown()
84
 
85
  with gr.Column(visible=False) as main_ui:
86
+ gr.Markdown("## Graffiti Admin Dashboard")
87
 
88
+ with gr.Tab("Calls Report"):
89
+ week_start, week_end = get_current_week_range()
 
90
  calls_table = gr.Dataframe()
91
+ week_btn = gr.Button("View This Week's Calls")
92
+ week_btn.click(lambda: filter_by_date_range("Calls", week_start, week_end), outputs=calls_table)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
+ with gr.Tab("Appointments Report"):
95
+ appt_table = gr.Dataframe()
96
+ appt_btn = gr.Button("View This Week's Appointments")
97
+ appt_btn.click(lambda: filter_by_date_range("Appointments", week_start, week_end), outputs=appt_table)
98
 
99
+ with gr.Tab("Appointed Leads"):
 
100
  leads_output = gr.Dataframe()
101
+ leads_btn = gr.Button("View Appointed Leads")
102
  leads_btn.click(fn=appointed_leads_table, outputs=leads_output)
103
 
104
+ with gr.Tab("Query Live Sheets"):
105
  sheet_choice = gr.Dropdown(choices=["LiveQuotes", "LiveCustomer", "LiveJobBags"], label="Select Sheet")
106
  field_input = gr.Textbox(label="Field (column name)")
107
  keyword_input = gr.Textbox(label="Keyword to search")
 
110
  query_info = gr.Markdown()
111
  query_btn.click(fn=search_table, inputs=[sheet_choice, field_input, keyword_input], outputs=[query_table, query_info])
112
 
113
+ with gr.Tab("Rep vs Targets"):
114
+ metric_table = gr.Dataframe()
115
+ metric_btn = gr.Button("View Weekly Targets")
116
+ metric_btn.click(fn=generate_user_metrics, outputs=metric_table)
117
+
118
  def do_login(user, pw):
119
  if VALID_USERS.get(user) == pw:
120
  return gr.update(visible=False), gr.update(visible=True), ""
121
  else:
122
+ return gr.update(visible=True), gr.update(visible=False), " Invalid email or password."
123
 
124
  login_btn.click(fn=do_login, inputs=[email, password], outputs=[login_ui, main_ui, login_msg])
125
 
126
+ app.launch(share=True)