IAMTFRMZA commited on
Commit
44e7320
·
verified ·
1 Parent(s): cdac651

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gspread
3
+ from oauth2client.service_account import ServiceAccountCredentials
4
+ import gradio as gr
5
+ import plotly.express as px
6
+
7
+ # === Google Sheets Auth ===
8
+ scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
9
+ creds = ServiceAccountCredentials.from_json_keyfile_name("tough-star.json", scope)
10
+ client = gspread.authorize(creds)
11
+
12
+ # === Load sheet data ===
13
+ sheet_url = "https://docs.google.com/spreadsheets/d/1bpeFS6yihb6niCavpwjWmVEypaSkGxONGg2jZfKX_sA"
14
+ sheet = client.open_by_url(sheet_url).worksheet("Calls")
15
+ data = sheet.get_all_records()
16
+ 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
21
+ df['Time'] = df['Timestamp'].dt.time
22
+
23
+ location_split = df['Location'].str.split(',', expand=True)
24
+ df['Latitude'] = pd.to_numeric(location_split[0], errors='coerce')
25
+ df['Longitude'] = pd.to_numeric(location_split[1], errors='coerce')
26
+ df = df.dropna(subset=['Date', 'Rep Name', 'Latitude', 'Longitude'])
27
+ 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):
33
+ reps = df[df['Date'] == pd.to_datetime(date).date()]['Rep Name'].dropna().unique()
34
+ return sorted(reps)
35
+
36
+ def show_map(date, rep):
37
+ subset = df[(df['Date'] == pd.to_datetime(date).date()) & (df['Rep Name'] == 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}")
54
+ table = subset[['Dealership Name', 'Time', 'Time Diff (min)']]
55
+ return table, fig
56
+
57
+ # === Gradio UI ===
58
+ def update(date):
59
+ return gr.Dropdown(choices=get_reps(date), label="Select Rep")
60
+
61
+ with gr.Blocks() as app:
62
+ gr.Markdown("## 🗺️ Rep Route & Visit Visualizer")
63
+
64
+ date_picker = gr.Dropdown(label="Select Date", choices=sorted(df['Date'].unique(), reverse=True))
65
+ rep_picker = gr.Dropdown(label="Select Rep")
66
+
67
+ btn = gr.Button("Show Route")
68
+
69
+ table = gr.Dataframe(label="Call Table")
70
+ map_plot = gr.Plot(label="Map")
71
+
72
+ date_picker.change(fn=update, inputs=date_picker, outputs=rep_picker)
73
+ btn.click(fn=show_map, inputs=[date_picker, rep_picker], outputs=[table, map_plot])
74
+
75
+ app.launch()