Spaces:
Sleeping
Sleeping
# app.py | |
import gradio as gr | |
import pandas as pd | |
import gspread | |
from oauth2client.service_account import ServiceAccountCredentials | |
from datetime import datetime, timedelta | |
# -------------------- AUTH -------------------- | |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"] | |
creds = ServiceAccountCredentials.from_json_keyfile_name("deep-mile-461309-t8-0e90103411e0.json", scope) | |
client = gspread.authorize(creds) | |
sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ" | |
# -------------------- UTILS -------------------- | |
def load_sheet(sheet_name): | |
try: | |
sheet = client.open_by_url(sheet_url).worksheet(sheet_name) | |
df = pd.DataFrame(sheet.get_all_records()) | |
return df | |
except Exception as e: | |
return pd.DataFrame([{"Error": str(e)}]) | |
def get_current_week_range(): | |
today = datetime.now() | |
start = today - timedelta(days=today.weekday()) | |
end = start + timedelta(days=6) | |
return start.date(), end.date() | |
def compose_date(year, month, day): | |
try: | |
return str(datetime(int(year), int(month), int(day)).date()) | |
except: | |
return None | |
# -------------------- REPORT FUNCTIONS -------------------- | |
def current_week_calls(): | |
df = load_sheet("Calls") | |
if "Call Date" not in df.columns: | |
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}]) | |
df["Call Date"] = pd.to_datetime(df["Call Date"], errors='coerce').dt.date | |
start, end = get_current_week_range() | |
return df[(df["Call Date"] >= start) & (df["Call Date"] <= end)] | |
def custom_date_calls(y, m, d): | |
date_str = compose_date(y, m, d) | |
if not date_str: | |
return pd.DataFrame([{"Error": "Invalid date input"}]) | |
df = load_sheet("Calls") | |
df["Call Date"] = pd.to_datetime(df["Call Date"], errors='coerce').dt.date.astype(str) | |
return df[df["Call Date"] == date_str] | |
def current_week_appointments(): | |
df = load_sheet("Appointments") | |
if "Appointment Date" not in df.columns: | |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}]) | |
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors='coerce').dt.date | |
start, end = get_current_week_range() | |
return df[(df["Appointment Date"] >= start) & (df["Appointment Date"] <= end)] | |
def custom_date_appointments(y, m, d): | |
date_str = compose_date(y, m, d) | |
if not date_str: | |
return pd.DataFrame([{"Error": "Invalid date input"}]) | |
df = load_sheet("Appointments") | |
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors='coerce').dt.date.astype(str) | |
return df[df["Appointment Date"] == date_str] | |
def appointed_leads(): | |
df = load_sheet("AllocatedLeads") | |
if "Rep" not in df.columns or "Company Name" not in df.columns: | |
return pd.DataFrame([{"Error": "Missing 'Rep' or 'Company Name' column"}]) | |
return df.groupby("Rep")["Company Name"].apply(list).reset_index() | |
# -------------------- UI -------------------- | |
with gr.Blocks(title="Graffiti Admin Dashboard") as app: | |
gr.Markdown("## π Graffiti Admin Dashboard") | |
with gr.Tab("π Calls Report"): | |
calls_btn = gr.Button("Load Current Week Calls") | |
calls_table = gr.Dataframe() | |
calls_btn.click(fn=current_week_calls, outputs=calls_table) | |
gr.Markdown("### π Search Calls by Specific Date") | |
cy, cm, cd = gr.Textbox(label="Year"), gr.Textbox(label="Month"), gr.Textbox(label="Day") | |
custom_calls_btn = gr.Button("Search Calls by Date") | |
custom_calls_table = gr.Dataframe() | |
custom_calls_btn.click(fn=custom_date_calls, inputs=[cy, cm, cd], outputs=custom_calls_table) | |
with gr.Tab("π Appointments Report"): | |
appt_btn = gr.Button("Load Current Week Appointments") | |
appt_table = gr.Dataframe() | |
appt_btn.click(fn=current_week_appointments, outputs=appt_table) | |
gr.Markdown("### π Search Appointments by Specific Date") | |
ay, am, ad = gr.Textbox(label="Year"), gr.Textbox(label="Month"), gr.Textbox(label="Day") | |
custom_appt_btn = gr.Button("Search Appointments by Date") | |
custom_appt_table = gr.Dataframe() | |
custom_appt_btn.click(fn=custom_date_appointments, inputs=[ay, am, ad], outputs=custom_appt_table) | |
with gr.Tab("π₯ Appointed Leads"): | |
leads_btn = gr.Button("View Appointed Leads") | |
leads_table = gr.Dataframe() | |
leads_btn.click(fn=appointed_leads, outputs=leads_table) | |
app.launch() | |