mabuseif commited on
Commit
28192eb
·
verified ·
1 Parent(s): 74f62c1

Update app/internal_loads.py

Browse files
Files changed (1) hide show
  1. app/internal_loads.py +83 -73
app/internal_loads.py CHANGED
@@ -925,6 +925,7 @@ def display_ventilation_infiltration_tab():
925
 
926
  def display_schedules_tab():
927
  import streamlit as st
 
928
 
929
  st.subheader("Schedules Editor")
930
 
@@ -938,9 +939,11 @@ def display_schedules_tab():
938
  "original_name": ""
939
  }
940
 
941
- # Initialize schedule_editor if not present
942
  if "schedule_editor" not in st.session_state:
943
  st.session_state.schedule_editor = DEFAULT_STATE.copy()
 
 
944
 
945
  editor_state = st.session_state.schedule_editor
946
  schedules = st.session_state.project_data["internal_loads"]["schedules"]
@@ -975,12 +978,8 @@ def display_schedules_tab():
975
 
976
  # ---------------------- UI FORM for name/description and actions ----------------------
977
  with st.form("schedule_form"):
978
- name = st.text_input("Schedule Name",
979
- value=editor_state.get("name", ""),
980
- key="schedule_name_input")
981
- description = st.text_area("Description",
982
- value=editor_state.get("description", ""),
983
- key="schedule_description_input")
984
 
985
  # ---------------------- SLIDERS LAYOUT ----------------------
986
  st.markdown("### Schedule Sliders")
@@ -995,12 +994,12 @@ def display_schedules_tab():
995
  st.markdown("<div style='text-align:center; font-weight:bold; font-size:18px;'>Weekend</div>", unsafe_allow_html=True)
996
 
997
  hide_elements = """
998
- <style>
999
- div[data-testid="stSliderTickBarMin"],
1000
- div[data-testid="stSliderTickBarMax"] {
1001
- display: none;
1002
- }
1003
- </style>
1004
  """
1005
  st.markdown(hide_elements, unsafe_allow_html=True)
1006
 
@@ -1043,11 +1042,11 @@ def display_schedules_tab():
1043
  col1, col2, col3 = st.columns(3)
1044
  with col1:
1045
  submit_label = "Update Schedule" if editor_state.get("is_edit") else "Save Schedule"
1046
- submitted = st.form_submit_button(submit_label, key="save_schedule_button")
1047
  with col2:
1048
- edit_submitted = st.form_submit_button("Edit Selected Schedule", key="edit_schedule_button")
1049
  with col3:
1050
- delete_submitted = st.form_submit_button("Delete Selected Schedule", key="delete_schedule_button")
1051
 
1052
  # ---------------------- Saved Schedules Selector ----------------------
1053
  st.markdown("### Saved Schedules")
@@ -1055,73 +1054,84 @@ def display_schedules_tab():
1055
  "Select Saved Schedule",
1056
  options=list(schedules.keys()),
1057
  index=list(schedules.keys()).index(editor_state.get("name", list(schedules.keys())[0])) if editor_state.get("name") in schedules and schedules else 0,
1058
- help="Select a saved schedule to edit or delete.",
1059
- key="saved_schedule_select"
1060
  )
1061
 
1062
  # ---------------------- Save logic ----------------------
1063
  if submitted:
1064
- if not name.strip():
1065
- st.error("Schedule name is required.")
1066
- elif name in schedules and not editor_state.get("is_edit"):
1067
- st.error("A schedule with this name already exists.")
1068
- else:
1069
- schedules[name] = {
1070
- "description": description,
1071
- "weekday": weekday_values,
1072
- "weekend": weekend_values
1073
- }
1074
- if editor_state.get("is_edit") and editor_state.get("original_name") and editor_state.get("original_name") != name:
1075
- if editor_state["original_name"] in schedules:
1076
- del schedules[editor_state["original_name"]]
1077
- # Reset editor state and slider values
1078
- st.session_state.schedule_editor = DEFAULT_STATE.copy()
1079
- for hour in range(24):
1080
- st.session_state[f"weekday_slider_{hour}_value"] = 0.0
1081
- st.session_state[f"weekend_slider_{hour}_value"] = 0.0
1082
- st.success(f"Schedule '{name}' saved successfully.")
1083
- st.session_state.module_rerun_flags["internal_loads"] = True
1084
- st.rerun()
 
 
 
 
1085
 
1086
  # ---------------------- Edit logic ----------------------
1087
  if edit_submitted and saved_schedule:
1088
- if saved_schedule in DEFAULT_SCHEDULE_TEMPLATES:
1089
- st.error("Default schedules cannot be edited.")
1090
- else:
1091
- schedule_data = schedules[saved_schedule]
1092
- # Update editor state without modifying slider widgets directly
1093
- st.session_state.schedule_editor = {
1094
- "is_edit": True,
1095
- "original_name": saved_schedule,
1096
- "name": saved_schedule,
1097
- "description": schedule_data.get("description", ""),
1098
- "weekday": schedule_data.get("weekday", [0.0] * 24),
1099
- "weekend": schedule_data.get("weekend", [0.0] * 24),
1100
- "template": "None"
1101
- }
1102
- # Preload slider values
1103
- for hour in range(24):
1104
- st.session_state[f"weekday_slider_{hour}_value"] = schedule_data.get("weekday", [0.0] * 24)[hour]
1105
- st.session_state[f"weekend_slider_{hour}_value"] = schedule_data.get("weekend", [0.0] * 24)[hour]
1106
- st.session_state.module_rerun_flags["internal_loads"] = True
1107
- st.rerun()
 
 
 
 
1108
 
1109
  # ---------------------- Delete logic ----------------------
1110
  if delete_submitted and saved_schedule:
1111
- if saved_schedule in DEFAULT_SCHEDULE_TEMPLATES:
1112
- st.error("Default schedules cannot be deleted.")
1113
- elif is_schedule_in_use(saved_schedule):
1114
- st.error(f"Schedule '{saved_schedule}' is in use and cannot be deleted.")
1115
- else:
1116
- del schedules[saved_schedule]
1117
- # Reset editor state and slider values
1118
- st.session_state.schedule_editor = DEFAULT_STATE.copy()
1119
- for hour in range(24):
1120
- st.session_state[f"weekday_slider_{hour}_value"] = 0.0
1121
- st.session_state[f"weekend_slider_{hour}_value"] = 0.0
1122
- st.success(f"Schedule '{saved_schedule}' deleted!")
1123
- st.session_state.module_rerun_flags["internal_loads"] = True
1124
- st.rerun()
 
 
 
 
1125
 
1126
  def is_schedule_in_use(schedule_name: str) -> bool:
1127
  """
 
925
 
926
  def display_schedules_tab():
927
  import streamlit as st
928
+ import uuid
929
 
930
  st.subheader("Schedules Editor")
931
 
 
939
  "original_name": ""
940
  }
941
 
942
+ # Initialize schedule_editor and schedule_action if not present
943
  if "schedule_editor" not in st.session_state:
944
  st.session_state.schedule_editor = DEFAULT_STATE.copy()
945
+ if "schedule_action" not in st.session_state:
946
+ st.session_state.schedule_action = {"action": None, "id": None}
947
 
948
  editor_state = st.session_state.schedule_editor
949
  schedules = st.session_state.project_data["internal_loads"]["schedules"]
 
978
 
979
  # ---------------------- UI FORM for name/description and actions ----------------------
980
  with st.form("schedule_form"):
981
+ name = st.text_input("Schedule Name", value=editor_state.get("name", ""))
982
+ description = st.text_area("Description", value=editor_state.get("description", ""))
 
 
 
 
983
 
984
  # ---------------------- SLIDERS LAYOUT ----------------------
985
  st.markdown("### Schedule Sliders")
 
994
  st.markdown("<div style='text-align:center; font-weight:bold; font-size:18px;'>Weekend</div>", unsafe_allow_html=True)
995
 
996
  hide_elements = """
997
+ <style>
998
+ div[data-testid="stSliderTickBarMin"],
999
+ div[data-testid="stSliderTickBarMax"] {
1000
+ display: none;
1001
+ }
1002
+ </style>
1003
  """
1004
  st.markdown(hide_elements, unsafe_allow_html=True)
1005
 
 
1042
  col1, col2, col3 = st.columns(3)
1043
  with col1:
1044
  submit_label = "Update Schedule" if editor_state.get("is_edit") else "Save Schedule"
1045
+ submitted = st.form_submit_button(submit_label)
1046
  with col2:
1047
+ edit_submitted = st.form_submit_button("Edit Selected Schedule")
1048
  with col3:
1049
+ delete_submitted = st.form_submit_button("Delete Selected Schedule")
1050
 
1051
  # ---------------------- Saved Schedules Selector ----------------------
1052
  st.markdown("### Saved Schedules")
 
1054
  "Select Saved Schedule",
1055
  options=list(schedules.keys()),
1056
  index=list(schedules.keys()).index(editor_state.get("name", list(schedules.keys())[0])) if editor_state.get("name") in schedules and schedules else 0,
1057
+ help="Select a saved schedule to edit or delete."
 
1058
  )
1059
 
1060
  # ---------------------- Save logic ----------------------
1061
  if submitted:
1062
+ action_id = str(uuid.uuid4())
1063
+ if st.session_state.schedule_action.get("id") != action_id:
1064
+ st.session_state.schedule_action = {"action": "save", "id": action_id}
1065
+ if not name.strip():
1066
+ st.error("Schedule name is required.")
1067
+ elif name in schedules and not editor_state.get("is_edit"):
1068
+ st.error("A schedule with this name already exists.")
1069
+ else:
1070
+ schedules[name] = {
1071
+ "description": description,
1072
+ "weekday": weekday_values,
1073
+ "weekend": weekend_values
1074
+ }
1075
+ if editor_state.get("is_edit") and editor_state.get("original_name") and editor_state.get("original_name") != name:
1076
+ if editor_state["original_name"] in schedules:
1077
+ del schedules[editor_state["original_name"]]
1078
+ # Reset editor state and slider values
1079
+ st.session_state.schedule_editor = DEFAULT_STATE.copy()
1080
+ for hour in range(24):
1081
+ st.session_state[f"weekday_slider_{hour}_value"] = 0.0
1082
+ st.session_state[f"weekend_slider_{hour}_value"] = 0.0
1083
+ st.success(f"Schedule '{name}' saved successfully.")
1084
+ st.session_state.schedule_action = {"action": None, "id": None}
1085
+ st.session_state.module_rerun_flags["internal_loads"] = True
1086
+ st.rerun()
1087
 
1088
  # ---------------------- Edit logic ----------------------
1089
  if edit_submitted and saved_schedule:
1090
+ action_id = str(uuid.uuid4())
1091
+ if st.session_state.schedule_action.get("id") != action_id:
1092
+ st.session_state.schedule_action = {"action": "edit", "id": action_id}
1093
+ if saved_schedule in DEFAULT_SCHEDULE_TEMPLATES:
1094
+ st.error("Default schedules cannot be edited.")
1095
+ else:
1096
+ schedule_data = schedules[saved_schedule]
1097
+ # Update editor state
1098
+ st.session_state.schedule_editor = {
1099
+ "is_edit": True,
1100
+ "original_name": saved_schedule,
1101
+ "name": saved_schedule,
1102
+ "description": schedule_data.get("description", ""),
1103
+ "weekday": schedule_data.get("weekday", [0.0] * 24),
1104
+ "weekend": schedule_data.get("weekend", [0.0] * 24),
1105
+ "template": "None"
1106
+ }
1107
+ # Preload slider values
1108
+ for hour in range(24):
1109
+ st.session_state[f"weekday_slider_{hour}_value"] = schedule_data.get("weekday", [0.0] * 24)[hour]
1110
+ st.session_state[f"weekend_slider_{hour}_value"] = schedule_data.get("weekend", [0.0] * 24)[hour]
1111
+ st.session_state.schedule_action = {"action": None, "id": None}
1112
+ st.session_state.module_rerun_flags["internal_loads"] = True
1113
+ st.rerun()
1114
 
1115
  # ---------------------- Delete logic ----------------------
1116
  if delete_submitted and saved_schedule:
1117
+ action_id = str(uuid.uuid4())
1118
+ if st.session_state.schedule_action.get("id") != action_id:
1119
+ st.session_state.schedule_action = {"action": "delete", "id": action_id}
1120
+ if saved_schedule in DEFAULT_SCHEDULE_TEMPLATES:
1121
+ st.error("Default schedules cannot be deleted.")
1122
+ elif is_schedule_in_use(saved_schedule):
1123
+ st.error(f"Schedule '{saved_schedule}' is in use and cannot be deleted.")
1124
+ else:
1125
+ del schedules[saved_schedule]
1126
+ # Reset editor state and slider values
1127
+ st.session_state.schedule_editor = DEFAULT_STATE.copy()
1128
+ for hour in range(24):
1129
+ st.session_state[f"weekday_slider_{hour}_value"] = 0.0
1130
+ st.session_state[f"weekend_slider_{hour}_value"] = 0.0
1131
+ st.success(f"Schedule '{saved_schedule}' deleted!")
1132
+ st.session_state.schedule_action = {"action": None, "id": None}
1133
+ st.session_state.module_rerun_flags["internal_loads"] = True
1134
+ st.rerun()
1135
 
1136
  def is_schedule_in_use(schedule_name: str) -> bool:
1137
  """