rehanafzal commited on
Commit
c238d7d
·
verified ·
1 Parent(s): 2724a3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -78
app.py CHANGED
@@ -1,107 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
- import plotly.express as px
4
- from app_backend import fetch_weather, generate_synthetic_data, optimize_load
5
 
6
  # Constants
7
  API_KEY = "84e26811a314599e940f343b4d5894a7"
8
- LOCATION = "Pakistan"
9
 
10
- # Sidebar
11
  st.sidebar.title("Smart Grid Dashboard")
12
- location = st.sidebar.text_input("Enter Location", LOCATION)
13
-
14
- # Fetch and display weather data
15
  weather = fetch_weather(API_KEY, location)
 
16
  if weather:
17
  st.sidebar.write(f"Temperature: {weather['temperature']} °C")
18
  st.sidebar.write(f"Wind Speed: {weather['wind_speed']} m/s")
19
  st.sidebar.write(f"Weather: {weather['weather']}")
20
 
21
- # Main dashboard with tabs
22
- tabs = st.tabs(["Home", "Electricity Storage", "Electricity Trading"])
23
 
24
- with tabs[0]:
25
- st.title("Real-Time Smart Grid Dashboard")
26
 
27
- # Generate synthetic data
 
 
 
 
28
  data = generate_synthetic_data()
29
 
30
- # Plot total consumption, grid generation, and storage usage
31
- fig = px.line(data, x="timestamp", y=["total_consumption_kwh", "grid_generation_kwh", "storage_usage_kwh"],
32
- title="Energy Consumption, Generation, and Storage Usage Over Time",
33
- labels={"value": "Energy (kWh)", "variable": "Energy Source"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  st.plotly_chart(fig)
35
 
36
- # Grid health overview
37
- st.subheader("Grid Health Overview")
38
- grid_health_counts = data["grid_health"].value_counts()
39
- st.bar_chart(grid_health_counts)
40
-
41
- # Optimization recommendations
42
- current_demand = data["total_consumption_kwh"].iloc[-1]
43
- current_solar = data["solar_output_kw"].iloc[-1]
44
- current_wind = data["wind_output_kw"].iloc[-1]
45
- recommendation = optimize_load(current_demand, current_solar, current_wind)
46
-
47
- st.subheader("Recommendations")
48
- st.write(f"Current Load Demand: {current_demand} kWh")
49
- st.write(f"Solar Output: {current_solar} kW")
50
- st.write(f"Wind Output: {current_wind} kW")
51
- st.write(f"Recommendation: {recommendation}")
52
-
53
  with tabs[1]:
54
- st.title("Energy Storage Overview")
55
-
56
- # Total energy stored
57
- total_storage = 500 # Example of total energy storage
58
- st.subheader(f"Total Energy Stored: {total_storage} kWh")
59
-
60
- # Energy storage contribution from different sources
61
- st.subheader("Energy Storage Contributions")
62
- energy_sources = pd.DataFrame({
63
- "Source": ["Wind", "Solar", "Turbine"],
64
- "Energy (kW/min)": [5, 7, 10]
65
- })
66
- st.bar_chart(energy_sources.set_index("Source"))
67
-
68
- # Show energy storage status with a rounded circle
69
- st.subheader("Energy Storage Circle")
70
- st.markdown("Energy storage is a combination of contributions from different renewable sources.")
71
-
72
- # Visualization of energy storage circle using Plotly
73
- storage_data = {
74
- "Source": ["Wind", "Solar", "Turbine"],
75
- "Energy": [5, 7, 10],
76
- }
77
- storage_df = pd.DataFrame(storage_data)
78
- fig = px.pie(storage_df, names="Source", values="Energy", title="Energy Storage Sources")
79
  st.plotly_chart(fig)
80
 
 
81
  with tabs[2]:
82
- st.title("Energy Trading Overview")
83
-
84
- # Energy cubes
85
- st.subheader("Energy Cubes Stored")
86
- energy_cubes = pd.DataFrame({
87
- "Country": ["China", "Sri Lanka", "Bangladesh"],
88
- "Energy (kWh)": [100, 200, 300],
89
- "Shareable": [True, True, False]
90
- })
91
-
92
- # Displaying the energy cubes in a grid
93
- st.write("Stored energy can be shared with other countries.")
94
- st.dataframe(energy_cubes)
95
-
96
- # Visualization of energy that can be shared
97
- st.subheader("Energy Trading Visualization")
98
- st.markdown("The following energy amounts are available for sharing with different countries.")
99
- trading_fig = px.bar(energy_cubes, x="Country", y="Energy (kWh)", color="Shareable", title="Energy Trading")
100
- st.plotly_chart(trading_fig)
101
-
102
 
 
 
 
 
 
 
 
103
 
 
 
104
 
 
 
 
 
 
 
105
 
106
 
107
 
 
1
+ # import streamlit as st
2
+ # import pandas as pd
3
+ # import plotly.express as px
4
+ # from app_backend import fetch_weather, generate_synthetic_data, optimize_load
5
+
6
+ # # Constants
7
+ # API_KEY = "84e26811a314599e940f343b4d5894a7"
8
+ # LOCATION = "Pakistan"
9
+
10
+ # # Sidebar
11
+ # st.sidebar.title("Smart Grid Dashboard")
12
+ # location = st.sidebar.text_input("Enter Location", LOCATION)
13
+
14
+ # # Fetch and display weather data
15
+ # weather = fetch_weather(API_KEY, location)
16
+ # if weather:
17
+ # st.sidebar.write(f"Temperature: {weather['temperature']} °C")
18
+ # st.sidebar.write(f"Wind Speed: {weather['wind_speed']} m/s")
19
+ # st.sidebar.write(f"Weather: {weather['weather']}")
20
+
21
+ # # Main dashboard with tabs
22
+ # tabs = st.tabs(["Home", "Electricity Storage", "Electricity Trading"])
23
+
24
+ # with tabs[0]:
25
+ # st.title("Real-Time Smart Grid Dashboard")
26
+
27
+ # # Generate synthetic data
28
+ # data = generate_synthetic_data()
29
+
30
+ # # Plot total consumption, grid generation, and storage usage
31
+ # fig = px.line(data, x="timestamp", y=["total_consumption_kwh", "grid_generation_kwh", "storage_usage_kwh"],
32
+ # title="Energy Consumption, Generation, and Storage Usage Over Time",
33
+ # labels={"value": "Energy (kWh)", "variable": "Energy Source"})
34
+ # st.plotly_chart(fig)
35
+
36
+ # # Grid health overview
37
+ # st.subheader("Grid Health Overview")
38
+ # grid_health_counts = data["grid_health"].value_counts()
39
+ # st.bar_chart(grid_health_counts)
40
+
41
+ # # Optimization recommendations
42
+ # current_demand = data["total_consumption_kwh"].iloc[-1]
43
+ # current_solar = data["solar_output_kw"].iloc[-1]
44
+ # current_wind = data["wind_output_kw"].iloc[-1]
45
+ # recommendation = optimize_load(current_demand, current_solar, current_wind)
46
+
47
+ # st.subheader("Recommendations")
48
+ # st.write(f"Current Load Demand: {current_demand} kWh")
49
+ # st.write(f"Solar Output: {current_solar} kW")
50
+ # st.write(f"Wind Output: {current_wind} kW")
51
+ # st.write(f"Recommendation: {recommendation}")
52
+
53
+ # with tabs[1]:
54
+ # st.title("Energy Storage Overview")
55
+
56
+ # # Total energy stored
57
+ # total_storage = 500 # Example of total energy storage
58
+ # st.subheader(f"Total Energy Stored: {total_storage} kWh")
59
+
60
+ # # Energy storage contribution from different sources
61
+ # st.subheader("Energy Storage Contributions")
62
+ # energy_sources = pd.DataFrame({
63
+ # "Source": ["Wind", "Solar", "Turbine"],
64
+ # "Energy (kW/min)": [5, 7, 10]
65
+ # })
66
+ # st.bar_chart(energy_sources.set_index("Source"))
67
+
68
+ # # Show energy storage status with a rounded circle
69
+ # st.subheader("Energy Storage Circle")
70
+ # st.markdown("Energy storage is a combination of contributions from different renewable sources.")
71
+
72
+ # # Visualization of energy storage circle using Plotly
73
+ # storage_data = {
74
+ # "Source": ["Wind", "Solar", "Turbine"],
75
+ # "Energy": [5, 7, 10],
76
+ # }
77
+ # storage_df = pd.DataFrame(storage_data)
78
+ # fig = px.pie(storage_df, names="Source", values="Energy", title="Energy Storage Sources")
79
+ # st.plotly_chart(fig)
80
+
81
+ # with tabs[2]:
82
+ # st.title("Energy Trading Overview")
83
+
84
+ # # Energy cubes
85
+ # st.subheader("Energy Cubes Stored")
86
+ # energy_cubes = pd.DataFrame({
87
+ # "Country": ["China", "Sri Lanka", "Bangladesh"],
88
+ # "Energy (kWh)": [100, 200, 300],
89
+ # "Shareable": [True, True, False]
90
+ # })
91
+
92
+ # # Displaying the energy cubes in a grid
93
+ # st.write("Stored energy can be shared with other countries.")
94
+ # st.dataframe(energy_cubes)
95
+
96
+ # # Visualization of energy that can be shared
97
+ # st.subheader("Energy Trading Visualization")
98
+ # st.markdown("The following energy amounts are available for sharing with different countries.")
99
+ # trading_fig = px.bar(energy_cubes, x="Country", y="Energy (kWh)", color="Shareable", title="Energy Trading")
100
+ # st.plotly_chart(trading_fig)
101
+
102
+
103
+
104
  import streamlit as st
105
  import pandas as pd
106
+ import plotly.graph_objects as go
107
+ from app_backend import fetch_weather, generate_synthetic_data, generate_storage_data
108
 
109
  # Constants
110
  API_KEY = "84e26811a314599e940f343b4d5894a7"
111
+ DEFAULT_LOCATION = "Pakistan"
112
 
113
+ # Sidebar for location and weather data
114
  st.sidebar.title("Smart Grid Dashboard")
115
+ location = st.sidebar.text_input("Enter Location", DEFAULT_LOCATION)
 
 
116
  weather = fetch_weather(API_KEY, location)
117
+
118
  if weather:
119
  st.sidebar.write(f"Temperature: {weather['temperature']} °C")
120
  st.sidebar.write(f"Wind Speed: {weather['wind_speed']} m/s")
121
  st.sidebar.write(f"Weather: {weather['weather']}")
122
 
123
+ # Main interface
124
+ st.title("Real-Time Smart Grid Dashboard")
125
 
126
+ # Tabs
127
+ tabs = st.tabs(["Home", "Storage", "Electricity Trade Management"])
128
 
129
+ # Home Tab
130
+ with tabs[0]:
131
+ st.header("Overview: Power and Energy Usage")
132
+
133
+ # Fetch synthetic data
134
  data = generate_synthetic_data()
135
 
136
+ # Line Graph for Power Consumption, Generation, and Storage
137
+ fig = go.Figure()
138
+ fig.add_trace(go.Scatter(
139
+ x=data["timestamp"],
140
+ y=data["total_power_consumption_mw"],
141
+ mode='lines',
142
+ name="Total Power Consumption (MW)",
143
+ line=dict(color="red")
144
+ ))
145
+ fig.add_trace(go.Scatter(
146
+ x=data["timestamp"],
147
+ y=data["grid_generation_mw"],
148
+ mode='lines',
149
+ name="Grid Generation (MW)",
150
+ line=dict(color="green")
151
+ ))
152
+ fig.add_trace(go.Scatter(
153
+ x=data["timestamp"],
154
+ y=data["storage_utilization_mw"],
155
+ mode='lines',
156
+ name="Storage Utilization (MW)",
157
+ line=dict(color="blue")
158
+ ))
159
+ fig.update_layout(title="Power and Energy Trends", xaxis_title="Time", yaxis_title="Power (MW)")
160
  st.plotly_chart(fig)
161
 
162
+ # Storage Tab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  with tabs[1]:
164
+ st.header("Energy Storage Overview")
165
+ storage_data = generate_storage_data()
166
+
167
+ st.write(f"**Total Energy Stored:** {storage_data['total_stored_kwh']} kWh")
168
+
169
+ # Circular storage breakdown
170
+ sources = ["Wind", "Solar", "Turbine"]
171
+ values = [storage_data["wind"], storage_data["solar"], storage_data["turbine"]]
172
+
173
+ fig = go.Figure(data=[go.Pie(labels=sources, values=values, hole=.4)])
174
+ fig.update_layout(title="Energy Storage Breakdown")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  st.plotly_chart(fig)
176
 
177
+ # Electricity Trade Management Tab
178
  with tabs[2]:
179
+ st.header("Electricity Trade Management")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
+ # Sample trade data
182
+ trade_data = {
183
+ "Country": ["Country A", "Country B", "Country C"],
184
+ "Energy Exported (MW)": [50, 30, 70],
185
+ "Energy Imported (MW)": [20, 40, 10],
186
+ }
187
+ trade_df = pd.DataFrame(trade_data)
188
 
189
+ st.subheader("Trade Details")
190
+ st.write(trade_df)
191
 
192
+ # Visualization
193
+ fig = go.Figure()
194
+ fig.add_trace(go.Bar(x=trade_df["Country"], y=trade_df["Energy Exported (MW)"], name="Exported", marker_color='purple'))
195
+ fig.add_trace(go.Bar(x=trade_df["Country"], y=trade_df["Energy Imported (MW)"], name="Imported", marker_color='orange'))
196
+ fig.update_layout(title="Energy Trade", barmode='group')
197
+ st.plotly_chart(fig)
198
 
199
 
200