Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,15 +17,19 @@ client = gspread.authorize(creds)
|
|
17 |
sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
|
18 |
|
19 |
# -------------------- UTILS --------------------
|
20 |
-
def
|
21 |
-
|
22 |
-
return
|
23 |
|
24 |
def load_sheet(sheet_name: str) -> pd.DataFrame:
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
def get_current_week_range():
|
31 |
today = datetime.now()
|
@@ -36,7 +40,7 @@ def get_current_week_range():
|
|
36 |
# -------------------- CALLS --------------------
|
37 |
def get_calls(rep=None):
|
38 |
df = load_sheet("Calls")
|
39 |
-
if "Call Date" not in df
|
40 |
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
41 |
df["Call Date"] = pd.to_datetime(df["Call Date"], errors="coerce").dt.date
|
42 |
start, end = get_current_week_range()
|
@@ -47,7 +51,7 @@ def get_calls(rep=None):
|
|
47 |
|
48 |
def search_calls_by_date(y, m, d, rep):
|
49 |
df = load_sheet("Calls")
|
50 |
-
if "Call Date" not in df
|
51 |
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
52 |
try:
|
53 |
target = datetime(int(y), int(m), int(d)).date()
|
@@ -62,7 +66,7 @@ def search_calls_by_date(y, m, d, rep):
|
|
62 |
# -------------------- APPOINTMENTS --------------------
|
63 |
def appointments_detail(rep=None):
|
64 |
df = load_sheet("Appointments")
|
65 |
-
if "Appointment Date" not in df
|
66 |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}])
|
67 |
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors="coerce").dt.date
|
68 |
start, end = get_current_week_range()
|
@@ -81,7 +85,7 @@ def appointments_summary(rep=None):
|
|
81 |
|
82 |
def search_appointments_by_date(y, m, d, rep):
|
83 |
df = load_sheet("Appointments")
|
84 |
-
if "Appointment Date" not in df
|
85 |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}])
|
86 |
try:
|
87 |
target = datetime(int(y), int(m), int(d)).date()
|
@@ -96,8 +100,7 @@ def search_appointments_by_date(y, m, d, rep):
|
|
96 |
# -------------------- LEADS --------------------
|
97 |
def get_leads_detail():
|
98 |
df = load_sheet("AllocatedLeads")
|
99 |
-
|
100 |
-
if "Assigned Rep" not in df.columns or "Company Name" not in df.columns:
|
101 |
return pd.DataFrame([{"Error": "Missing 'Assigned Rep' or 'Company Name' column"}])
|
102 |
return df
|
103 |
|
@@ -189,9 +192,9 @@ with gr.Blocks(title="Graffiti Admin Dashboard") as app:
|
|
189 |
|
190 |
# Appointed Leads
|
191 |
with gr.Tab("Appointed Leads"):
|
192 |
-
leads_btn
|
193 |
-
leads_sum
|
194 |
-
leads_det
|
195 |
leads_btn.click(
|
196 |
fn=lambda: (get_leads_summary(), get_leads_detail()),
|
197 |
outputs=[leads_sum, leads_det]
|
|
|
17 |
sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
|
18 |
|
19 |
# -------------------- UTILS --------------------
|
20 |
+
def normalize_header(raw_header):
|
21 |
+
# strip and titleize
|
22 |
+
return [h.strip().title() for h in raw_header]
|
23 |
|
24 |
def load_sheet(sheet_name: str) -> pd.DataFrame:
|
25 |
+
ws = client.open_by_url(sheet_url).worksheet(sheet_name)
|
26 |
+
all_vals = ws.get_all_values()
|
27 |
+
if not all_vals or len(all_vals) < 2:
|
28 |
+
return pd.DataFrame()
|
29 |
+
header = normalize_header(all_vals[0])
|
30 |
+
rows = all_vals[1:]
|
31 |
+
df = pd.DataFrame(rows, columns=header)
|
32 |
+
return df
|
33 |
|
34 |
def get_current_week_range():
|
35 |
today = datetime.now()
|
|
|
40 |
# -------------------- CALLS --------------------
|
41 |
def get_calls(rep=None):
|
42 |
df = load_sheet("Calls")
|
43 |
+
if "Call Date" not in df:
|
44 |
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
45 |
df["Call Date"] = pd.to_datetime(df["Call Date"], errors="coerce").dt.date
|
46 |
start, end = get_current_week_range()
|
|
|
51 |
|
52 |
def search_calls_by_date(y, m, d, rep):
|
53 |
df = load_sheet("Calls")
|
54 |
+
if "Call Date" not in df:
|
55 |
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
56 |
try:
|
57 |
target = datetime(int(y), int(m), int(d)).date()
|
|
|
66 |
# -------------------- APPOINTMENTS --------------------
|
67 |
def appointments_detail(rep=None):
|
68 |
df = load_sheet("Appointments")
|
69 |
+
if "Appointment Date" not in df:
|
70 |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}])
|
71 |
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors="coerce").dt.date
|
72 |
start, end = get_current_week_range()
|
|
|
85 |
|
86 |
def search_appointments_by_date(y, m, d, rep):
|
87 |
df = load_sheet("Appointments")
|
88 |
+
if "Appointment Date" not in df:
|
89 |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}])
|
90 |
try:
|
91 |
target = datetime(int(y), int(m), int(d)).date()
|
|
|
100 |
# -------------------- LEADS --------------------
|
101 |
def get_leads_detail():
|
102 |
df = load_sheet("AllocatedLeads")
|
103 |
+
if "Assigned Rep" not in df or "Company Name" not in df:
|
|
|
104 |
return pd.DataFrame([{"Error": "Missing 'Assigned Rep' or 'Company Name' column"}])
|
105 |
return df
|
106 |
|
|
|
192 |
|
193 |
# Appointed Leads
|
194 |
with gr.Tab("Appointed Leads"):
|
195 |
+
leads_btn = gr.Button("View Appointed Leads")
|
196 |
+
leads_sum = gr.Dataframe(label="๐ Leads Count by Rep")
|
197 |
+
leads_det = gr.Dataframe(label="๐ Detailed Leads")
|
198 |
leads_btn.click(
|
199 |
fn=lambda: (get_leads_summary(), get_leads_detail()),
|
200 |
outputs=[leads_sum, leads_det]
|