Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ df = pd.DataFrame(data)
|
|
17 |
|
18 |
# === Parse and clean ===
|
19 |
df['Timestamp'] = pd.to_datetime(df['Timestamp'], dayfirst=True, errors='coerce')
|
20 |
-
df['Date'] = df['Timestamp'].dt.date.astype(str)
|
21 |
df['Time'] = df['Timestamp'].dt.time
|
22 |
|
23 |
location_split = df['Location'].str.split(',', expand=True)
|
@@ -28,6 +28,14 @@ df = df[(df['Latitude'] != 0) & (df['Longitude'] != 0)]
|
|
28 |
df = df.sort_values(by=['Rep Name', 'Timestamp'])
|
29 |
df['Time Diff (min)'] = df.groupby(['Rep Name', 'Date'])['Timestamp'].diff().dt.total_seconds().div(60).fillna(0)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# === Functions ===
|
32 |
def get_reps(date_str):
|
33 |
reps = df[df['Date'] == date_str]['Rep Name'].dropna().unique()
|
@@ -41,11 +49,10 @@ def show_map(date_str, rep):
|
|
41 |
subset = subset.sort_values(by='Timestamp').copy()
|
42 |
subset['Visit Order'] = range(1, len(subset) + 1)
|
43 |
|
44 |
-
# Center and
|
45 |
center_lat = subset['Latitude'].mean()
|
46 |
center_lon = subset['Longitude'].mean()
|
47 |
|
48 |
-
# Line + colored scatter
|
49 |
fig = px.line_mapbox(
|
50 |
subset,
|
51 |
lat="Latitude", lon="Longitude",
|
@@ -56,7 +63,6 @@ def show_map(date_str, rep):
|
|
56 |
center={"lat": center_lat, "lon": center_lon}
|
57 |
)
|
58 |
|
59 |
-
# Colored points by visit order
|
60 |
scatter = px.scatter_mapbox(
|
61 |
subset,
|
62 |
lat="Latitude", lon="Longitude",
|
@@ -65,11 +71,9 @@ def show_map(date_str, rep):
|
|
65 |
hover_data=["Time", "Time Diff (min)"],
|
66 |
color_continuous_scale="Bluered"
|
67 |
)
|
68 |
-
|
69 |
for trace in scatter.data:
|
70 |
fig.add_trace(trace)
|
71 |
|
72 |
-
# Start/End markers
|
73 |
fig.add_trace(px.scatter_mapbox(
|
74 |
pd.DataFrame([subset.iloc[0]]),
|
75 |
lat="Latitude", lon="Longitude",
|
@@ -80,7 +84,34 @@ def show_map(date_str, rep):
|
|
80 |
text=["End"], color_discrete_sequence=["red"]).data[0])
|
81 |
|
82 |
fig.update_layout(mapbox_style="open-street-map", title=f"๐ {rep}'s Route on {date_str}")
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
return table, fig
|
85 |
|
86 |
# === Gradio UI ===
|
|
|
17 |
|
18 |
# === Parse and clean ===
|
19 |
df['Timestamp'] = pd.to_datetime(df['Timestamp'], dayfirst=True, errors='coerce')
|
20 |
+
df['Date'] = df['Timestamp'].dt.date.astype(str)
|
21 |
df['Time'] = df['Timestamp'].dt.time
|
22 |
|
23 |
location_split = df['Location'].str.split(',', expand=True)
|
|
|
28 |
df = df.sort_values(by=['Rep Name', 'Timestamp'])
|
29 |
df['Time Diff (min)'] = df.groupby(['Rep Name', 'Date'])['Timestamp'].diff().dt.total_seconds().div(60).fillna(0)
|
30 |
|
31 |
+
# Add Visit Order
|
32 |
+
df['Visit Order'] = df.groupby(['Rep Name', 'Date']).cumcount() + 1
|
33 |
+
|
34 |
+
# Construct image thumbnail URLs from Google Drive folder
|
35 |
+
drive_folder_url = "https://drive.google.com/uc?id="
|
36 |
+
df['Image ID'] = df['Image'].str.extract(r'Calls_Images/([^.]+)')
|
37 |
+
df['Image URL'] = df['Image ID'].apply(lambda x: f"{drive_folder_url}{x}" if pd.notna(x) else "")
|
38 |
+
|
39 |
# === Functions ===
|
40 |
def get_reps(date_str):
|
41 |
reps = df[df['Date'] == date_str]['Rep Name'].dropna().unique()
|
|
|
49 |
subset = subset.sort_values(by='Timestamp').copy()
|
50 |
subset['Visit Order'] = range(1, len(subset) + 1)
|
51 |
|
52 |
+
# Center and zoom
|
53 |
center_lat = subset['Latitude'].mean()
|
54 |
center_lon = subset['Longitude'].mean()
|
55 |
|
|
|
56 |
fig = px.line_mapbox(
|
57 |
subset,
|
58 |
lat="Latitude", lon="Longitude",
|
|
|
63 |
center={"lat": center_lat, "lon": center_lon}
|
64 |
)
|
65 |
|
|
|
66 |
scatter = px.scatter_mapbox(
|
67 |
subset,
|
68 |
lat="Latitude", lon="Longitude",
|
|
|
71 |
hover_data=["Time", "Time Diff (min)"],
|
72 |
color_continuous_scale="Bluered"
|
73 |
)
|
|
|
74 |
for trace in scatter.data:
|
75 |
fig.add_trace(trace)
|
76 |
|
|
|
77 |
fig.add_trace(px.scatter_mapbox(
|
78 |
pd.DataFrame([subset.iloc[0]]),
|
79 |
lat="Latitude", lon="Longitude",
|
|
|
84 |
text=["End"], color_discrete_sequence=["red"]).data[0])
|
85 |
|
86 |
fig.update_layout(mapbox_style="open-street-map", title=f"๐ {rep}'s Route on {date_str}")
|
87 |
+
|
88 |
+
# === Build display table
|
89 |
+
table = subset[[
|
90 |
+
'Visit Order', 'Dealership Name', 'Time', 'Time Diff (min)',
|
91 |
+
'Type of call', 'Sales or service', 'Image URL'
|
92 |
+
]]
|
93 |
+
table = table.rename(columns={
|
94 |
+
'Dealership Name': '๐งญ Dealer',
|
95 |
+
'Time': '๐ Time',
|
96 |
+
'Time Diff (min)': 'โฑ๏ธ Time Spent',
|
97 |
+
'Type of call': '๐ Call Type',
|
98 |
+
'Sales or service': '๐ผ Category',
|
99 |
+
'Image URL': '๐ธ Photo'
|
100 |
+
})
|
101 |
+
|
102 |
+
# Add time summary
|
103 |
+
total_time = round(table['โฑ๏ธ Time Spent'].sum(), 2)
|
104 |
+
summary_row = pd.DataFrame([{
|
105 |
+
'๐งญ Dealer': f"๐งฎ Total Time: {total_time} min",
|
106 |
+
'Visit Order': '',
|
107 |
+
'๐ Time': '',
|
108 |
+
'โฑ๏ธ Time Spent': '',
|
109 |
+
'๐ Call Type': '',
|
110 |
+
'๐ผ Category': '',
|
111 |
+
'๐ธ Photo': ''
|
112 |
+
}])
|
113 |
+
table = pd.concat([table, summary_row], ignore_index=True)
|
114 |
+
|
115 |
return table, fig
|
116 |
|
117 |
# === Gradio UI ===
|