Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,38 +16,50 @@ scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/au
|
|
| 16 |
creds = ServiceAccountCredentials.from_json_keyfile_name("tough-star.json", scope)
|
| 17 |
client = gspread.authorize(creds)
|
| 18 |
sheet_url = "https://docs.google.com/spreadsheets/d/1bpeFS6yihb6niCavpwjWmVEypaSkGxONGg2jZfKX_sA"
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
df
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
df['
|
| 29 |
-
df['
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
df['
|
| 34 |
-
df['
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# ------------------ DASHBOARD FUNCTIONS ------------------
|
| 38 |
def generate_summary(date_str):
|
|
|
|
|
|
|
| 39 |
day_df = df[df['Date'] == date_str]
|
| 40 |
active = day_df.groupby('Rep Name').size().reset_index(name='Total Visits')
|
| 41 |
active_list = active['Rep Name'].tolist()
|
| 42 |
-
inactive_list = [rep for rep in
|
| 43 |
inactive_df = pd.DataFrame({'Inactive Reps': inactive_list})
|
| 44 |
return active, inactive_df
|
| 45 |
|
| 46 |
def get_reps(date_str):
|
|
|
|
| 47 |
reps = df[df['Date'] == date_str]['Rep Name'].dropna().unique()
|
| 48 |
return gr.update(choices=sorted(reps))
|
| 49 |
|
| 50 |
def show_map(date_str, rep):
|
|
|
|
| 51 |
subset = df[(df['Date'] == date_str) & (df['Rep Name'] == rep)]
|
| 52 |
if subset.empty:
|
| 53 |
return "No valid data", None
|
|
@@ -124,8 +136,11 @@ with gr.Blocks() as app:
|
|
| 124 |
|
| 125 |
with gr.Column(visible=False) as main_ui:
|
| 126 |
gr.Markdown("## ๐๏ธ Carfind Rep Tracker")
|
|
|
|
|
|
|
|
|
|
| 127 |
with gr.Tab("๐ Summary"):
|
| 128 |
-
date_summary = gr.Dropdown(label="Select Date", choices=
|
| 129 |
active_table = gr.Dataframe(label="โ
Active Reps (with total visits)")
|
| 130 |
inactive_table = gr.Dataframe(label="โ ๏ธ Inactive Reps")
|
| 131 |
date_summary.change(fn=generate_summary, inputs=date_summary, outputs=[active_table, inactive_table])
|
|
@@ -133,7 +148,7 @@ with gr.Blocks() as app:
|
|
| 133 |
with gr.Tab("๐ค KAM's"):
|
| 134 |
with gr.Row():
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
-
date_picker = gr.Dropdown(label="Select Date", choices=
|
| 137 |
rep_picker = gr.Dropdown(label="Select Rep")
|
| 138 |
btn = gr.Button("Show Route")
|
| 139 |
with gr.Column(scale=2):
|
|
|
|
| 16 |
creds = ServiceAccountCredentials.from_json_keyfile_name("tough-star.json", scope)
|
| 17 |
client = gspread.authorize(creds)
|
| 18 |
sheet_url = "https://docs.google.com/spreadsheets/d/1bpeFS6yihb6niCavpwjWmVEypaSkGxONGg2jZfKX_sA"
|
| 19 |
+
|
| 20 |
+
# ------------------ DATA REFRESH FUNCTION ------------------
|
| 21 |
+
def refresh_data():
|
| 22 |
+
sheet = client.open_by_url(sheet_url).worksheet("Calls")
|
| 23 |
+
data = sheet.get_all_records()
|
| 24 |
+
df = pd.DataFrame(data)
|
| 25 |
+
|
| 26 |
+
# Timestamp parsing
|
| 27 |
+
df['Timestamp'] = pd.to_datetime(df['Timestamp'], dayfirst=True, errors='coerce')
|
| 28 |
+
df['Date'] = df['Timestamp'].dt.date.astype(str)
|
| 29 |
+
df['Time'] = df['Timestamp'].dt.time
|
| 30 |
+
|
| 31 |
+
# Location parsing
|
| 32 |
+
location_split = df['Location'].str.split(',', expand=True)
|
| 33 |
+
df['Latitude'] = pd.to_numeric(location_split[0], errors='coerce')
|
| 34 |
+
df['Longitude'] = pd.to_numeric(location_split[1], errors='coerce')
|
| 35 |
+
|
| 36 |
+
# Data cleaning
|
| 37 |
+
df = df.dropna(subset=['Date', 'Rep Name', 'Latitude', 'Longitude'])
|
| 38 |
+
df = df[(df['Latitude'] != 0) & (df['Longitude'] != 0)]
|
| 39 |
+
df = df.sort_values(by=['Rep Name', 'Timestamp'])
|
| 40 |
+
df['Time Diff (min)'] = df.groupby(['Rep Name', 'Date'])['Timestamp'].diff().dt.total_seconds().div(60).fillna(0)
|
| 41 |
+
df['Visit Order'] = df.groupby(['Rep Name', 'Date']).cumcount() + 1
|
| 42 |
+
|
| 43 |
+
return df
|
| 44 |
|
| 45 |
# ------------------ DASHBOARD FUNCTIONS ------------------
|
| 46 |
def generate_summary(date_str):
|
| 47 |
+
df = refresh_data()
|
| 48 |
+
all_reps = sorted(df['Rep Name'].dropna().unique())
|
| 49 |
day_df = df[df['Date'] == date_str]
|
| 50 |
active = day_df.groupby('Rep Name').size().reset_index(name='Total Visits')
|
| 51 |
active_list = active['Rep Name'].tolist()
|
| 52 |
+
inactive_list = [rep for rep in all_reps if rep not in active_list]
|
| 53 |
inactive_df = pd.DataFrame({'Inactive Reps': inactive_list})
|
| 54 |
return active, inactive_df
|
| 55 |
|
| 56 |
def get_reps(date_str):
|
| 57 |
+
df = refresh_data()
|
| 58 |
reps = df[df['Date'] == date_str]['Rep Name'].dropna().unique()
|
| 59 |
return gr.update(choices=sorted(reps))
|
| 60 |
|
| 61 |
def show_map(date_str, rep):
|
| 62 |
+
df = refresh_data()
|
| 63 |
subset = df[(df['Date'] == date_str) & (df['Rep Name'] == rep)]
|
| 64 |
if subset.empty:
|
| 65 |
return "No valid data", None
|
|
|
|
| 136 |
|
| 137 |
with gr.Column(visible=False) as main_ui:
|
| 138 |
gr.Markdown("## ๐๏ธ Carfind Rep Tracker")
|
| 139 |
+
df_initial = refresh_data()
|
| 140 |
+
unique_dates = sorted(df_initial['Date'].unique(), reverse=True)
|
| 141 |
+
|
| 142 |
with gr.Tab("๐ Summary"):
|
| 143 |
+
date_summary = gr.Dropdown(label="Select Date", choices=unique_dates)
|
| 144 |
active_table = gr.Dataframe(label="โ
Active Reps (with total visits)")
|
| 145 |
inactive_table = gr.Dataframe(label="โ ๏ธ Inactive Reps")
|
| 146 |
date_summary.change(fn=generate_summary, inputs=date_summary, outputs=[active_table, inactive_table])
|
|
|
|
| 148 |
with gr.Tab("๐ค KAM's"):
|
| 149 |
with gr.Row():
|
| 150 |
with gr.Column(scale=1):
|
| 151 |
+
date_picker = gr.Dropdown(label="Select Date", choices=unique_dates)
|
| 152 |
rep_picker = gr.Dropdown(label="Select Rep")
|
| 153 |
btn = gr.Button("Show Route")
|
| 154 |
with gr.Column(scale=2):
|