rehanafzal's picture
Create app.py
5cb69b8 verified
import streamlit as st
import pandas as pd
import plotly.express as px
from app_backend import fetch_weather, generate_synthetic_data, optimize_load
# Constants
API_KEY = "84e26811a314599e940f343b4d5894a7"
LOCATION = "New York"
# Sidebar
st.sidebar.title("Smart Grid Dashboard")
location = st.sidebar.text_input("Enter Location", LOCATION)
# Fetch and display weather data
weather = fetch_weather(API_KEY, location)
if weather:
st.sidebar.write(f"Temperature: {weather['temperature']} °C")
st.sidebar.write(f"Wind Speed: {weather['wind_speed']} m/s")
st.sidebar.write(f"Weather: {weather['weather']}")
# Main dashboard
st.title("Real-Time Smart Grid Dashboard")
# Generate synthetic data
data = generate_synthetic_data()
# Plot load demand
fig = px.line(data, x="timestamp", y="load_demand_kwh", title="Load Demand Over Time")
st.plotly_chart(fig)
# Renewable energy contribution
fig = px.bar(
data,
x="timestamp",
y=["solar_output_kw", "wind_output_kw"],
title="Renewable Energy Contributions",
labels={"value": "Power (kW)", "variable": "Energy Source"}
)
st.plotly_chart(fig)
# Grid health
st.subheader("Grid Health Overview")
grid_health_counts = data["grid_health"].value_counts()
st.bar_chart(grid_health_counts)
# Optimization recommendations
current_demand = data["load_demand_kwh"].iloc[-1]
current_solar = data["solar_output_kw"].iloc[-1]
current_wind = data["wind_output_kw"].iloc[-1]
recommendation = optimize_load(current_demand, current_solar, current_wind)
st.subheader("Recommendations")
st.write(f"Current Load Demand: {current_demand} kWh")
st.write(f"Solar Output: {current_solar} kW")
st.write(f"Wind Output: {current_wind} kW")
st.write(f"Recommendation: {recommendation}")