IAMTFRMZA commited on
Commit
06faff1
·
verified ·
1 Parent(s): 1051212

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -111
app.py CHANGED
@@ -1,138 +1,107 @@
1
  # app.py
 
2
  import pandas as pd
3
  import gspread
4
- import gradio as gr
5
  from oauth2client.service_account import ServiceAccountCredentials
6
  from datetime import datetime, timedelta
7
- import re
8
-
9
- # ------------------ AUTH ------------------
10
- VALID_USERS = {
11
- "[email protected]": "Pass.123",
12
- "[email protected]": "Pass.123",
13
- "[email protected]": "Pass.123",
14
- "[email protected]": "Pass.123"
15
- }
16
 
17
- # ------------------ GOOGLE SHEET SETUP ------------------
18
  scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
19
- creds = ServiceAccountCredentials.from_json_keyfile_name("deep-mile-461309-t8-0e90103411e0.json", scope)
20
  client = gspread.authorize(creds)
21
  sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
22
 
23
- # ------------------ DATA UTILS ------------------
24
- def clean_utf(df):
25
- for col in df.select_dtypes(include="object").columns:
26
- df[col] = df[col].astype(str).apply(lambda x: re.sub(r'[^\x00-\x7F]', '', x))
27
- return df
28
-
29
- def prune_5_years(df, date_col="Date"):
30
- df[date_col] = pd.to_datetime(df[date_col], errors='coerce')
31
- cutoff = pd.Timestamp.now() - pd.DateOffset(years=5)
32
- return df[df[date_col] >= cutoff]
33
-
34
- def load_sheet(name, prune=False):
35
  try:
36
- sheet = client.open_by_url(sheet_url).worksheet(name)
37
  df = pd.DataFrame(sheet.get_all_records())
38
- df = clean_utf(df)
39
- if prune:
40
- df = prune_5_years(df)
41
  return df
42
  except Exception as e:
43
  return pd.DataFrame([{"Error": str(e)}])
44
 
45
- # ------------------ REPORT FUNCTIONS ------------------
46
- def get_week_range():
47
- today = datetime.today()
48
  start = today - timedelta(days=today.weekday())
49
  end = start + timedelta(days=6)
50
  return start.date(), end.date()
51
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  def current_week_calls():
53
  df = load_sheet("Calls")
54
- df["Date"] = pd.to_datetime(df["Date"], errors='coerce')
55
- start, end = get_week_range()
56
- return df[(df["Date"] >= pd.Timestamp(start)) & (df["Date"] <= pd.Timestamp(end))]
57
 
58
  def current_week_appointments():
59
  df = load_sheet("Appointments")
60
- df["Date"] = pd.to_datetime(df["Date"], errors='coerce')
61
- start, end = get_week_range()
62
- return df[(df["Date"] >= pd.Timestamp(start)) & (df["Date"] <= pd.Timestamp(end))]
63
 
64
- def get_appointed_leads():
65
  df = load_sheet("Appointed Leads")
66
- if "Rep" in df.columns and "Customer Name" in df.columns:
67
- return df.groupby("Rep")["Customer Name"].apply(list).reset_index(name="Leads")
68
- return pd.DataFrame([{"Error": "Missing 'Rep' or 'Customer Name' column."}])
69
-
70
- def live_search(sheet_name, field, keyword):
71
- df = load_sheet(sheet_name, prune=(sheet_name == "LiveQuotes"))
72
- if field not in df.columns:
73
- return pd.DataFrame(), "Field not found"
74
- matches = df[df[field].astype(str).str.contains(keyword, case=False, na=False)]
75
- return matches, f"Found {len(matches)} results"
76
-
77
- def rep_target_comparison():
78
- calls = current_week_calls()
79
- users = load_sheet("Users")
80
- if "Rep Name" not in users.columns:
81
- return pd.DataFrame([{"Error": "Missing 'Rep Name' in Users sheet"}])
82
-
83
- calls_grouped = calls.groupby("Rep Name").size().reset_index(name="Visits")
84
- merged = pd.merge(users, calls_grouped, how="left", on="Rep Name")
85
- merged["Visits"] = merged["Visits"].fillna(0).astype(int)
86
- merged["Target Met"] = merged["Visits"] >= merged.get("Target", 0)
87
- return merged
88
-
89
- # ------------------ GRADIO UI ------------------
90
- with gr.Blocks() as app:
91
- with gr.Row():
92
- with gr.Column(visible=True) as login_ui:
93
- gr.Markdown("## 🔐 Login Required")
94
- email = gr.Textbox(label="Email")
95
- password = gr.Textbox(label="Password", type="password")
96
- login_btn = gr.Button("Login")
97
- login_msg = gr.Markdown("")
98
-
99
- with gr.Column(visible=False) as main_ui:
100
- gr.Markdown("## 📊 Graffiti Admin Dashboard")
101
-
102
- with gr.Tab("Calls Report"):
103
- btn_calls = gr.Button("Load Current Week Calls")
104
- calls_output = gr.Dataframe()
105
- btn_calls.click(fn=current_week_calls, outputs=calls_output)
106
-
107
- with gr.Tab("Appointments Report"):
108
- btn_appt = gr.Button("Load Current Week Appointments")
109
- appt_output = gr.Dataframe()
110
- btn_appt.click(fn=current_week_appointments, outputs=appt_output)
111
-
112
- with gr.Tab("Appointed Leads"):
113
- btn_leads = gr.Button("View Appointed Leads")
114
- leads_output = gr.Dataframe()
115
- btn_leads.click(fn=get_appointed_leads, outputs=leads_output)
116
-
117
- with gr.Tab("Query Live Sheets"):
118
- dropdown = gr.Dropdown(choices=["LiveQuotes", "LiveCustomer", "LiveJobBags"], label="Select Sheet")
119
- field = gr.Textbox(label="Field (column name)")
120
- keyword = gr.Textbox(label="Keyword to search")
121
- search_btn = gr.Button("Search")
122
- query_table = gr.Dataframe()
123
- status = gr.Markdown()
124
- search_btn.click(fn=live_search, inputs=[dropdown, field, keyword], outputs=[query_table, status])
125
-
126
- with gr.Tab("Weekly Rep Performance"):
127
- btn_perf = gr.Button("Compare Rep Activity vs Target")
128
- perf_output = gr.Dataframe()
129
- btn_perf.click(fn=rep_target_comparison, outputs=perf_output)
130
-
131
- def do_login(user, pw):
132
- if VALID_USERS.get(user) == pw:
133
- return gr.update(visible=False), gr.update(visible=True), ""
134
- return gr.update(visible=True), gr.update(visible=False), "❌ Invalid login"
135
-
136
- login_btn.click(fn=do_login, inputs=[email, password], outputs=[login_ui, main_ui, login_msg])
137
-
138
- app.launch()
 
1
  # app.py
2
+ import gradio as gr
3
  import pandas as pd
4
  import gspread
 
5
  from oauth2client.service_account import ServiceAccountCredentials
6
  from datetime import datetime, timedelta
 
 
 
 
 
 
 
 
 
7
 
8
+ # -------------------- AUTH --------------------
9
  scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
10
+ creds = ServiceAccountCredentials.from_json_keyfile_name("/mnt/data/deep-mile-461309-t8-0e90103411e0.json", scope)
11
  client = gspread.authorize(creds)
12
  sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
13
 
14
+ # -------------------- UTILS --------------------
15
+ def load_sheet(sheet_name):
 
 
 
 
 
 
 
 
 
 
16
  try:
17
+ sheet = client.open_by_url(sheet_url).worksheet(sheet_name)
18
  df = pd.DataFrame(sheet.get_all_records())
 
 
 
19
  return df
20
  except Exception as e:
21
  return pd.DataFrame([{"Error": str(e)}])
22
 
23
+ def get_current_week_range():
24
+ today = datetime.now()
 
25
  start = today - timedelta(days=today.weekday())
26
  end = start + timedelta(days=6)
27
  return start.date(), end.date()
28
 
29
+ def filter_current_week(df, date_column):
30
+ df[date_column] = pd.to_datetime(df[date_column], errors='coerce').dt.date
31
+ start_date, end_date = get_current_week_range()
32
+ return df[(df[date_column] >= start_date) & (df[date_column] <= end_date)]
33
+
34
+ def compose_date(year, month, day):
35
+ try:
36
+ return str(datetime(int(year), int(month), int(day)).date())
37
+ except:
38
+ return None
39
+
40
+ # -------------------- FUNCTIONS --------------------
41
  def current_week_calls():
42
  df = load_sheet("Calls")
43
+ if "Date" not in df.columns:
44
+ return pd.DataFrame([{"Error": "Missing 'Date' column"}])
45
+ return filter_current_week(df, "Date")
46
 
47
  def current_week_appointments():
48
  df = load_sheet("Appointments")
49
+ if "Date" not in df.columns:
50
+ return pd.DataFrame([{"Error": "Missing 'Date' column"}])
51
+ return filter_current_week(df, "Date")
52
 
53
+ def appointed_leads():
54
  df = load_sheet("Appointed Leads")
55
+ if "Rep" not in df.columns or "Customer Name" not in df.columns:
56
+ return pd.DataFrame([{"Error": "Missing 'Rep' or 'Customer Name' column"}])
57
+ grouped = df.groupby("Rep")["Customer Name"].apply(list).reset_index()
58
+ return grouped
59
+
60
+ def custom_date_calls(y, m, d):
61
+ date_str = compose_date(y, m, d)
62
+ if not date_str:
63
+ return pd.DataFrame([{"Error": "Invalid date input"}])
64
+ df = load_sheet("Calls")
65
+ df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date.astype(str)
66
+ return df[df['Date'] == date_str]
67
+
68
+ def custom_date_appointments(y, m, d):
69
+ date_str = compose_date(y, m, d)
70
+ if not date_str:
71
+ return pd.DataFrame([{"Error": "Invalid date input"}])
72
+ df = load_sheet("Appointments")
73
+ df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date.astype(str)
74
+ return df[df['Date'] == date_str]
75
+
76
+ # -------------------- UI --------------------
77
+ with gr.Blocks(title="Graffiti Admin Dashboard") as app:
78
+ gr.Markdown("## 📅 Graffiti Admin Dashboard")
79
+
80
+ with gr.Tab("Calls Report"):
81
+ calls_btn = gr.Button("Load Current Week Calls")
82
+ calls_table = gr.Dataframe()
83
+ calls_btn.click(fn=current_week_calls, outputs=calls_table)
84
+
85
+ gr.Markdown("### 🔎 Check by Specific Date (YYYY-MM-DD via dropdowns)")
86
+ y, m, d = gr.Textbox(label="Year"), gr.Textbox(label="Month"), gr.Textbox(label="Day")
87
+ date_calls_btn = gr.Button("Search Calls by Date")
88
+ date_calls_table = gr.Dataframe()
89
+ date_calls_btn.click(fn=custom_date_calls, inputs=[y, m, d], outputs=date_calls_table)
90
+
91
+ with gr.Tab("Appointments Report"):
92
+ appt_btn = gr.Button("Load Current Week Appointments")
93
+ appt_table = gr.Dataframe()
94
+ appt_btn.click(fn=current_week_appointments, outputs=appt_table)
95
+
96
+ gr.Markdown("### 🔎 Check by Specific Date")
97
+ ay, am, ad = gr.Textbox(label="Year"), gr.Textbox(label="Month"), gr.Textbox(label="Day")
98
+ date_appt_btn = gr.Button("Search Appointments by Date")
99
+ date_appt_table = gr.Dataframe()
100
+ date_appt_btn.click(fn=custom_date_appointments, inputs=[ay, am, ad], outputs=date_appt_table)
101
+
102
+ with gr.Tab("Appointed Leads"):
103
+ leads_btn = gr.Button("View Appointed Leads")
104
+ leads_table = gr.Dataframe()
105
+ leads_btn.click(fn=appointed_leads, outputs=leads_table)
106
+
107
+ app.launch(share=True)