mudassarrafique's picture
Update app.py
6b279ed verified
import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import datetime
# Load dataset function
def load_data():
# Ensure this path is correct for your environment
df = pd.read_excel("grid_load_data.xlsx")
return df
# Calculate grid load based on time and dataset
def calculate_grid_load(df, current_time):
current_hour = current_time.hour
hourly_load = df[df["Time"].dt.hour == current_hour]["Grid Load (kW)"].mean()
return hourly_load
# Create a gauge chart for grid load status
def create_gauge(load_value):
# Define ranges for low, normal, high
ranges = [0, 2000, 3000, 4000]
colors = ['lightgreen', 'green', 'red']
labels = ['Low', 'Normal', 'High']
# Determine color and range based on load value
if load_value < 2000:
color = colors[0]
range_label = labels[0]
elif load_value < 3000:
color = colors[1]
range_label = labels[1]
else:
color = colors[2]
range_label = labels[2]
# Create the gauge chart using plotly
fig = go.Figure(go.Indicator(
mode="gauge+number+delta",
value=load_value,
domain={'x': [0, 1], 'y': [0, 1]},
gauge={
'axis': {'range': [0, 5000]},
'bar': {'color': color},
'steps': [
{'range': [0, 2000], 'color': 'lightgreen'},
{'range': [2000, 3000], 'color': 'green'},
{'range': [3000, 5000], 'color': 'red'}
],
},
title={'text': f"Grid Load Status: {range_label}"},
delta={'reference': 2000}
))
return fig
# Function to calculate the required EVs for grid stabilization
def calculate_ev_requirements(grid_load, ev_capacity=80, ev_efficiency=0.85):
# Assuming each EV contributes its battery capacity (80kWh) and efficiency (85%).
# Formula: EVs required = Grid load / (EV capacity * efficiency)
evs_needed = grid_load / (ev_capacity * ev_efficiency)
return np.ceil(evs_needed) # Round to the nearest whole number
# Function to calculate power and energy consumption from EV during underload condition
def calculate_ev_power_consumption(ev_capacity=80, charge_rate=0.85):
# Power consumed per EV during underload (assuming 85% depth of charge)
power_consumed = ev_capacity * charge_rate
return power_consumed
# Display the grid load prediction and related EV info
def display_grid_load_prediction_and_ev_info():
st.header("Grid Load Prediction and EV Charging Info")
# Allow user to manually set grid load using slider (0 to 5000 kW)
grid_load = st.slider(
"Select Grid Load (kW):",
min_value=0,
max_value=5000,
value=2000, # Default value
step=100,
help="Drag the slider to set the desired grid load."
)
# Display the gauge chart based on selected load value
st.plotly_chart(create_gauge(grid_load), use_container_width=True)
# LED Indicators for overload/normal status
if grid_load > 3500:
st.markdown('<p style="color: red; font-size: 24px;">&#128313; Left LED: Overload! Grid is in danger.</p>', unsafe_allow_html=True)
else:
st.markdown('<p style="color: green; font-size: 24px;">&#128313; Right LED: Normal. Grid is stable.</p>', unsafe_allow_html=True)
# EV Charging Status
if grid_load < 3000:
st.markdown('<p style="color: green;">Green: Charging allowed. EVs can charge.</p>', unsafe_allow_html=True)
# Calculate and display the number of EVs that can be connected based on grid load
evs_connected = calculate_ev_requirements(grid_load)
st.write(f"Approximately {evs_connected} EVs can be connected to the grid for charging.")
else:
st.markdown('<p style="color: red;">Red: Grid overload! Disconnecting EV from grid.</p>', unsafe_allow_html=True)
# If the load exceeds 3500 kW, calculate how much energy is needed to stabilize the grid
if grid_load > 3500:
energy_required = grid_load - 3500
evs_needed = calculate_ev_requirements(energy_required)
st.write(f"To stabilize the grid, {energy_required} kWh of energy is required.")
st.write(f"Approximately {evs_needed} EVs are needed to supply this energy to the grid.")
# Add a button to connect EVs to stabilize the grid
if st.button("Connect EVs to Stabilize Grid"):
grid_load -= energy_required # Decrease grid load by the energy provided by EVs
st.write(f"Grid load reduced to {grid_load} kW. EVs are stabilizing the grid.")
# Allow the user to disconnect EVs if the grid load is too high
if grid_load > 3000:
st.button("Disconnect EV from Grid", key="disconnect_charge", help="Grid load is too high. Disconnect EV.")
else:
st.button("Allow EV to Charge", key="allow_charge", help="Grid load is normal. EVs can charge.")
# Display Main Application
def main():
st.sidebar.title("EV Charging Optimization")
# Display the Grid Load Prediction and EV Info all on the same page
display_grid_load_prediction_and_ev_info()
if __name__ == "__main__":
main()