IAMTFRMZA commited on
Commit
a137762
·
verified ·
1 Parent(s): 65e2013

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -10
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) # Convert to string
21
  df['Time'] = df['Timestamp'].dt.time
22
 
23
  location_split = df['Location'].str.split(',', expand=True)
@@ -38,20 +38,49 @@ def show_map(date_str, rep):
38
  if subset.empty:
39
  return "No valid data", None
40
 
41
- subset = subset.sort_values(by='Timestamp')
 
 
 
 
 
 
 
42
  fig = px.line_mapbox(
43
  subset,
44
  lat="Latitude", lon="Longitude",
45
  hover_name="Dealership Name",
46
- hover_data={"Time": True, "Time Diff (min)": True},
47
- zoom=10, height=500
 
 
 
 
 
 
 
 
 
 
 
 
48
  )
49
- fig.add_trace(px.scatter_mapbox(pd.DataFrame([subset.iloc[0]]),
50
- lat="Latitude", lon="Longitude", text=["Start"], color_discrete_sequence=["green"]).data[0])
51
- fig.add_trace(px.scatter_mapbox(pd.DataFrame([subset.iloc[-1]]),
52
- lat="Latitude", lon="Longitude", text=["End"], color_discrete_sequence=["red"]).data[0])
53
- fig.update_layout(mapbox_style="open-street-map", title=f"{rep} on {date_str}")
54
- table = subset[['Dealership Name', 'Time', 'Time Diff (min)']]
 
 
 
 
 
 
 
 
 
 
55
  return table, fig
56
 
57
  # === 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) # Convert to string
21
  df['Time'] = df['Timestamp'].dt.time
22
 
23
  location_split = df['Location'].str.split(',', expand=True)
 
38
  if subset.empty:
39
  return "No valid data", None
40
 
41
+ subset = subset.sort_values(by='Timestamp').copy()
42
+ subset['Visit Order'] = range(1, len(subset) + 1)
43
+
44
+ # Center and size for better visibility
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",
52
  hover_name="Dealership Name",
53
+ hover_data={"Time": True, "Time Diff (min)": True, "Visit Order": True},
54
+ height=700,
55
+ zoom=13,
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",
63
+ color="Visit Order",
64
+ hover_name="Dealership Name",
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",
76
+ text=["Start"], color_discrete_sequence=["green"]).data[0])
77
+ fig.add_trace(px.scatter_mapbox(
78
+ pd.DataFrame([subset.iloc[-1]]),
79
+ lat="Latitude", lon="Longitude",
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
+ table = subset[['Dealership Name', 'Time', 'Time Diff (min)', 'Visit Order']]
84
  return table, fig
85
 
86
  # === Gradio UI ===