King-Afridi's picture
Update app.py
70e99a5 verified
import streamlit as st
import plotly.graph_objects as go
import time
import random
# Function to simulate the sunlight intensity
def get_sunlight_intensity():
return random.uniform(0, 1) # Random intensity between 0 and 1
# Function to create the solar panel working animation
def plot_solar_panel(intensity):
# Define Solar Panel
fig = go.Figure()
# Add the solar panel as a rectangle
fig.add_shape(
type="rect",
x0=0, y0=0,
x1=10, y1=5,
line=dict(color="black", width=3),
fillcolor="yellow" if intensity > 0.5 else "gray",
)
# Title of the plot
fig.update_layout(
title=f"Solar Panel - Efficiency: {intensity*100:.2f}%",
showlegend=False,
plot_bgcolor='white',
xaxis=dict(showgrid=False, zeroline=False),
yaxis=dict(showgrid=False, zeroline=False),
margin=dict(l=0, r=0, t=30, b=0)
)
return fig
# Streamlit UI
st.title("Real-Time Solar Panel Simulation")
# Display a message explaining the app
st.markdown("""
This app simulates the real-time operation of a solar panel.
The panel's efficiency will change based on simulated sunlight intensity.
""")
# Simulate the solar panel working in real-time
st.write("Simulating Solar Panel Efficiency...")
st.write("Refreshing every second...")
# Create a plot to display the solar panel's operation
while True:
# Get random sunlight intensity (0 to 1 scale)
sunlight_intensity = get_sunlight_intensity()
# Plot the solar panel with the current intensity
fig = plot_solar_panel(sunlight_intensity)
# Display the Plotly chart in Streamlit
st.plotly_chart(fig)
# Wait for a short period before updating
time.sleep(1)