rehanafzal commited on
Commit
305f877
·
verified ·
1 Parent(s): e849fe2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +189 -89
app.py CHANGED
@@ -352,14 +352,138 @@
352
 
353
 
354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
  import streamlit as st
 
356
  import plotly.express as px
357
  from app_backend import fetch_weather, generate_synthetic_data, optimize_load
358
- import pandas as pd
359
 
360
  # Constants
361
- API_KEY = "84e26811a314599e940f343b4d5894a7" # Replace with your OpenWeather API key
362
- LOCATION = "Pakistan"
363
 
364
  # Sidebar
365
  st.sidebar.title("Smart Grid Dashboard")
@@ -367,108 +491,84 @@ location = st.sidebar.text_input("Enter Location", LOCATION)
367
 
368
  # Fetch and display weather data
369
  weather = fetch_weather(API_KEY, location)
370
- st.sidebar.subheader("Weather Information")
371
  if weather:
372
- st.sidebar.write(f"**Temperature**: {weather['temperature']} °C")
373
- st.sidebar.write(f"**Wind Speed**: {weather['wind_speed']} m/s")
374
- st.sidebar.write(f"**Weather**: {weather['weather']}")
375
 
376
- # Main tabs
377
- tab1, tab2, tab3 = st.tabs(["Home", "Power Storage", "Power Trading management"])
378
 
379
  # Home Tab
380
- with tab1:
381
  st.title("Real-Time Smart Grid Dashboard")
382
-
383
  # Generate synthetic data
384
  data = generate_synthetic_data()
385
-
386
- # Show weather at top
387
- if weather:
388
- st.write(f"### Location: {location}")
389
- st.write(f"Temperature: {weather['temperature']} °C | Wind Speed: {weather['wind_speed']} m/s | Weather: {weather['weather']}")
390
-
391
- # Power consumption graph
392
- fig = px.line(
393
- data,
394
- x="timestamp",
395
- y=["power_consumption_mw", "generation_mw", "storage_usage_mw"],
396
- labels={"value": "Power (MW)", "variable": "Metric"},
397
- title="Power Flow Over Time"
398
- )
399
- fig.update_traces(mode="lines+markers")
400
- st.plotly_chart(fig)
401
-
402
- # Grid health as bar chart
403
  st.subheader("Grid Health Overview")
404
  grid_health_counts = data["grid_health"].value_counts()
405
- fig_health = px.bar(
406
- grid_health_counts,
407
- x=grid_health_counts.index,
408
- y=grid_health_counts.values,
409
- labels={"x": "Grid Status", "y": "Count"},
410
- title="Grid Health Status"
411
- )
412
- st.plotly_chart(fig_health)
413
-
414
- # AI recommendations
415
- st.subheader("AI Recommendations")
416
- current_demand = data["power_consumption_mw"].iloc[-1]
417
- current_solar = data["solar_output_mw"].iloc[-1]
418
- current_wind = data["wind_output_mw"].iloc[-1]
419
  recommendation = optimize_load(current_demand, current_solar, current_wind)
420
- st.write(f"**Current Load Demand**: {current_demand} MW")
421
- st.write(f"**Solar Output**: {current_solar} MW")
422
- st.write(f"**Wind Output**: {current_wind} MW")
423
- st.write(f"**Recommendation**: {recommendation}")
 
 
424
 
425
  # Storage Tab
426
- with tab2:
427
- st.title("Energy Storage Status")
428
-
429
- # Pie chart of energy percentage contribution
430
- storage_data = {
431
- "Wind": data["wind_output_mw"].mean(),
432
- "Solar": data["solar_output_mw"].mean(),
433
- "Turbine": data["turbine_output_mw"].mean()
 
434
  }
435
- fig_pie = px.pie(
436
- names=storage_data.keys(),
437
- values=storage_data.values(),
438
- title="Energy Contribution by Resource"
439
- )
440
- st.plotly_chart(fig_pie)
441
-
442
- # Circle visualization for storage
443
  st.subheader("Total Energy Stored")
444
- total_storage = sum(storage_data.values())
445
- st.write(f"**Total Energy Stored**: {total_storage:.2f} MW")
446
-
447
- st.markdown(
448
- """
449
- <div style="display: flex; justify-content: center; align-items: center; flex-direction: column;">
450
- <div style="width: 150px; height: 150px; border-radius: 50%; background-color: #FFDD00; display: flex; justify-content: center; align-items: center; font-size: 24px; font-weight: bold; margin-bottom: 20px;">
451
- {total_storage:.2f} MW
452
- </div>
453
- <div style="display: flex; gap: 50px;">
454
- <div style="width: 100px; height: 100px; border-radius: 50%; background-color: #0073FF; display: flex; justify-content: center; align-items: center; font-size: 16px; font-weight: bold;">
455
- Wind<br>{storage_data["Wind"]:.2f} MW
456
- </div>
457
- <div style="width: 100px; height: 100px; border-radius: 50%; background-color: #FF5733; display: flex; justify-content: center; align-items: center; font-size: 16px; font-weight: bold;">
458
- Solar<br>{storage_data["Solar"]:.2f} MW
459
- </div>
460
- <div style="width: 100px; height: 100px; border-radius: 50%; background-color: #28B463; display: flex; justify-content: center; align-items: center; font-size: 16px; font-weight: bold;">
461
- Turbine<br>{storage_data["Turbine"]:.2f} MW
462
- </div>
463
- </div>
464
- </div>
465
- """,
466
- unsafe_allow_html=True
467
- )
468
 
469
  # Trading Tab
470
- with tab3:
471
  st.title("Electricity Trade Management")
472
- st.write("Under development...")
 
 
 
 
 
 
 
 
 
 
 
 
 
473
 
474
 
 
352
 
353
 
354
 
355
+ # import streamlit as st
356
+ # import plotly.express as px
357
+ # from app_backend import fetch_weather, generate_synthetic_data, optimize_load
358
+ # import pandas as pd
359
+
360
+ # # Constants
361
+ # API_KEY = "84e26811a314599e940f343b4d5894a7" # Replace with your OpenWeather API key
362
+ # LOCATION = "Pakistan"
363
+
364
+ # # Sidebar
365
+ # st.sidebar.title("Smart Grid Dashboard")
366
+ # location = st.sidebar.text_input("Enter Location", LOCATION)
367
+
368
+ # # Fetch and display weather data
369
+ # weather = fetch_weather(API_KEY, location)
370
+ # st.sidebar.subheader("Weather Information")
371
+ # if weather:
372
+ # st.sidebar.write(f"**Temperature**: {weather['temperature']} °C")
373
+ # st.sidebar.write(f"**Wind Speed**: {weather['wind_speed']} m/s")
374
+ # st.sidebar.write(f"**Weather**: {weather['weather']}")
375
+
376
+ # # Main tabs
377
+ # tab1, tab2, tab3 = st.tabs(["Home", "Power Storage", "Power Trading management"])
378
+
379
+ # # Home Tab
380
+ # with tab1:
381
+ # st.title("Real-Time Smart Grid Dashboard")
382
+
383
+ # # Generate synthetic data
384
+ # data = generate_synthetic_data()
385
+
386
+ # # Show weather at top
387
+ # if weather:
388
+ # st.write(f"### Location: {location}")
389
+ # st.write(f"Temperature: {weather['temperature']} °C | Wind Speed: {weather['wind_speed']} m/s | Weather: {weather['weather']}")
390
+
391
+ # # Power consumption graph
392
+ # fig = px.line(
393
+ # data,
394
+ # x="timestamp",
395
+ # y=["power_consumption_mw", "generation_mw", "storage_usage_mw"],
396
+ # labels={"value": "Power (MW)", "variable": "Metric"},
397
+ # title="Power Flow Over Time"
398
+ # )
399
+ # fig.update_traces(mode="lines+markers")
400
+ # st.plotly_chart(fig)
401
+
402
+ # # Grid health as bar chart
403
+ # st.subheader("Grid Health Overview")
404
+ # grid_health_counts = data["grid_health"].value_counts()
405
+ # fig_health = px.bar(
406
+ # grid_health_counts,
407
+ # x=grid_health_counts.index,
408
+ # y=grid_health_counts.values,
409
+ # labels={"x": "Grid Status", "y": "Count"},
410
+ # title="Grid Health Status"
411
+ # )
412
+ # st.plotly_chart(fig_health)
413
+
414
+ # # AI recommendations
415
+ # st.subheader("AI Recommendations")
416
+ # current_demand = data["power_consumption_mw"].iloc[-1]
417
+ # current_solar = data["solar_output_mw"].iloc[-1]
418
+ # current_wind = data["wind_output_mw"].iloc[-1]
419
+ # recommendation = optimize_load(current_demand, current_solar, current_wind)
420
+ # st.write(f"**Current Load Demand**: {current_demand} MW")
421
+ # st.write(f"**Solar Output**: {current_solar} MW")
422
+ # st.write(f"**Wind Output**: {current_wind} MW")
423
+ # st.write(f"**Recommendation**: {recommendation}")
424
+
425
+ # # Storage Tab
426
+ # with tab2:
427
+ # st.title("Energy Storage Status")
428
+
429
+ # # Pie chart of energy percentage contribution
430
+ # storage_data = {
431
+ # "Wind": data["wind_output_mw"].mean(),
432
+ # "Solar": data["solar_output_mw"].mean(),
433
+ # "Turbine": data["turbine_output_mw"].mean()
434
+ # }
435
+ # fig_pie = px.pie(
436
+ # names=storage_data.keys(),
437
+ # values=storage_data.values(),
438
+ # title="Energy Contribution by Resource"
439
+ # )
440
+ # st.plotly_chart(fig_pie)
441
+
442
+ # # Circle visualization for storage
443
+ # st.subheader("Total Energy Stored")
444
+ # total_storage = sum(storage_data.values())
445
+ # st.write(f"**Total Energy Stored**: {total_storage:.2f} MW")
446
+
447
+ # st.markdown(
448
+ # """
449
+ # <div style="display: flex; justify-content: center; align-items: center; flex-direction: column;">
450
+ # <div style="width: 150px; height: 150px; border-radius: 50%; background-color: #FFDD00; display: flex; justify-content: center; align-items: center; font-size: 24px; font-weight: bold; margin-bottom: 20px;">
451
+ # {total_storage:.2f} MW
452
+ # </div>
453
+ # <div style="display: flex; gap: 50px;">
454
+ # <div style="width: 100px; height: 100px; border-radius: 50%; background-color: #0073FF; display: flex; justify-content: center; align-items: center; font-size: 16px; font-weight: bold;">
455
+ # Wind<br>{storage_data["Wind"]:.2f} MW
456
+ # </div>
457
+ # <div style="width: 100px; height: 100px; border-radius: 50%; background-color: #FF5733; display: flex; justify-content: center; align-items: center; font-size: 16px; font-weight: bold;">
458
+ # Solar<br>{storage_data["Solar"]:.2f} MW
459
+ # </div>
460
+ # <div style="width: 100px; height: 100px; border-radius: 50%; background-color: #28B463; display: flex; justify-content: center; align-items: center; font-size: 16px; font-weight: bold;">
461
+ # Turbine<br>{storage_data["Turbine"]:.2f} MW
462
+ # </div>
463
+ # </div>
464
+ # </div>
465
+ # """,
466
+ # unsafe_allow_html=True
467
+ # )
468
+
469
+ # # Trading Tab
470
+ # with tab3:
471
+ # st.title("Electricity Trade Management")
472
+ # st.write("Under development...")
473
+
474
+
475
+
476
+ # code 5
477
+
478
+
479
  import streamlit as st
480
+ import pandas as pd
481
  import plotly.express as px
482
  from app_backend import fetch_weather, generate_synthetic_data, optimize_load
 
483
 
484
  # Constants
485
+ API_KEY = "YOUR_API_KEY"
486
+ LOCATION = "New York"
487
 
488
  # Sidebar
489
  st.sidebar.title("Smart Grid Dashboard")
 
491
 
492
  # Fetch and display weather data
493
  weather = fetch_weather(API_KEY, location)
 
494
  if weather:
495
+ st.sidebar.write(f"Temperature: {weather['temperature']} °C")
496
+ st.sidebar.write(f"Wind Speed: {weather['wind_speed']} m/s")
497
+ st.sidebar.write(f"Weather: {weather['weather']}")
498
 
499
+ # Tabs
500
+ tab_home, tab_storage, tab_trading = st.tabs(["Home", "Storage", "Electricity Trade Management"])
501
 
502
  # Home Tab
503
+ with tab_home:
504
  st.title("Real-Time Smart Grid Dashboard")
505
+
506
  # Generate synthetic data
507
  data = generate_synthetic_data()
508
+
509
+ # Grid Health
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510
  st.subheader("Grid Health Overview")
511
  grid_health_counts = data["grid_health"].value_counts()
512
+ st.bar_chart(grid_health_counts)
513
+
514
+ # Power Consumption, Generation & Storage Graph
515
+ st.subheader("Power Consumption, Generation & Storage")
516
+ fig = px.line(data, x="timestamp", y=["load_demand_kwh", "solar_output_kw", "wind_output_kw"],
517
+ title="Power Consumption, Generation & Storage", labels={"value": "Power (MW)"})
518
+ fig.update_traces(line=dict(width=2))
519
+ st.plotly_chart(fig)
520
+
521
+ # Optimization Recommendations
522
+ current_demand = data["load_demand_kwh"].iloc[-1]
523
+ current_solar = data["solar_output_kw"].iloc[-1]
524
+ current_wind = data["wind_output_kw"].iloc[-1]
 
525
  recommendation = optimize_load(current_demand, current_solar, current_wind)
526
+
527
+ st.subheader("Recommendations")
528
+ st.write(f"Current Load Demand: {current_demand} MW")
529
+ st.write(f"Solar Output: {current_solar} MW")
530
+ st.write(f"Wind Output: {current_wind} MW")
531
+ st.write(f"Recommendation: {recommendation}")
532
 
533
  # Storage Tab
534
+ with tab_storage:
535
+ st.title("Energy Storage Overview")
536
+
537
+ # Energy Contribution by Resources
538
+ st.subheader("Energy Contribution Percentage by Resources")
539
+ energy_data = {
540
+ "Wind": 5,
541
+ "Solar": 7,
542
+ "Turbine": 10
543
  }
544
+ energy_df = pd.DataFrame(list(energy_data.items()), columns=["Source", "Energy (MW)"])
545
+ fig = px.pie(energy_df, values="Energy (MW)", names="Source", title="Energy Contribution by Resources")
546
+ st.plotly_chart(fig)
547
+
548
+ # Energy Storage Merge
 
 
 
549
  st.subheader("Total Energy Stored")
550
+ st.write("Energy stored from all sources:")
551
+ energy_stored = sum(energy_data.values())
552
+ st.write(f"Total Energy Stored: {energy_stored} MW")
553
+ st.write("Energy sources merged into total energy storage:")
554
+ st.write(f"Total Energy Stored in Grid: {energy_stored} MW")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
555
 
556
  # Trading Tab
557
+ with tab_trading:
558
  st.title("Electricity Trade Management")
559
+
560
+ # Simulating Electricity Trade (Energy cubes & trading)
561
+ st.subheader("Energy Trade Overview")
562
+ energy_trade = {
563
+ "USA": 50,
564
+ "Germany": 40,
565
+ "India": 30
566
+ }
567
+ trade_df = pd.DataFrame(list(energy_trade.items()), columns=["Country", "Energy (MW)"])
568
+ fig = px.bar(trade_df, x="Country", y="Energy (MW)", title="Energy Trading Overview")
569
+ st.plotly_chart(fig)
570
+
571
+ st.write("Energy cubes available for trading:")
572
+ st.write("The system can trade energy with other countries.")
573
 
574