Diego Marroquin commited on
Commit
25fa3a9
·
1 Parent(s): 476b297

Added Graphs and tables

Browse files
Files changed (2) hide show
  1. app.py +95 -11
  2. requirements.txt +2 -1
app.py CHANGED
@@ -12,6 +12,9 @@ import json
12
  from calendar import monthrange
13
  import pymongo
14
  from mongoengine import StringField, ListField, DateTimeField, DictField
 
 
 
15
 
16
  def mongo_unavs_call(user_input_start_date, user_input_end_date, user_input_past_date):
17
  print("Starting mongo_unavs_call")
@@ -541,19 +544,13 @@ def run_app():
541
  st.write("Data received from Flask:")
542
  df_nucmonitor = get_nucmonitor_data(start_date, end_date, current_date)
543
  df_photo_date = get_nucmonitor_data(start_date, end_date, past_date)
544
-
 
545
  st.write("Nucmonitor")
546
  st.write(df_nucmonitor) # Display DataFrame
547
 
548
  st.write("Photo Date")
549
  st.write(df_photo_date)
550
-
551
- # # Create a line chart using Streamlit
552
- # st.title("Power Plant Data Visualization")
553
- # df1 = df.iloc[:-1, :-1]
554
- # # Create a line chart using Streamlit
555
- # st.line_chart(df1)
556
- # st.write(df1)
557
 
558
  # Get info on current forecast Nucmonitor
559
  st.title("Total Energy per Day at Current Forecast")
@@ -563,9 +560,9 @@ def run_app():
563
  # Get the last column
564
  df_nucmonitor_2 = df_nucmonitor_2.iloc[:, -1]
565
 
566
- st.write(df_nucmonitor_2)
567
 
568
- st.line_chart(df_nucmonitor_2)
569
 
570
  # Get info on past date forecast Nucmonitor
571
  st.title("Total Energy per Day at Past Date Forecast")
@@ -575,12 +572,98 @@ def run_app():
575
  # Get the last column
576
  df_photo_date_2 = df_photo_date_2.iloc[:, -1]
577
 
 
 
578
  st.write(df_photo_date_2)
579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580
  st.line_chart(df_photo_date_2)
581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582
  # Add a download button
583
  # Create a BytesIO object to hold the Excel data
 
584
  excel_buffer = io.BytesIO()
585
 
586
 
@@ -594,7 +677,7 @@ def run_app():
594
 
595
 
596
  # Save the DataFrame to the BytesIO object as an Excel file
597
- df_nucmonitor.to_excel(excel_buffer, index=True)
598
  # Set the cursor position to the beginning of the BytesIO object
599
  excel_buffer.seek(0)
600
 
@@ -606,5 +689,6 @@ def run_app():
606
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
607
  )
608
 
 
609
  if __name__ == '__main__':
610
  run_app()
 
12
  from calendar import monthrange
13
  import pymongo
14
  from mongoengine import StringField, ListField, DateTimeField, DictField
15
+ import matplotlib.pyplot as plt
16
+ from matplotlib.dates import MonthLocator
17
+
18
 
19
  def mongo_unavs_call(user_input_start_date, user_input_end_date, user_input_past_date):
20
  print("Starting mongo_unavs_call")
 
544
  st.write("Data received from Flask:")
545
  df_nucmonitor = get_nucmonitor_data(start_date, end_date, current_date)
546
  df_photo_date = get_nucmonitor_data(start_date, end_date, past_date)
547
+ current_date_str = str(current_date.strftime('%Y-%m-%d'))
548
+ past_date_str = str(past_date.strftime('%Y-%m-%d'))
549
  st.write("Nucmonitor")
550
  st.write(df_nucmonitor) # Display DataFrame
551
 
552
  st.write("Photo Date")
553
  st.write(df_photo_date)
 
 
 
 
 
 
 
554
 
555
  # Get info on current forecast Nucmonitor
556
  st.title("Total Energy per Day at Current Forecast")
 
560
  # Get the last column
561
  df_nucmonitor_2 = df_nucmonitor_2.iloc[:, -1]
562
 
563
+ print(df_nucmonitor_2)
564
 
565
+ st.write(df_nucmonitor_2)
566
 
567
  # Get info on past date forecast Nucmonitor
568
  st.title("Total Energy per Day at Past Date Forecast")
 
572
  # Get the last column
573
  df_photo_date_2 = df_photo_date_2.iloc[:, -1]
574
 
575
+ print(df_photo_date_2)
576
+
577
  st.write(df_photo_date_2)
578
 
579
+ # Create a Table that displays the forecast of each dataframe total for two months before date and two months after
580
+ # Create a Table that displays the forecast of each dataframe for the Winter months (Nov, Dec, Jan, Feb, Mar)
581
+
582
+ # Filter dates for two months before and after the current date
583
+ # Define date ranges
584
+ two_months_before = (current_date - pd.DateOffset(months=2)).strftime('%Y-%m-%d')
585
+ one_month_before = (current_date - pd.DateOffset(months=1)).strftime('%Y-%m-%d')
586
+ one_month_after = (current_date + pd.DateOffset(months=1)).strftime('%Y-%m-%d')
587
+ two_months_after = (current_date + pd.DateOffset(months=2)).strftime('%Y-%m-%d')
588
+
589
+ # Filter DataFrames based on date ranges
590
+ df_nucmonitor_filtered = df_nucmonitor_2[
591
+ (df_nucmonitor_2.index == two_months_before) |
592
+ (df_nucmonitor_2.index == one_month_before) |
593
+ (df_nucmonitor_2.index == current_date_str) |
594
+ (df_nucmonitor_2.index == one_month_after) |
595
+ (df_nucmonitor_2.index == two_months_after)
596
+ ]
597
+
598
+ df_photo_date_filtered = df_photo_date_2[
599
+ (df_photo_date_2.index == two_months_before) |
600
+ (df_photo_date_2.index == one_month_before) |
601
+ (df_photo_date_2.index == current_date_str) |
602
+ (df_photo_date_2.index == one_month_after) |
603
+ (df_photo_date_2.index == two_months_after)
604
+ ]
605
+
606
+ # Display the filtered DataFrames
607
+ st.write(f"Forecast update {current_date_str}")
608
+ st.write(df_nucmonitor_filtered)
609
+ st.write(f"Forecast update {past_date_str}")
610
+ st.write(df_photo_date_filtered)
611
+ current_forecast_update = df_nucmonitor_filtered.tolist()
612
+ past_forecast_update = df_photo_date_filtered.tolist()
613
+ delta = [current - past for current, past in zip(current_forecast_update, past_forecast_update)]
614
+
615
+ # Create a DataFrame for display
616
+ data = {
617
+ 'Dates': [two_months_before, one_month_before, current_date_str, one_month_after, two_months_after],
618
+ f"Forecast update {current_date_str}": current_forecast_update,
619
+ f"Forecast update {past_date_str}": past_forecast_update,
620
+ 'Delta': delta
621
+ }
622
+
623
+ df_display = pd.DataFrame(data)
624
+
625
+ # Display the DataFrame as a horizontal table
626
+ st.write("Table 1. Average expected availability on the French nuclear fleet (MW) - M-1, M, M+1, M+2, M+3")
627
+ st.table(df_display)
628
+
629
+
630
+ # Line charts of the forecasts (need to combine them so they appear in the same chart)
631
+ st.write("Current forecast")
632
+ st.line_chart(df_nucmonitor_2)
633
+
634
+ st.write("Previous forecast")
635
  st.line_chart(df_photo_date_2)
636
 
637
+ # Combine dataframes
638
+ combined_df = pd.concat([df_nucmonitor_2, df_photo_date_2], axis=1)
639
+ combined_df.columns = [f'Forecast {current_date_str}', f'Forecast {past_date_str}']
640
+
641
+ print(combined_df)
642
+ st.write(f"Graph 1. {start_date} to {end_date}")
643
+ st.line_chart(combined_df)
644
+
645
+ # Set Nucmonitor as a dotted line until the current date
646
+
647
+ fig, ax = plt.subplots(figsize=(10, 6))
648
+
649
+ plt.plot(combined_df.index, combined_df[f'Forecast {current_date_str}'], 'r--', label=f'Forecast {current_date_str}')
650
+ plt.plot(combined_df.index, combined_df[f'Forecast {past_date_str}'], 'b-', label=f'Forecast {past_date_str}')
651
+
652
+ plt.axvline(current_date_str, color='k', linestyle='--', linewidth=1, label='Current Date')
653
+
654
+ # Set the x-axis to show only the first day of every month
655
+ ax.xaxis.set_major_locator(MonthLocator(bymonthday=1))
656
+
657
+ plt.legend()
658
+
659
+ plt.xticks(rotation=45)
660
+
661
+ st.pyplot(fig)
662
+
663
+
664
  # Add a download button
665
  # Create a BytesIO object to hold the Excel data
666
+
667
  excel_buffer = io.BytesIO()
668
 
669
 
 
677
 
678
 
679
  # Save the DataFrame to the BytesIO object as an Excel file
680
+ df_nucmonitor_2.to_excel(excel_buffer, index=True)
681
  # Set the cursor position to the beginning of the BytesIO object
682
  excel_buffer.seek(0)
683
 
 
689
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
690
  )
691
 
692
+
693
  if __name__ == '__main__':
694
  run_app()
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  mongoengine==0.26.0
2
  pymongo==4.3.3
3
- openpyxl
 
 
1
  mongoengine==0.26.0
2
  pymongo==4.3.3
3
+ openpyxl
4
+ matplotlib