Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,23 +4,22 @@ import gspread
|
|
4 |
from oauth2client.service_account import ServiceAccountCredentials
|
5 |
from datetime import datetime
|
6 |
|
7 |
-
#
|
8 |
USER_CREDENTIALS = {"[email protected]": "Pass.123"}
|
9 |
|
10 |
-
#
|
11 |
def get_sheet_data():
|
12 |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
13 |
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
14 |
client = gspread.authorize(creds)
|
15 |
-
sheet = client.
|
16 |
data = pd.DataFrame(sheet.get_all_records())
|
17 |
return data
|
18 |
|
19 |
-
#
|
20 |
def generate_report(data, period="Daily"):
|
21 |
data["Date"] = pd.to_datetime(data.get("Date", datetime.today()))
|
22 |
|
23 |
-
# Time filter
|
24 |
now = pd.Timestamp.today()
|
25 |
if period == "Weekly":
|
26 |
data = data[data["Date"] >= now - pd.Timedelta(days=7)]
|
@@ -29,7 +28,6 @@ def generate_report(data, period="Daily"):
|
|
29 |
elif period == "Yearly":
|
30 |
data = data[data["Date"].dt.year == now.year]
|
31 |
|
32 |
-
# Key metrics
|
33 |
total_visits = len(data)
|
34 |
current = len(data[data["Current/Prospect Custor"] == "Current"])
|
35 |
new = len(data[data["Current/Prospect Custor"] == "Prospect"])
|
@@ -52,19 +50,20 @@ def generate_report(data, period="Daily"):
|
|
52 |
f"- Total Order Value: ${order_value:,.2f}"
|
53 |
)
|
54 |
|
55 |
-
#
|
56 |
def login(email, password):
|
57 |
if USER_CREDENTIALS.get(email) == password:
|
58 |
return gr.update(visible=True), gr.update(visible=False), ""
|
59 |
else:
|
60 |
-
return gr.update(visible=False), gr.update(visible=True), "Invalid credentials"
|
61 |
|
|
|
62 |
def report(period):
|
63 |
data = get_sheet_data()
|
64 |
return generate_report(data, period)
|
65 |
|
66 |
-
#
|
67 |
-
with gr.Blocks() as
|
68 |
with gr.Row(visible=True) as login_row:
|
69 |
email = gr.Textbox(label="Email")
|
70 |
password = gr.Textbox(label="Password", type="password")
|
@@ -72,11 +71,11 @@ with gr.Blocks() as demo:
|
|
72 |
login_error = gr.Textbox(label="", visible=False)
|
73 |
|
74 |
with gr.Row(visible=False) as dashboard:
|
75 |
-
period_dropdown = gr.Dropdown(["Daily", "Weekly", "Monthly", "Yearly"], value="Daily", label="Select Period")
|
76 |
-
report_output = gr.Markdown()
|
77 |
report_btn = gr.Button("Generate Report")
|
|
|
78 |
|
79 |
login_btn.click(fn=login, inputs=[email, password], outputs=[dashboard, login_error, login_error])
|
80 |
report_btn.click(fn=report, inputs=[period_dropdown], outputs=report_output)
|
81 |
|
82 |
-
|
|
|
4 |
from oauth2client.service_account import ServiceAccountCredentials
|
5 |
from datetime import datetime
|
6 |
|
7 |
+
# -- Login Setup --
|
8 |
USER_CREDENTIALS = {"[email protected]": "Pass.123"}
|
9 |
|
10 |
+
# -- Load Google Sheets Data --
|
11 |
def get_sheet_data():
|
12 |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
13 |
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
14 |
client = gspread.authorize(creds)
|
15 |
+
sheet = client.open_by_title("userAccess (6)").worksheet("Field Sales")
|
16 |
data = pd.DataFrame(sheet.get_all_records())
|
17 |
return data
|
18 |
|
19 |
+
# -- Reporting Logic --
|
20 |
def generate_report(data, period="Daily"):
|
21 |
data["Date"] = pd.to_datetime(data.get("Date", datetime.today()))
|
22 |
|
|
|
23 |
now = pd.Timestamp.today()
|
24 |
if period == "Weekly":
|
25 |
data = data[data["Date"] >= now - pd.Timedelta(days=7)]
|
|
|
28 |
elif period == "Yearly":
|
29 |
data = data[data["Date"].dt.year == now.year]
|
30 |
|
|
|
31 |
total_visits = len(data)
|
32 |
current = len(data[data["Current/Prospect Custor"] == "Current"])
|
33 |
new = len(data[data["Current/Prospect Custor"] == "Prospect"])
|
|
|
50 |
f"- Total Order Value: ${order_value:,.2f}"
|
51 |
)
|
52 |
|
53 |
+
# -- Login Handler --
|
54 |
def login(email, password):
|
55 |
if USER_CREDENTIALS.get(email) == password:
|
56 |
return gr.update(visible=True), gr.update(visible=False), ""
|
57 |
else:
|
58 |
+
return gr.update(visible=False), gr.update(visible=True), "❌ Invalid credentials"
|
59 |
|
60 |
+
# -- UI Logic --
|
61 |
def report(period):
|
62 |
data = get_sheet_data()
|
63 |
return generate_report(data, period)
|
64 |
|
65 |
+
# -- Gradio App --
|
66 |
+
with gr.Blocks() as app:
|
67 |
with gr.Row(visible=True) as login_row:
|
68 |
email = gr.Textbox(label="Email")
|
69 |
password = gr.Textbox(label="Password", type="password")
|
|
|
71 |
login_error = gr.Textbox(label="", visible=False)
|
72 |
|
73 |
with gr.Row(visible=False) as dashboard:
|
74 |
+
period_dropdown = gr.Dropdown(["Daily", "Weekly", "Monthly", "Yearly"], value="Daily", label="Select Report Period")
|
|
|
75 |
report_btn = gr.Button("Generate Report")
|
76 |
+
report_output = gr.Markdown()
|
77 |
|
78 |
login_btn.click(fn=login, inputs=[email, password], outputs=[dashboard, login_error, login_error])
|
79 |
report_btn.click(fn=report, inputs=[period_dropdown], outputs=report_output)
|
80 |
|
81 |
+
app.launch()
|