newheatmap / app.py
Sanjayraju30's picture
Update app.py
9d225ad verified
raw
history blame
3.01 kB
import dash
from dash import html, dcc
import pandas as pd
import plotly.graph_objects as go
import random
# Configuration
AREAS = ['Kurnool', 'Hyderabad', 'Ballari', 'Gadwal']
POLES_PER_AREA = 12
# Simulate poles
def simulate_poles():
poles = []
for area in AREAS:
for i in range(1, POLES_PER_AREA + 1):
solar = round(random.uniform(3.0, 7.5), 2)
wind = round(random.uniform(0.5, 2.0), 2)
tilt = round(random.uniform(0, 10), 2)
vibration = round(random.uniform(0, 3), 2)
camera_status = random.choice(['Online', 'Offline'])
total_power = solar + wind
# Determine alert level
if total_power < 3 or tilt > 8 or vibration > 2.8:
alert = 'Red'
elif total_power < 4 or tilt > 6 or vibration > 2.5:
alert = 'Yellow'
else:
alert = 'Green'
poles.append({
'Area': area,
'Pole': f'P{i}',
'Solar (kWh)': solar,
'Wind (kWh)': wind,
'Total Power': total_power,
'Tilt (°)': tilt,
'Vibration (g)': vibration,
'Camera': camera_status,
'Alert': alert
})
return pd.DataFrame(poles)
# Create data
df = simulate_poles()
alert_map = {'Green': 1, 'Yellow': 2, 'Red': 3}
df['AlertLevel'] = df['Alert'].map(alert_map)
# Create heatmap
def create_alert_heatmap(df):
pivot_df = df.pivot(index='Area', columns='Pole', values='AlertLevel')
color_scale = [
[0, "green"],
[0.5, "yellow"],
[1.0, "red"]
]
fig = go.Figure(data=go.Heatmap(
z=pivot_df.values,
x=pivot_df.columns,
y=pivot_df.index,
colorscale=color_scale,
zmin=1,
zmax=3,
showscale=False,
hovertemplate="Pole: %{x}<br>Area: %{y}<br>Alert Level: %{z}<extra></extra>"
))
fig.update_layout(title="Smart Pole Alert Heatmap", height=500)
return fig
# App
app = dash.Dash(__name__)
server = app.server # for Hugging Face
app.layout = html.Div([
html.H1("Vedavathi Smart Pole Monitoring", style={"textAlign": "center"}),
dcc.Graph(figure=create_alert_heatmap(df)),
html.Div([
html.H3("🚨 Red Alert Poles", style={"color": "red", "textAlign": "center"}),
html.Div([
html.Span(f"{row['Area']} - {row['Pole']}", style={
"animation": "blinker 1s linear infinite",
"color": "red",
"fontWeight": "bold",
"fontSize": "18px",
"margin": "8px",
"display": "inline-block"
})
for _, row in df[df["Alert"] == "Red"].iterrows()
], style={"textAlign": "center"})
]),
html.Style("""
@keyframes blinker {
50% { opacity: 0; }
}
""")
])
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0", port=7860)