IAMTFRMZA commited on
Commit
f68916e
Β·
verified Β·
1 Parent(s): 106b612

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -20
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
- sheet = client.open_by_url(sheet_url).worksheet("Calls")
20
- data = sheet.get_all_records()
21
- df = pd.DataFrame(data)
22
-
23
- # ------------------ DATA CLEANING ------------------
24
- df['Timestamp'] = pd.to_datetime(df['Timestamp'], dayfirst=True, errors='coerce')
25
- df['Date'] = df['Timestamp'].dt.date.astype(str)
26
- df['Time'] = df['Timestamp'].dt.time
27
- location_split = df['Location'].str.split(',', expand=True)
28
- df['Latitude'] = pd.to_numeric(location_split[0], errors='coerce')
29
- df['Longitude'] = pd.to_numeric(location_split[1], errors='coerce')
30
- df = df.dropna(subset=['Date', 'Rep Name', 'Latitude', 'Longitude'])
31
- df = df[(df['Latitude'] != 0) & (df['Longitude'] != 0)]
32
- df = df.sort_values(by=['Rep Name', 'Timestamp'])
33
- df['Time Diff (min)'] = df.groupby(['Rep Name', 'Date'])['Timestamp'].diff().dt.total_seconds().div(60).fillna(0)
34
- df['Visit Order'] = df.groupby(['Rep Name', 'Date']).cumcount() + 1
35
- ALL_REPS = sorted(df['Rep Name'].dropna().unique())
 
 
 
 
 
 
 
 
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 ALL_REPS if rep not in active_list]
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=sorted(df['Date'].unique(), reverse=True))
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=sorted(df['Date'].unique(), reverse=True))
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):