Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,35 +3,14 @@ import pandas as pd
|
|
3 |
import plotly.express as px
|
4 |
from simple_salesforce import Salesforce
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
try:
|
15 |
-
sf = Salesforce(
|
16 |
-
username="[email protected]",
|
17 |
-
password="Vedavathi@04",
|
18 |
-
security_token="jqe4His8AcuFJucZz5NBHfGU",
|
19 |
-
domain="login"
|
20 |
-
)
|
21 |
-
return sf
|
22 |
-
except Exception as e:
|
23 |
-
st.error(f"β Failed to connect to Salesforce: {e}")
|
24 |
-
return None
|
25 |
-
|
26 |
-
sf = connect_salesforce()
|
27 |
-
|
28 |
-
# -------------------------------
|
29 |
-
# Main App Logic
|
30 |
-
# -------------------------------
|
31 |
-
if sf:
|
32 |
-
st.success("β
Connected to Salesforce")
|
33 |
-
|
34 |
-
# Query Pole data
|
35 |
query = """
|
36 |
SELECT Id,
|
37 |
Name,
|
@@ -49,92 +28,87 @@ SELECT Id,
|
|
49 |
FROM Pole__c
|
50 |
LIMIT 1000
|
51 |
"""
|
52 |
-
result = sf.query_all(query)
|
53 |
-
df = pd.DataFrame(result['records']).drop(columns='attributes')
|
54 |
-
|
55 |
-
# Clean and format data
|
56 |
-
df['Installed_Date__c'] = pd.to_datetime(df['Installed_Date__c'])
|
57 |
-
df['Last_Maintenance_Date__c'] = pd.to_datetime(df['Last_Maintenance_Date__c'])
|
58 |
-
df['Power_Generation__c'] = pd.to_numeric(df['Power_Generation__c'], errors='coerce')
|
59 |
-
|
60 |
-
# Sidebar filters
|
61 |
-
st.sidebar.header("π Filters")
|
62 |
-
status_filter = st.sidebar.multiselect("Select Pole Status", df['Status__c'].unique(), default=df['Status__c'].unique())
|
63 |
-
fault_filter = st.sidebar.multiselect("Select Fault Status", df['Fault_Status__c'].unique(), default=df['Fault_Status__c'].unique())
|
64 |
-
|
65 |
-
# Apply filters
|
66 |
-
filtered_df = df[
|
67 |
-
df['Status__c'].isin(status_filter) &
|
68 |
-
df['Fault_Status__c'].isin(fault_filter)
|
69 |
-
]
|
70 |
-
|
71 |
-
st.title("β‘ Vedavathi Smart Pole Dashboard")
|
72 |
-
|
73 |
-
# -------------------------------
|
74 |
-
# π Data Table
|
75 |
-
# -------------------------------
|
76 |
-
st.subheader("π Pole Data Table")
|
77 |
-
st.dataframe(filtered_df)
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
)
|
136 |
-
st.plotly_chart(
|
137 |
-
|
138 |
else:
|
139 |
-
st.
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import plotly.express as px
|
4 |
from simple_salesforce import Salesforce
|
5 |
|
6 |
+
# π Connect to Salesforce
|
7 |
+
sf = Salesforce(
|
8 |
+
username="[email protected]",
|
9 |
+
password="Vedavathi@04",
|
10 |
+
security_token="jqe4His8AcuFJucZz5NBHfGU"
|
11 |
+
)
|
12 |
+
|
13 |
+
# π Query to get Pole records
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
query = """
|
15 |
SELECT Id,
|
16 |
Name,
|
|
|
28 |
FROM Pole__c
|
29 |
LIMIT 1000
|
30 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# π¦ Fetch and flatten records
|
33 |
+
result = sf.query_all(query)
|
34 |
+
df = pd.json_normalize(result['records'])
|
35 |
+
|
36 |
+
# π§Ό Remove 'attributes' column if present
|
37 |
+
df = df.drop(columns='attributes', errors='ignore')
|
38 |
+
|
39 |
+
# π₯οΈ Streamlit App Layout
|
40 |
+
st.set_page_config(page_title="VIEP Dashboard", layout="wide")
|
41 |
+
st.title("π Vedavathi Intelligent Energy Poles Dashboard")
|
42 |
+
|
43 |
+
# π Show Full Table
|
44 |
+
st.subheader("π Pole Data Table")
|
45 |
+
st.dataframe(df)
|
46 |
+
|
47 |
+
# π Bar Chart: Solar vs Wind Generation
|
48 |
+
st.subheader("π Solar vs Wind Generation")
|
49 |
+
fig_bar = px.bar(
|
50 |
+
df,
|
51 |
+
x="Name",
|
52 |
+
y=["Solar_Generation__c", "Wind_Generation__c"],
|
53 |
+
barmode="group",
|
54 |
+
labels={"value": "Power (kW)", "Name": "Pole"},
|
55 |
+
title="Power Generation per Pole"
|
56 |
+
)
|
57 |
+
st.plotly_chart(fig_bar, use_container_width=True)
|
58 |
+
|
59 |
+
# π₯ Pie Chart: Camera Status
|
60 |
+
st.subheader("π₯ Camera Status Distribution")
|
61 |
+
fig_cam = px.pie(
|
62 |
+
df,
|
63 |
+
names="Camera_Status__c",
|
64 |
+
title="Camera Status",
|
65 |
+
hole=0.3
|
66 |
+
)
|
67 |
+
st.plotly_chart(fig_cam, use_container_width=True)
|
68 |
+
|
69 |
+
# π¨ Pie Chart: Alert Level
|
70 |
+
st.subheader("π¨ Alert Level Distribution")
|
71 |
+
fig_alert = px.pie(
|
72 |
+
df,
|
73 |
+
names="Alert_Level__c",
|
74 |
+
title="Alert Level",
|
75 |
+
hole=0.3
|
76 |
+
)
|
77 |
+
st.plotly_chart(fig_alert, use_container_width=True)
|
78 |
+
|
79 |
+
# π Health Score by Site
|
80 |
+
st.subheader("π Health Score by Site")
|
81 |
+
fig_health = px.box(
|
82 |
+
df,
|
83 |
+
x="Site__c",
|
84 |
+
y="Health_Score__c",
|
85 |
+
points="all",
|
86 |
+
title="Health Score Distribution",
|
87 |
+
labels={"Site__c": "Site", "Health_Score__c": "Health Score"}
|
88 |
+
)
|
89 |
+
st.plotly_chart(fig_health, use_container_width=True)
|
90 |
+
|
91 |
+
# πΊοΈ Map of Pole Locations
|
92 |
+
st.subheader("πΊοΈ Pole Locations Map")
|
93 |
+
df_map = df.dropna(subset=["Location_Latitude__c", "Location_Longitude__c"])
|
94 |
+
if not df_map.empty:
|
95 |
+
fig_map = px.scatter_mapbox(
|
96 |
+
df_map,
|
97 |
+
lat="Location_Latitude__c",
|
98 |
+
lon="Location_Longitude__c",
|
99 |
+
hover_name="Name",
|
100 |
+
color="Alert_Level__c",
|
101 |
+
zoom=4,
|
102 |
+
mapbox_style="open-street-map"
|
103 |
)
|
104 |
+
st.plotly_chart(fig_map, use_container_width=True)
|
|
|
105 |
else:
|
106 |
+
st.warning("No location data available to show the map.")
|
107 |
+
|
108 |
+
# π₯ Download CSV
|
109 |
+
st.download_button(
|
110 |
+
label="β¬οΈ Download Data as CSV",
|
111 |
+
data=df.to_csv(index=False),
|
112 |
+
file_name="pole_data.csv",
|
113 |
+
mime="text/csv"
|
114 |
+
)
|