Sirivennela commited on
Commit
62f1ae6
Β·
verified Β·
1 Parent(s): 731905a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -115
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
- # Page configuration
7
- st.set_page_config(page_title="Vedavathi Pole Dashboard", layout="wide")
8
-
9
- # -------------------------------
10
- # Connect to Salesforce
11
- # -------------------------------
12
- @st.cache_resource
13
- def connect_salesforce():
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
- # πŸ”‹ Bar Chart - Power Generation
81
- # -------------------------------
82
- st.subheader("πŸ”‹ Power Generation per Pole")
83
- fig_bar = px.bar(
84
- filtered_df,
85
- x='Name',
86
- y='Power_Generation__c',
87
- color='Status__c',
88
- title="Power Output by Pole",
89
- height=400
90
- )
91
- st.plotly_chart(fig_bar, use_container_width=True)
92
-
93
- # -------------------------------
94
- # ⚠️ Pie Chart - Fault Status
95
- # -------------------------------
96
- st.subheader("⚠️ Fault Status Distribution")
97
- pie_data = filtered_df['Fault_Status__c'].value_counts().reset_index()
98
- pie_data.columns = ['Fault Status', 'Count']
99
- fig_pie = px.pie(
100
- pie_data,
101
- names='Fault Status',
102
- values='Count',
103
- title="Fault Breakdown",
104
- height=400
105
- )
106
- st.plotly_chart(fig_pie, use_container_width=True)
107
-
108
- # -------------------------------
109
- # πŸ•’ Line Chart - Installations
110
- # -------------------------------
111
- st.subheader("πŸ•’ Installation Trend")
112
- install_trend = filtered_df.groupby(filtered_df['Installed_Date__c'].dt.to_period('M')).size()
113
- install_trend.index = install_trend.index.to_timestamp()
114
- fig_line_install = px.line(
115
- x=install_trend.index,
116
- y=install_trend.values,
117
- labels={"x": "Month", "y": "Poles Installed"},
118
- title="Installations Over Time",
119
- markers=True
120
- )
121
- st.plotly_chart(fig_line_install, use_container_width=True)
122
-
123
- # -------------------------------
124
- # πŸ› οΈ Line Chart - Maintenance
125
- # -------------------------------
126
- st.subheader("πŸ› οΈ Maintenance Trend")
127
- maint_trend = filtered_df.groupby(filtered_df['Last_Maintenance_Date__c'].dt.to_period('M')).size()
128
- maint_trend.index = maint_trend.index.to_timestamp()
129
- fig_line_maint = px.line(
130
- x=maint_trend.index,
131
- y=maint_trend.values,
132
- labels={"x": "Month", "y": "Maintenance Events"},
133
- title="Maintenance Over Time",
134
- markers=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  )
136
- st.plotly_chart(fig_line_maint, use_container_width=True)
137
-
138
  else:
139
- st.error("❌ Could not connect to Salesforce. Check your credentials.")
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
+ )