Diego Marroquin commited on
Commit
86c9138
·
1 Parent(s): eaf8efb

Added current and past date forecast; removed photodate boolean from everywhere; improved caching

Browse files
Files changed (1) hide show
  1. app.py +66 -78
app.py CHANGED
@@ -13,7 +13,7 @@ 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_photo_date, user_input_past_date):
17
  print("Starting mongo_unavs_call")
18
  # Connect to the MongoDB database
19
  user = "dmarroquin"
@@ -201,7 +201,7 @@ def get_unavailabilities(usr_start_date, usr_end_date):
201
  # --------------------------------------------------------------------------------------- #
202
 
203
 
204
- def nuc_monitor(usr_start_date, usr_end_date, photo_date, past_date):
205
  # # Slightly changed metadata to fit the data from the RTE API: ST-LAURENT B 2 --> ST LAURENT 2, ....
206
 
207
  plants_metadata = {"BELLEVILLE 1": 1310.0, "BELLEVILLE 2": 1310.0, "BLAYAIS 1": 910.0, "BLAYAIS 2": 910.0,
@@ -221,8 +221,9 @@ def nuc_monitor(usr_start_date, usr_end_date, photo_date, past_date):
221
 
222
  # --------------------- INITIAL DATA CLEANING FOR RTE DATA ------------------------ #
223
  # unav_API = rte_data.json()
224
- rte_stuff = get_unavailabilities(usr_start_date, usr_end_date)
225
- unav_API = rte_stuff
 
226
  # print(unav_API)
227
  # Store the unavailabilities in a list
228
  unavailabilities = []
@@ -260,8 +261,10 @@ def nuc_monitor(usr_start_date, usr_end_date, photo_date, past_date):
260
  # --------------------- INITIAL DATA CLEANING FOR MONGO DATA ------------------------ #
261
 
262
  # # Create a DataFrame
263
- mongo_data = mongo_unavs_call(usr_start_date, usr_end_date, photo_date, past_date)
264
- mongo_df = pd.DataFrame(mongo_data)
 
 
265
 
266
  # Unpack the dictionaries into separate columns
267
  mongo_df_unpacked = pd.json_normalize(mongo_df['generation_unavailabilities'])
@@ -310,11 +313,13 @@ def nuc_monitor(usr_start_date, usr_end_date, photo_date, past_date):
310
  past_date_str = str(past_date)
311
  current_datetime_str = current_datetime.strftime("%Y-%m-%d")
312
 
313
- if photo_date == True:
314
- nuclear_unav = merged_df.copy()[(merged_df.copy()["production_type"] == "NUCLEAR") & (merged_df.copy()["updated_date"] <= past_date_str)]
315
- photo_date = True
316
- else: # need to add updated_date as a conditional to get the newest for that day
317
- nuclear_unav = merged_df.copy()[(merged_df.copy()["production_type"] == "NUCLEAR") & (merged_df.copy()["updated_date"] <= end_date_str)]
 
 
318
 
319
  # --------------------------- HERE IS THE CHANGE TO GET ONLY ACTIVE OR ACTIVE AND INACTIVE --------------------------- #
320
 
@@ -489,18 +494,11 @@ def nuc_monitor(usr_start_date, usr_end_date, photo_date, past_date):
489
  output_results = {plant: {str(date): power for date, power in plant_data.items()} for plant, plant_data in output_results.items()}
490
  # print(output_results)
491
  # -------------------------------------------------
492
- if photo_date == False:
493
 
494
- json_data = json.dumps(output_results)
495
- # print(json_data)
496
- return json_data
497
- else:
498
-
499
- json_data = json.dumps(output_results)
500
- # print(json_data)
501
- return json_data
502
  # -------------------------------------------------
503
- return
504
 
505
  @st.cache_data
506
  def get_rte_data(start_date, end_date):
@@ -508,13 +506,15 @@ def get_rte_data(start_date, end_date):
508
  print(rte_data)
509
  return rte_data
510
  @st.cache_data
511
- def get_mongodb_data(start_date, end_date, photo_date, past_date):
512
- database_data = mongo_unavs_call(start_date, end_date, photo_date, past_date)
513
  return database_data
514
 
515
  @st.cache_data
516
- def get_nucmonitor_data(start_date, end_date, photo_date, past_date):
517
- response_nucmonitor = nuc_monitor(start_date, end_date, photo_date, past_date)
 
 
518
  # nucmonitor_data = response_nucmonitor.json()
519
  # nucmonitor_json = json.loads(nucmonitor_data)
520
  print(response_nucmonitor)
@@ -528,66 +528,57 @@ def run_app():
528
  # Get user input (e.g., dates)
529
  start_date = st.date_input("Start Date")
530
  end_date = st.date_input("End Date")
531
- photo_date = st.checkbox("Photodate")
532
-
533
- if photo_date == True:
534
- past_date = st.date_input("Cutoff Date")
535
- else:
536
- past_date = None
537
 
538
  with st.form("nucmonitor_form"):
539
  submitted = st.form_submit_button("Get Nucmonitor")
540
 
541
  if not submitted:
542
- pass
543
 
544
  else:
545
  st.write("Data received from Flask:")
546
- df = get_nucmonitor_data(start_date, end_date, photo_date, past_date)
547
- # st.sidebar.write("FILTERS")
548
-
549
- st.write(df) # Display DataFrame
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
-
557
- # ---------------------------------------------------
558
- #######################################################
559
- #################### FILTERS ##########################
560
- #######################################################
561
- # if df is not None:
562
-
563
- # st.title("Data Filters")
564
- # df_columns_lst = df.columns.tolist()
565
- # print("df_columns_lst", df_columns_lst)
566
-
567
- # # Select columns for display
568
- # selected_columns = st.sidebar.multiselect("Select Columns to Display",
569
- # options=df_columns_lst,
570
- # default=df_columns_lst,
571
- # key=None
572
- # )
573
-
574
- # st.sidebar.write('Selected plants:', selected_columns)
575
-
576
- # # Filter rows by checking column 0
577
- # # filter_row_contains_value = st.sidebar.checkbox("Filter Rows by Date", False)
578
-
579
- # # filtered_df = df.copy()
580
- # print("filtered_df = df.copy()")
581
- # filtered_by_plant = df.copy()[selected_columns]
582
- # print("filtered_df = filtered_df[selected_columns]")
583
 
584
- # # if filter_row_contains_value:
585
- # # filtered_df = filtered_df[filtered_df.iloc[:, 0].astype(str).str.contains('0', case=False, na=False)]
 
 
 
 
 
 
586
 
587
- # st.write("Filtered Data:")
588
- # st.write(filtered_by_plant)
589
- # print("st.write(filtered_df)")
590
 
 
 
 
 
 
 
 
 
 
591
  # Add a download button
592
  # Create a BytesIO object to hold the Excel data
593
  excel_buffer = io.BytesIO()
@@ -603,7 +594,7 @@ def run_app():
603
 
604
 
605
  # Save the DataFrame to the BytesIO object as an Excel file
606
- df.to_excel(excel_buffer, index=True)
607
  # Set the cursor position to the beginning of the BytesIO object
608
  excel_buffer.seek(0)
609
 
@@ -615,8 +606,5 @@ def run_app():
615
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
616
  )
617
 
618
-
619
-
620
-
621
  if __name__ == '__main__':
622
  run_app()
 
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")
18
  # Connect to the MongoDB database
19
  user = "dmarroquin"
 
201
  # --------------------------------------------------------------------------------------- #
202
 
203
 
204
+ def nuc_monitor(usr_start_date, usr_end_date, past_date, mongo_db_data, rte_data):
205
  # # Slightly changed metadata to fit the data from the RTE API: ST-LAURENT B 2 --> ST LAURENT 2, ....
206
 
207
  plants_metadata = {"BELLEVILLE 1": 1310.0, "BELLEVILLE 2": 1310.0, "BLAYAIS 1": 910.0, "BLAYAIS 2": 910.0,
 
221
 
222
  # --------------------- INITIAL DATA CLEANING FOR RTE DATA ------------------------ #
223
  # unav_API = rte_data.json()
224
+ # rte_stuff = get_unavailabilities(usr_start_date, usr_end_date)
225
+ # rte_stuff = get_rte_data(usr_start_date, usr_end_date)
226
+ unav_API = rte_data
227
  # print(unav_API)
228
  # Store the unavailabilities in a list
229
  unavailabilities = []
 
261
  # --------------------- INITIAL DATA CLEANING FOR MONGO DATA ------------------------ #
262
 
263
  # # Create a DataFrame
264
+ # mongo_data = mongo_unavs_call(usr_start_date, usr_end_date, past_date)
265
+ # mongo_data = get_mongodb_data(usr_start_date, usr_end_date, past_date)
266
+
267
+ mongo_df = pd.DataFrame(mongo_db_data)
268
 
269
  # Unpack the dictionaries into separate columns
270
  mongo_df_unpacked = pd.json_normalize(mongo_df['generation_unavailabilities'])
 
313
  past_date_str = str(past_date)
314
  current_datetime_str = current_datetime.strftime("%Y-%m-%d")
315
 
316
+ nuclear_unav = merged_df.copy()[(merged_df.copy()["production_type"] == "NUCLEAR") & (merged_df.copy()["updated_date"] <= past_date_str)]
317
+
318
+ # if photo_date == True:
319
+ # nuclear_unav = merged_df.copy()[(merged_df.copy()["production_type"] == "NUCLEAR") & (merged_df.copy()["updated_date"] <= past_date_str)]
320
+ # photo_date = True
321
+ # else: # need to add updated_date as a conditional to get the newest for that day
322
+ # nuclear_unav = merged_df.copy()[(merged_df.copy()["production_type"] == "NUCLEAR") & (merged_df.copy()["updated_date"] <= end_date_str)]
323
 
324
  # --------------------------- HERE IS THE CHANGE TO GET ONLY ACTIVE OR ACTIVE AND INACTIVE --------------------------- #
325
 
 
494
  output_results = {plant: {str(date): power for date, power in plant_data.items()} for plant, plant_data in output_results.items()}
495
  # print(output_results)
496
  # -------------------------------------------------
 
497
 
498
+ json_data = json.dumps(output_results)
499
+ # print(json_data)
500
+ return json_data
 
 
 
 
 
501
  # -------------------------------------------------
 
502
 
503
  @st.cache_data
504
  def get_rte_data(start_date, end_date):
 
506
  print(rte_data)
507
  return rte_data
508
  @st.cache_data
509
+ def get_mongodb_data(start_date, end_date, past_date):
510
+ database_data = mongo_unavs_call(start_date, end_date, past_date)
511
  return database_data
512
 
513
  @st.cache_data
514
+ def get_nucmonitor_data(start_date, end_date, past_date):
515
+ mongo = get_mongodb_data(start_date, end_date, past_date)
516
+ rte = get_rte_data(start_date, end_date)
517
+ response_nucmonitor = nuc_monitor(start_date, end_date, past_date, mongo, rte)
518
  # nucmonitor_data = response_nucmonitor.json()
519
  # nucmonitor_json = json.loads(nucmonitor_data)
520
  print(response_nucmonitor)
 
528
  # Get user input (e.g., dates)
529
  start_date = st.date_input("Start Date")
530
  end_date = st.date_input("End Date")
531
+ past_date = st.date_input("Cutoff Date")
532
+ current_date = datetime.datetime.now()
 
 
 
 
533
 
534
  with st.form("nucmonitor_form"):
535
  submitted = st.form_submit_button("Get Nucmonitor")
536
 
537
  if not submitted:
538
+ st.write("Form not submitted")
539
 
540
  else:
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")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
 
561
+ # Remove the final row 'Total'
562
+ df_nucmonitor_2 = df_nucmonitor.iloc[:-1, :]
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")
 
572
 
573
+ # Remove the final row 'Total'
574
+ df_photo_date_2 = df_photo_date.iloc[:-1, :]
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()
 
594
 
595
 
596
  # Save the DataFrame to the BytesIO object as an Excel file
597
+ df_nucmonitor_2.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
  mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
607
  )
608
 
 
 
 
609
  if __name__ == '__main__':
610
  run_app()