Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
"andrew@lortechnologies.com": "Pass.123",
|
| 12 |
-
"donovanm@bid4cars.co.za": "Pass.123",
|
| 13 |
-
"grant@bid4cars.co.za": "Pass.123",
|
| 14 |
-
"phonnie@c2group.co.za": "Pass.123"
|
| 15 |
-
}
|
| 16 |
|
| 17 |
-
#
|
| 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 |
-
#
|
| 24 |
-
def
|
| 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(
|
| 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 |
-
|
| 46 |
-
|
| 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 |
-
|
| 55 |
-
|
| 56 |
-
return
|
| 57 |
|
| 58 |
def current_week_appointments():
|
| 59 |
df = load_sheet("Appointments")
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
return
|
| 63 |
|
| 64 |
-
def
|
| 65 |
df = load_sheet("Appointed Leads")
|
| 66 |
-
if "Rep" in df.columns
|
| 67 |
-
return
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
with gr.
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|