Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,20 @@
|
|
|
|
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 |
-
#
|
8 |
-
|
9 |
"[email protected]": "Pass.123",
|
10 |
"[email protected]": "Pass.123",
|
11 |
"[email protected]": "Pass.123"
|
12 |
}
|
13 |
|
14 |
-
#
|
15 |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
16 |
creds = ServiceAccountCredentials.from_json_keyfile_name("tough-star.json", scope)
|
17 |
client = gspread.authorize(creds)
|
18 |
-
|
19 |
-
# === Load and clean sheet data ===
|
20 |
sheet_url = "https://docs.google.com/spreadsheets/d/1bpeFS6yihb6niCavpwjWmVEypaSkGxONGg2jZfKX_sA"
|
21 |
sheet = client.open_by_url(sheet_url).worksheet("Calls")
|
22 |
data = sheet.get_all_records()
|
@@ -35,15 +33,14 @@ df = df.sort_values(by=['Rep Name', 'Timestamp'])
|
|
35 |
df['Time Diff (min)'] = df.groupby(['Rep Name', 'Date'])['Timestamp'].diff().dt.total_seconds().div(60).fillna(0)
|
36 |
df['Visit Order'] = df.groupby(['Rep Name', 'Date']).cumcount() + 1
|
37 |
|
38 |
-
|
39 |
-
all_reps = sorted(df['Rep Name'].dropna().unique())
|
40 |
|
41 |
-
#
|
42 |
def generate_summary(date_str):
|
43 |
day_df = df[df['Date'] == date_str]
|
44 |
active = day_df.groupby('Rep Name').size().reset_index(name='Total Visits')
|
45 |
active_list = active['Rep Name'].tolist()
|
46 |
-
inactive_list = [rep for rep in
|
47 |
inactive_df = pd.DataFrame({'Inactive Reps': inactive_list})
|
48 |
return active, inactive_df
|
49 |
|
@@ -93,8 +90,8 @@ def show_map(date_str, rep):
|
|
93 |
|
94 |
fig.update_layout(mapbox_style="open-street-map", title=f"π {rep}'s Route on {date_str}")
|
95 |
|
96 |
-
table = subset[[
|
97 |
-
'Visit Order', 'Dealership Name', 'Time', 'Time Diff (min)',
|
98 |
'Type of call', 'Sales or service'
|
99 |
]].rename(columns={
|
100 |
'Dealership Name': 'π§ Dealer',
|
@@ -114,57 +111,47 @@ def show_map(date_str, rep):
|
|
114 |
'πΌ Category': ''
|
115 |
}])
|
116 |
table = pd.concat([table, summary_row], ignore_index=True)
|
117 |
-
|
118 |
return table, fig
|
119 |
|
120 |
-
#
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
password = gr.Textbox(label="Password", type="password")
|
157 |
-
login_btn = gr.Button("Login")
|
158 |
-
login_status = gr.Markdown("")
|
159 |
-
|
160 |
-
def do_login(user_email, user_password):
|
161 |
-
if check_login(user_email, user_password):
|
162 |
-
import gradio as gr2
|
163 |
-
login_app.close()
|
164 |
-
main_app().launch(share=False) # launch the main app
|
165 |
else:
|
166 |
-
return "β
|
167 |
|
168 |
-
login_btn.click(fn=
|
169 |
|
170 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import gspread
|
4 |
from oauth2client.service_account import ServiceAccountCredentials
|
|
|
5 |
import plotly.express as px
|
6 |
|
7 |
+
# ------------------ Authentication ------------------
|
8 |
+
VALID_USERS = {
|
9 |
"[email protected]": "Pass.123",
|
10 |
"[email protected]": "Pass.123",
|
11 |
"[email protected]": "Pass.123"
|
12 |
}
|
13 |
|
14 |
+
# ------------------ Data Setup ------------------
|
15 |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
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()
|
|
|
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 |
|
36 |
+
ALL_REPS = sorted(df['Rep Name'].dropna().unique())
|
|
|
37 |
|
38 |
+
# ------------------ App Logic ------------------
|
39 |
def generate_summary(date_str):
|
40 |
day_df = df[df['Date'] == date_str]
|
41 |
active = day_df.groupby('Rep Name').size().reset_index(name='Total Visits')
|
42 |
active_list = active['Rep Name'].tolist()
|
43 |
+
inactive_list = [rep for rep in ALL_REPS if rep not in active_list]
|
44 |
inactive_df = pd.DataFrame({'Inactive Reps': inactive_list})
|
45 |
return active, inactive_df
|
46 |
|
|
|
90 |
|
91 |
fig.update_layout(mapbox_style="open-street-map", title=f"π {rep}'s Route on {date_str}")
|
92 |
|
93 |
+
table = subset[[
|
94 |
+
'Visit Order', 'Dealership Name', 'Time', 'Time Diff (min)',
|
95 |
'Type of call', 'Sales or service'
|
96 |
]].rename(columns={
|
97 |
'Dealership Name': 'π§ Dealer',
|
|
|
111 |
'πΌ Category': ''
|
112 |
}])
|
113 |
table = pd.concat([table, summary_row], ignore_index=True)
|
|
|
114 |
return table, fig
|
115 |
|
116 |
+
# ------------------ Login + Protected UI ------------------
|
117 |
+
with gr.Blocks() as app:
|
118 |
+
state = gr.State(False)
|
119 |
+
|
120 |
+
with gr.Row():
|
121 |
+
with gr.Column(scale=1):
|
122 |
+
login_title = gr.Markdown("## π Login Required")
|
123 |
+
email = gr.Textbox(label="Email")
|
124 |
+
password = gr.Textbox(label="Password", type="password")
|
125 |
+
login_btn = gr.Button("Login")
|
126 |
+
login_msg = gr.Markdown("")
|
127 |
+
|
128 |
+
with gr.Column(visible=False) as main_ui:
|
129 |
+
gr.Markdown("## ποΈ Carfind Rep Tracker")
|
130 |
+
with gr.Tab("π Summary"):
|
131 |
+
date_summary = gr.Dropdown(label="Select Date", choices=sorted(df['Date'].unique(), reverse=True))
|
132 |
+
active_table = gr.Dataframe(label="β
Active Reps (with total visits)")
|
133 |
+
inactive_table = gr.Dataframe(label="β οΈ Inactive Reps")
|
134 |
+
date_summary.change(fn=generate_summary, inputs=date_summary, outputs=[active_table, inactive_table])
|
135 |
+
|
136 |
+
with gr.Tab("π€ KAM's"):
|
137 |
+
with gr.Row():
|
138 |
+
with gr.Column(scale=1):
|
139 |
+
date_picker = gr.Dropdown(label="Select Date", choices=sorted(df['Date'].unique(), reverse=True))
|
140 |
+
rep_picker = gr.Dropdown(label="Select Rep")
|
141 |
+
btn = gr.Button("Show Route")
|
142 |
+
with gr.Column(scale=2):
|
143 |
+
table = gr.Dataframe(label="Call Table")
|
144 |
+
|
145 |
+
map_plot = gr.Plot(label="Map")
|
146 |
+
date_picker.change(fn=get_reps, inputs=date_picker, outputs=rep_picker)
|
147 |
+
btn.click(fn=show_map, inputs=[date_picker, rep_picker], outputs=[table, map_plot])
|
148 |
+
|
149 |
+
def check_login(u, p):
|
150 |
+
if VALID_USERS.get(u) == p:
|
151 |
+
return gr.update(visible=False), gr.update(visible=True), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
else:
|
153 |
+
return gr.update(), gr.update(), "β Incorrect email or password."
|
154 |
|
155 |
+
login_btn.click(fn=check_login, inputs=[email, password], outputs=[login_title, main_ui, login_msg])
|
156 |
|
157 |
+
app.launch()
|