IAMTFRMZA's picture
Update app.py
d5bcf89 verified
raw
history blame
4.23 kB
import pandas as pd
import gspread
import gradio as gr
from oauth2client.service_account import ServiceAccountCredentials
# ------------------ AUTH ------------------
VALID_USERS = {
"[email protected]": "Pass.123",
"[email protected]": "Pass.123",
"[email protected]": "Pass.123",
"[email protected]": "Pass.123"
}
# ------------------ GOOGLE SHEET SETUP ------------------
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"
# ------------------ SHEET REFRESH FUNCTIONS ------------------
def load_sheet(sheet_name):
sheet = client.open_by_url(sheet_url).worksheet(sheet_name)
data = sheet.get_all_records()
df = pd.DataFrame(data)
return df
# ------------------ REPORTS TAB ------------------
def filter_calls_by_date(date):
df = load_sheet("Calls")
df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date.astype(str)
return df[df['Date'] == date]
def filter_appointments_by_date(date):
df = load_sheet("Appointments")
df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date.astype(str)
return df[df['Date'] == date]
# ------------------ APPOINTED LEADS ------------------
def appointed_leads_table():
df = load_sheet("Appointed Leads")
grouped = df.groupby('Rep')['Customer Name'].apply(list).reset_index()
return grouped
# ------------------ INTERACTIVE QUERY VIEW ------------------
def search_table(sheet_name, field, keyword):
df = load_sheet(sheet_name)
if field not in df.columns:
return pd.DataFrame(), "Field not found."
results = df[df[field].astype(str).str.contains(keyword, case=False, na=False)]
return results, f"Found {len(results)} results."
# ------------------ GRADIO UI ------------------
with gr.Blocks() as app:
with gr.Row():
with gr.Column(visible=True) as login_ui:
gr.Markdown("## πŸ” Login Required")
email = gr.Textbox(label="Email")
password = gr.Textbox(label="Password", type="password")
login_btn = gr.Button("Login")
login_msg = gr.Markdown("")
with gr.Column(visible=False) as main_ui:
gr.Markdown("## πŸ—‚οΈ Graffiti Admin Dashboard")
with gr.Tab("πŸ“… Calls Report"):
calls_date = gr.Textbox(label="Enter Date (YYYY-MM-DD)")
calls_table = gr.Dataframe()
calls_date.change(lambda d: filter_calls_by_date(d), inputs=calls_date, outputs=calls_table)
with gr.Tab("πŸ“… Appointments Report"):
appt_date = gr.Textbox(label="Enter Date (YYYY-MM-DD)")
appt_table = gr.Dataframe()
appt_date.change(lambda d: filter_appointments_by_date(d), inputs=appt_date, outputs=appt_table)
with gr.Tab("πŸ§‘ Appointed Leads"):
leads_btn = gr.Button("View Appointed Leads")
leads_output = gr.Dataframe()
leads_btn.click(fn=appointed_leads_table, outputs=leads_output)
with gr.Tab("πŸ” Query Live Sheets"):
sheet_choice = gr.Dropdown(choices=["LiveQuotes", "LiveCustomer", "LiveJobBags"], label="Select Sheet")
field_input = gr.Textbox(label="Field (column name)")
keyword_input = gr.Textbox(label="Keyword to search")
query_btn = gr.Button("Search")
query_table = gr.Dataframe()
query_info = gr.Markdown()
query_btn.click(fn=search_table, inputs=[sheet_choice, field_input, keyword_input], outputs=[query_table, query_info])
def do_login(user, pw):
if VALID_USERS.get(user) == pw:
return gr.update(visible=False), gr.update(visible=True), ""
else:
return gr.update(visible=True), gr.update(visible=False), "❌ Invalid email or password."
login_btn.click(fn=do_login, inputs=[email, password], outputs=[login_ui, main_ui, login_msg])
app.launch()