IAMTFRMZA commited on
Commit
88ff990
·
verified ·
1 Parent(s): 1b4859e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import gspread
4
+ from oauth2client.service_account import ServiceAccountCredentials
5
+ from datetime import datetime
6
+
7
+ # --- Config ---
8
+ USER_CREDENTIALS = {"[email protected]": "Pass.123"}
9
+
10
+ # --- Google Sheets Setup ---
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("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
+ # Time filter
24
+ now = pd.Timestamp.today()
25
+ if period == "Weekly":
26
+ data = data[data["Date"] >= now - pd.Timedelta(days=7)]
27
+ elif period == "Monthly":
28
+ data = data[data["Date"].dt.month == now.month]
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"])
36
+ second_hand = len(data[data["Customer Type"].str.contains("Second", na=False)])
37
+ telesales = len(data[data["Source"] == "TeleSales"])
38
+ oem_visits = len(data[data["Source"] == "OEM Visit"])
39
+ orders = len(data[data["Order Received"] == "Yes"])
40
+ order_value = data["Order Value"].sum()
41
+
42
+ return (
43
+ f"### {period} Report\n"
44
+ f"- Total Visits: {total_visits}\n"
45
+ f"- Current Dealerships: {current}\n"
46
+ f"- New Dealerships: {new}\n"
47
+ f"- % Current: {current / total_visits * 100:.1f}% | % New: {new / total_visits * 100:.1f}%\n"
48
+ f"- Second-hand Dealerships: {second_hand}\n"
49
+ f"- TeleSales Calls: {telesales}\n"
50
+ f"- OEM Visits: {oem_visits}\n"
51
+ f"- Orders Received: {orders}\n"
52
+ f"- Total Order Value: ${order_value:,.2f}"
53
+ )
54
+
55
+ # --- Interface Functions ---
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
+ # --- Gradio UI ---
67
+ with gr.Blocks() as demo:
68
+ with gr.Row(visible=True) as login_row:
69
+ email = gr.Textbox(label="Email")
70
+ password = gr.Textbox(label="Password", type="password")
71
+ login_btn = gr.Button("Login")
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
+ demo.launch()