Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,13 @@ from oauth2client.service_account import ServiceAccountCredentials
|
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
|
| 7 |
# -------------------- AUTH --------------------
|
| 8 |
-
scope = [
|
|
|
|
|
|
|
|
|
|
| 9 |
creds = ServiceAccountCredentials.from_json_keyfile_name(
|
| 10 |
-
"deep-mile-461309-t8-0e90103411e0.json",
|
|
|
|
| 11 |
)
|
| 12 |
client = gspread.authorize(creds)
|
| 13 |
sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
|
|
@@ -41,9 +45,9 @@ def filter_week(df, date_col, rep_col=None, rep=None):
|
|
| 41 |
|
| 42 |
def filter_date(df, date_col, rep_col, y, m, d, rep):
|
| 43 |
try:
|
| 44 |
-
target = datetime(int(y),int(m),int(d)).date()
|
| 45 |
except:
|
| 46 |
-
return pd.DataFrame([{"Error":"Invalid date input"}])
|
| 47 |
df[date_col] = pd.to_datetime(df[date_col], errors="coerce").dt.date
|
| 48 |
out = df[df[date_col] == target]
|
| 49 |
if rep:
|
|
@@ -53,19 +57,21 @@ def filter_date(df, date_col, rep_col, y, m, d, rep):
|
|
| 53 |
# -------------------- REPORT FUNCTIONS --------------------
|
| 54 |
def get_calls(rep=None):
|
| 55 |
df = load_sheet("Calls")
|
| 56 |
-
if "Call Date" not in df:
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
def search_calls_by_date(y,m,d,rep):
|
| 60 |
df = load_sheet("Calls")
|
| 61 |
-
if "Call Date" not in df:
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
# -------------------- APPOINTMENTS (UPCOMING) --------------------
|
| 65 |
def upcoming_summary_and_detail(rep=None):
|
| 66 |
df = load_sheet("Appointments")
|
| 67 |
if "Appointment Date" not in df:
|
| 68 |
-
return pd.DataFrame([{"Error":"Missing 'Appointment Date' column"}]), pd.DataFrame()
|
| 69 |
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors="coerce").dt.date
|
| 70 |
today = datetime.now().date()
|
| 71 |
future = df[df["Appointment Date"] >= today]
|
|
@@ -74,14 +80,14 @@ def upcoming_summary_and_detail(rep=None):
|
|
| 74 |
summary = future.groupby("Rep").size().reset_index(name="Appointment Count")
|
| 75 |
return summary, future
|
| 76 |
|
| 77 |
-
def search_appointments_by_date(y,m,d,rep):
|
| 78 |
df = load_sheet("Appointments")
|
| 79 |
if "Appointment Date" not in df:
|
| 80 |
-
return pd.DataFrame([{"Error":"Missing 'Appointment Date' column"}]), pd.DataFrame()
|
| 81 |
try:
|
| 82 |
-
target = datetime(int(y),int(m),int(d)).date()
|
| 83 |
except:
|
| 84 |
-
return pd.DataFrame([{"Error":"Invalid date input"}]), pd.DataFrame()
|
| 85 |
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors="coerce").dt.date
|
| 86 |
out = df[df["Appointment Date"] == target]
|
| 87 |
if rep:
|
|
@@ -89,16 +95,17 @@ def search_appointments_by_date(y,m,d,rep):
|
|
| 89 |
summary = out.groupby("Rep").size().reset_index(name="Appointment Count")
|
| 90 |
return summary, out
|
| 91 |
|
| 92 |
-
# -------------------- LEADS --------------------
|
| 93 |
def get_leads_detail():
|
| 94 |
df = load_sheet("AllocatedLeads")
|
| 95 |
if "Assigned Rep" not in df or "Company Name" not in df:
|
| 96 |
-
return pd.DataFrame([{"Error":"Missing 'Assigned Rep' or 'Company Name' column"}])
|
| 97 |
return df
|
| 98 |
|
| 99 |
def get_leads_summary():
|
| 100 |
df = get_leads_detail()
|
| 101 |
-
if "Error" in df.columns:
|
|
|
|
| 102 |
return df.groupby("Assigned Rep").size().reset_index(name="Leads Count")
|
| 103 |
|
| 104 |
# -------------------- INSIGHTS --------------------
|
|
@@ -106,14 +113,29 @@ def compute_insights():
|
|
| 106 |
calls = get_calls()
|
| 107 |
appt_summary, _ = upcoming_summary_and_detail()
|
| 108 |
leads = get_leads_detail()
|
| 109 |
-
def top(df,col):
|
| 110 |
return df[col].value_counts().idxmax() if not df.empty else "N/A"
|
| 111 |
return pd.DataFrame([
|
| 112 |
-
{"Metric":"Most Calls This Week",
|
| 113 |
-
{"Metric":"Most Upcoming Appointments",
|
| 114 |
-
{"Metric":"Most Leads Allocated",
|
| 115 |
])
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# -------------------- DROPDOWN OPTIONS --------------------
|
| 118 |
def rep_options(sheet, col):
|
| 119 |
df = load_sheet(sheet)
|
|
@@ -123,50 +145,71 @@ def rep_options(sheet, col):
|
|
| 123 |
with gr.Blocks(title="Graffiti Admin Dashboard") as app:
|
| 124 |
gr.Markdown("# π Graffiti Admin Dashboard")
|
| 125 |
|
| 126 |
-
# Calls Report
|
| 127 |
with gr.Tab("Calls Report"):
|
| 128 |
-
rc = gr.Dropdown("Optional Rep Filter", choices=rep_options("Calls","Rep"))
|
| 129 |
btn_calls = gr.Button("Load Current Week Calls")
|
| 130 |
tbl_calls = gr.Dataframe()
|
| 131 |
btn_calls.click(fn=get_calls, inputs=rc, outputs=tbl_calls)
|
| 132 |
|
| 133 |
gr.Markdown("### π Search Calls by Specific Date")
|
| 134 |
-
y1,m1,d1 = gr.Textbox("Year"), gr.Textbox("Month"), gr.Textbox("Day")
|
| 135 |
-
rc2 = gr.Dropdown("Optional Rep Filter", choices=rep_options("Calls","Rep"))
|
| 136 |
btn_cd = gr.Button("Search Calls by Date")
|
| 137 |
tbl_cd = gr.Dataframe()
|
| 138 |
-
btn_cd.click(fn=search_calls_by_date, inputs=[y1,m1,d1,rc2], outputs=tbl_cd)
|
| 139 |
|
| 140 |
-
# Appointments Report
|
| 141 |
with gr.Tab("Appointments Report"):
|
| 142 |
-
ra = gr.Dropdown("Optional Rep Filter", choices=rep_options("Appointments","Rep"))
|
| 143 |
btn_appt = gr.Button("Load Upcoming Appointments")
|
| 144 |
-
sum_appt = gr.Dataframe(label="π Summary by Rep")
|
| 145 |
-
det_appt = gr.Dataframe(label="π Detailed")
|
| 146 |
-
btn_appt.click(fn=upcoming_summary_and_detail, inputs=ra, outputs=[sum_appt,det_appt])
|
| 147 |
|
| 148 |
gr.Markdown("### π Search Appointments by Specific Date")
|
| 149 |
-
y2,m2,d2 = gr.Textbox("Year"), gr.Textbox("Month"), gr.Textbox("Day")
|
| 150 |
-
ra2 = gr.Dropdown("Optional Rep Filter", choices=rep_options("Appointments","Rep"))
|
| 151 |
btn_ad = gr.Button("Search Appointments by Date")
|
| 152 |
-
sum_ad = gr.Dataframe(label="π Summary by Rep")
|
| 153 |
-
det_ad = gr.Dataframe(label="π Detailed")
|
| 154 |
btn_ad.click(fn=search_appointments_by_date,
|
| 155 |
-
inputs=[y2,m2,d2,ra2],
|
| 156 |
-
outputs=[sum_ad,det_ad])
|
| 157 |
|
| 158 |
-
# Appointed Leads
|
| 159 |
with gr.Tab("Appointed Leads"):
|
| 160 |
btn_leads = gr.Button("View Appointed Leads")
|
| 161 |
sum_leads = gr.Dataframe(label="π Leads Count by Rep")
|
| 162 |
-
det_leads = gr.Dataframe(label="π Detailed")
|
| 163 |
-
btn_leads.click(fn=lambda:(get_leads_summary(),get_leads_detail()),
|
| 164 |
-
outputs=[sum_leads,det_leads])
|
| 165 |
|
| 166 |
-
# Insights
|
| 167 |
with gr.Tab("Insights"):
|
| 168 |
btn_ins = gr.Button("Generate Insights")
|
| 169 |
tbl_ins = gr.Dataframe()
|
| 170 |
btn_ins.click(fn=compute_insights, outputs=tbl_ins)
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
app.launch()
|
|
|
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
|
| 7 |
# -------------------- AUTH --------------------
|
| 8 |
+
scope = [
|
| 9 |
+
"https://spreadsheets.google.com/feeds",
|
| 10 |
+
"https://www.googleapis.com/auth/drive"
|
| 11 |
+
]
|
| 12 |
creds = ServiceAccountCredentials.from_json_keyfile_name(
|
| 13 |
+
"deep-mile-461309-t8-0e90103411e0.json",
|
| 14 |
+
scope
|
| 15 |
)
|
| 16 |
client = gspread.authorize(creds)
|
| 17 |
sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
|
|
|
|
| 45 |
|
| 46 |
def filter_date(df, date_col, rep_col, y, m, d, rep):
|
| 47 |
try:
|
| 48 |
+
target = datetime(int(y), int(m), int(d)).date()
|
| 49 |
except:
|
| 50 |
+
return pd.DataFrame([{"Error": "Invalid date input"}])
|
| 51 |
df[date_col] = pd.to_datetime(df[date_col], errors="coerce").dt.date
|
| 52 |
out = df[df[date_col] == target]
|
| 53 |
if rep:
|
|
|
|
| 57 |
# -------------------- REPORT FUNCTIONS --------------------
|
| 58 |
def get_calls(rep=None):
|
| 59 |
df = load_sheet("Calls")
|
| 60 |
+
if "Call Date" not in df:
|
| 61 |
+
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
| 62 |
+
return filter_week(df, "Call Date", "Rep", rep)
|
| 63 |
|
| 64 |
+
def search_calls_by_date(y, m, d, rep):
|
| 65 |
df = load_sheet("Calls")
|
| 66 |
+
if "Call Date" not in df:
|
| 67 |
+
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
| 68 |
+
return filter_date(df, "Call Date", "Rep", y, m, d, rep)
|
| 69 |
|
| 70 |
# -------------------- APPOINTMENTS (UPCOMING) --------------------
|
| 71 |
def upcoming_summary_and_detail(rep=None):
|
| 72 |
df = load_sheet("Appointments")
|
| 73 |
if "Appointment Date" not in df:
|
| 74 |
+
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}]), pd.DataFrame()
|
| 75 |
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors="coerce").dt.date
|
| 76 |
today = datetime.now().date()
|
| 77 |
future = df[df["Appointment Date"] >= today]
|
|
|
|
| 80 |
summary = future.groupby("Rep").size().reset_index(name="Appointment Count")
|
| 81 |
return summary, future
|
| 82 |
|
| 83 |
+
def search_appointments_by_date(y, m, d, rep):
|
| 84 |
df = load_sheet("Appointments")
|
| 85 |
if "Appointment Date" not in df:
|
| 86 |
+
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}]), pd.DataFrame()
|
| 87 |
try:
|
| 88 |
+
target = datetime(int(y), int(m), int(d)).date()
|
| 89 |
except:
|
| 90 |
+
return pd.DataFrame([{"Error": "Invalid date input"}]), pd.DataFrame()
|
| 91 |
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors="coerce").dt.date
|
| 92 |
out = df[df["Appointment Date"] == target]
|
| 93 |
if rep:
|
|
|
|
| 95 |
summary = out.groupby("Rep").size().reset_index(name="Appointment Count")
|
| 96 |
return summary, out
|
| 97 |
|
| 98 |
+
# -------------------- APPOINTED LEADS --------------------
|
| 99 |
def get_leads_detail():
|
| 100 |
df = load_sheet("AllocatedLeads")
|
| 101 |
if "Assigned Rep" not in df or "Company Name" not in df:
|
| 102 |
+
return pd.DataFrame([{"Error": "Missing 'Assigned Rep' or 'Company Name' column"}])
|
| 103 |
return df
|
| 104 |
|
| 105 |
def get_leads_summary():
|
| 106 |
df = get_leads_detail()
|
| 107 |
+
if "Error" in df.columns:
|
| 108 |
+
return df
|
| 109 |
return df.groupby("Assigned Rep").size().reset_index(name="Leads Count")
|
| 110 |
|
| 111 |
# -------------------- INSIGHTS --------------------
|
|
|
|
| 113 |
calls = get_calls()
|
| 114 |
appt_summary, _ = upcoming_summary_and_detail()
|
| 115 |
leads = get_leads_detail()
|
| 116 |
+
def top(df, col):
|
| 117 |
return df[col].value_counts().idxmax() if not df.empty else "N/A"
|
| 118 |
return pd.DataFrame([
|
| 119 |
+
{"Metric": "Most Calls This Week", "Rep": top(calls, "Rep")},
|
| 120 |
+
{"Metric": "Most Upcoming Appointments", "Rep": top(appt_summary, "Rep")},
|
| 121 |
+
{"Metric": "Most Leads Allocated", "Rep": top(leads, "Assigned Rep")},
|
| 122 |
])
|
| 123 |
|
| 124 |
+
# -------------------- USER MANAGEMENT --------------------
|
| 125 |
+
def load_users():
|
| 126 |
+
df = load_sheet("User")
|
| 127 |
+
for col in ["Name", "Email", "Company", "Target Figures"]:
|
| 128 |
+
if col not in df.columns:
|
| 129 |
+
df[col] = ""
|
| 130 |
+
return df[["Name", "Email", "Company", "Target Figures"]]
|
| 131 |
+
|
| 132 |
+
def save_users(df):
|
| 133 |
+
ws = client.open_by_url(sheet_url).worksheet("User")
|
| 134 |
+
ws.clear()
|
| 135 |
+
rows = [df.columns.tolist()] + df.fillna("").values.tolist()
|
| 136 |
+
ws.update(rows)
|
| 137 |
+
return df
|
| 138 |
+
|
| 139 |
# -------------------- DROPDOWN OPTIONS --------------------
|
| 140 |
def rep_options(sheet, col):
|
| 141 |
df = load_sheet(sheet)
|
|
|
|
| 145 |
with gr.Blocks(title="Graffiti Admin Dashboard") as app:
|
| 146 |
gr.Markdown("# π Graffiti Admin Dashboard")
|
| 147 |
|
| 148 |
+
# Calls Report Tab
|
| 149 |
with gr.Tab("Calls Report"):
|
| 150 |
+
rc = gr.Dropdown(label="Optional Rep Filter", choices=rep_options("Calls", "Rep"))
|
| 151 |
btn_calls = gr.Button("Load Current Week Calls")
|
| 152 |
tbl_calls = gr.Dataframe()
|
| 153 |
btn_calls.click(fn=get_calls, inputs=rc, outputs=tbl_calls)
|
| 154 |
|
| 155 |
gr.Markdown("### π Search Calls by Specific Date")
|
| 156 |
+
y1, m1, d1 = gr.Textbox(label="Year"), gr.Textbox(label="Month"), gr.Textbox(label="Day")
|
| 157 |
+
rc2 = gr.Dropdown(label="Optional Rep Filter", choices=rep_options("Calls", "Rep"))
|
| 158 |
btn_cd = gr.Button("Search Calls by Date")
|
| 159 |
tbl_cd = gr.Dataframe()
|
| 160 |
+
btn_cd.click(fn=search_calls_by_date, inputs=[y1, m1, d1, rc2], outputs=tbl_cd)
|
| 161 |
|
| 162 |
+
# Appointments Report Tab
|
| 163 |
with gr.Tab("Appointments Report"):
|
| 164 |
+
ra = gr.Dropdown(label="Optional Rep Filter", choices=rep_options("Appointments", "Rep"))
|
| 165 |
btn_appt = gr.Button("Load Upcoming Appointments")
|
| 166 |
+
sum_appt = gr.Dataframe(label="π Upcoming Appointments Summary by Rep")
|
| 167 |
+
det_appt = gr.Dataframe(label="π Detailed Upcoming Appointments")
|
| 168 |
+
btn_appt.click(fn=upcoming_summary_and_detail, inputs=ra, outputs=[sum_appt, det_appt])
|
| 169 |
|
| 170 |
gr.Markdown("### π Search Appointments by Specific Date")
|
| 171 |
+
y2, m2, d2 = gr.Textbox(label="Year"), gr.Textbox(label="Month"), gr.Textbox(label="Day")
|
| 172 |
+
ra2 = gr.Dropdown(label="Optional Rep Filter", choices=rep_options("Appointments", "Rep"))
|
| 173 |
btn_ad = gr.Button("Search Appointments by Date")
|
| 174 |
+
sum_ad = gr.Dataframe(label="π Appointments Summary for Date by Rep")
|
| 175 |
+
det_ad = gr.Dataframe(label="π Detailed Appointments for Date")
|
| 176 |
btn_ad.click(fn=search_appointments_by_date,
|
| 177 |
+
inputs=[y2, m2, d2, ra2],
|
| 178 |
+
outputs=[sum_ad, det_ad])
|
| 179 |
|
| 180 |
+
# Appointed Leads Tab
|
| 181 |
with gr.Tab("Appointed Leads"):
|
| 182 |
btn_leads = gr.Button("View Appointed Leads")
|
| 183 |
sum_leads = gr.Dataframe(label="π Leads Count by Rep")
|
| 184 |
+
det_leads = gr.Dataframe(label="π Detailed Leads")
|
| 185 |
+
btn_leads.click(fn=lambda: (get_leads_summary(), get_leads_detail()),
|
| 186 |
+
outputs=[sum_leads, det_leads])
|
| 187 |
|
| 188 |
+
# Insights Tab
|
| 189 |
with gr.Tab("Insights"):
|
| 190 |
btn_ins = gr.Button("Generate Insights")
|
| 191 |
tbl_ins = gr.Dataframe()
|
| 192 |
btn_ins.click(fn=compute_insights, outputs=tbl_ins)
|
| 193 |
|
| 194 |
+
# User Management Tab
|
| 195 |
+
with gr.Tab("User Management"):
|
| 196 |
+
gr.Markdown("### π§βπΌ Manage Users\nEdit, add, or remove rows, then click **Save Users**.")
|
| 197 |
+
users_df = gr.Dataframe(
|
| 198 |
+
value=load_users(),
|
| 199 |
+
label="Users",
|
| 200 |
+
interactive=True
|
| 201 |
+
)
|
| 202 |
+
save_btn = gr.Button("Save Users")
|
| 203 |
+
save_status = gr.Markdown()
|
| 204 |
+
|
| 205 |
+
def on_save(df):
|
| 206 |
+
saved = save_users(pd.DataFrame(df))
|
| 207 |
+
return "β
Users saved!", saved
|
| 208 |
+
|
| 209 |
+
save_btn.click(
|
| 210 |
+
fn=on_save,
|
| 211 |
+
inputs=users_df,
|
| 212 |
+
outputs=[save_status, users_df]
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
app.launch()
|