mabuseif commited on
Commit
6112ee7
·
verified ·
1 Parent(s): 91951c7

Update app/internal_loads.py

Browse files
Files changed (1) hide show
  1. app/internal_loads.py +141 -136
app/internal_loads.py CHANGED
@@ -922,173 +922,178 @@ def display_ventilation_infiltration_tab():
922
  st.session_state.module_rerun_flags["internal_loads"] = True
923
  st.rerun()
924
 
925
- # Custom CSS for vertical sliders
926
  st.markdown("""
927
  <style>
928
  .slider-container {
929
  display: flex;
930
  flex-direction: row;
 
931
  align-items: flex-end;
932
- justify-content: center;
933
- gap: 8px;
934
- padding-top: 30px;
935
  }
936
  .slider-item {
 
 
 
 
 
 
937
  writing-mode: bt-lr;
938
  transform: rotate(-90deg);
939
- height: 150px;
940
  width: 30px;
941
  }
942
  .hour-label {
943
  text-align: center;
944
  font-size: 12px;
945
  margin-top: 5px;
 
 
 
 
 
 
 
946
  }
947
  </style>
948
  """, unsafe_allow_html=True)
949
 
950
  def display_schedules_tab():
951
- """Display the schedules tab content with responsive vertical slider interface."""
952
  st.subheader("Schedule Management")
953
 
954
- # Split the display into two columns
955
- col1, col2 = st.columns([3, 2])
 
956
 
957
- with col1:
958
- st.subheader("Saved Schedules")
959
- schedules = st.session_state.project_data["internal_loads"]["schedules"]
960
- if schedules:
961
- display_schedules_table(schedules)
962
- else:
963
- st.write("No schedules available.")
964
 
965
- with col2:
966
- st.subheader("Schedule Editor/Creator")
967
-
968
- # Initialize editor state
969
- if "schedule_editor" not in st.session_state:
970
- st.session_state.schedule_editor = {}
971
-
972
- editor_state = st.session_state.get("schedule_editor", {})
973
- is_edit = editor_state.get("is_edit", False)
974
-
975
- # Schedule name and description
976
- name = st.text_input(
977
- "Schedule Name",
978
- value=editor_state.get("name", ""),
979
- help="Enter a unique name for this schedule."
980
- )
981
-
982
- description = st.text_area(
983
- "Description",
984
- value=editor_state.get("description", ""),
985
- help="Brief description of this schedule."
986
  )
987
-
988
- # Template selection for new schedules
989
- if not is_edit:
990
- template = st.selectbox(
991
- "Start from Template",
992
- ["Custom"] + list(DEFAULT_SCHEDULE_TEMPLATES.keys()),
993
- help="Select a template to start with, or choose Custom for blank schedule."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
994
  )
995
- else:
996
- template = "Custom"
997
-
998
- # Initialize schedule values
999
- if template != "Custom" and not is_edit:
1000
- weekday_values = DEFAULT_SCHEDULE_TEMPLATES[template]["weekday"]
1001
- weekend_values = DEFAULT_SCHEDULE_TEMPLATES[template]["weekend"]
1002
- else:
1003
- weekday_values = editor_state.get("weekday", [0.0] * 24)
1004
- weekend_values = editor_state.get("weekend", [0.0] * 24)
1005
-
1006
- # Weekday schedule
1007
- st.write("**Weekday Schedule**")
1008
- st.markdown('<div class="slider-container">', unsafe_allow_html=True)
1009
- weekday_schedule = []
1010
- for hour in range(24):
1011
- with st.container():
1012
- val = st.slider(
1013
- label=f"{hour:02d}:00",
1014
- min_value=0.0,
1015
- max_value=1.0,
1016
- step=0.01,
1017
- value=float(weekday_values[hour]),
1018
- key=f"weekday_{hour}",
1019
- help=f"Hour {hour}:00 weekday value",
1020
- format="%.2f"
1021
- )
1022
- st.markdown(f'<div class="hour-label">{hour:02d}:00</div>', unsafe_allow_html=True)
1023
- weekday_schedule.append(val)
1024
- st.markdown('</div>', unsafe_allow_html=True)
1025
-
1026
- # Weekend schedule
1027
- st.write("**Weekend Schedule**")
1028
- st.markdown('<div class="slider-container">', unsafe_allow_html=True)
1029
- weekend_schedule = []
1030
- for hour in range(24):
1031
- with st.container():
1032
- val = st.slider(
1033
- label=f"{hour:02d}:00",
1034
- min_value=0.0,
1035
- max_value=1.0,
1036
- step=0.01,
1037
- value=float(weekend_values[hour]),
1038
- key=f"weekend_{hour}",
1039
- help=f"Hour {hour}:00 weekend value",
1040
- format="%.2f"
1041
- )
1042
- st.markdown(f'<div class="hour-label">{hour:02d}:00</div>', unsafe_allow_html=True)
1043
- weekend_schedule.append(val)
1044
- st.markdown('</div>', unsafe_allow_html=True)
1045
-
1046
- # Preview chart
1047
- st.write("**Schedule Preview**")
1048
- display_schedule_chart(f"Preview: {name or 'New Schedule'}", {
1049
- "weekday": weekday_schedule,
1050
- "weekend": weekend_schedule
1051
- })
1052
-
1053
- # Action buttons
1054
- col1, col2 = st.columns(2)
1055
- with col1:
1056
- submit_label = "Update Schedule" if is_edit else "Add Schedule"
1057
- if st.button(submit_label):
1058
- # Validate inputs
1059
- if not name.strip():
1060
- st.error("Schedule name is required.")
1061
- elif name in schedules and not is_edit:
1062
- st.error("A schedule with this name already exists.")
1063
  else:
1064
- # Create schedule data
1065
- schedule_data = {
1066
- "description": description,
1067
- "weekday": weekday_schedule,
1068
- "weekend": weekend_schedule
1069
- }
1070
-
1071
- # Update or add schedule
1072
- if is_edit and st.session_state.schedule_editor.get("original_name"):
1073
- original_name = st.session_state.schedule_editor["original_name"]
1074
- if original_name != name and original_name in schedules:
1075
- del schedules[original_name]
1076
- schedules[name] = schedule_data
1077
- st.success(f"Schedule '{name}' updated successfully!")
1078
- else:
1079
- schedules[name] = schedule_data
1080
- st.success(f"Schedule '{name}' added successfully!")
1081
-
1082
- # Clear editor state
1083
- st.session_state.schedule_editor = {}
1084
- st.session_state.module_rerun_flags["internal_loads"] = True
1085
- st.rerun()
1086
-
1087
- with col2:
1088
- if st.button("Cancel"):
1089
  st.session_state.schedule_editor = {}
1090
  st.session_state.module_rerun_flags["internal_loads"] = True
1091
  st.rerun()
 
 
 
 
 
 
1092
 
1093
  def is_schedule_in_use(schedule_name: str) -> bool:
1094
  """
 
922
  st.session_state.module_rerun_flags["internal_loads"] = True
923
  st.rerun()
924
 
925
+ # Custom CSS for vertical sliders resembling a column chart
926
  st.markdown("""
927
  <style>
928
  .slider-container {
929
  display: flex;
930
  flex-direction: row;
931
+ justify-content: space-evenly;
932
  align-items: flex-end;
933
+ gap: 4px;
934
+ padding: 20px 0;
 
935
  }
936
  .slider-item {
937
+ display: flex;
938
+ flex-direction: column;
939
+ align-items: center;
940
+ width: 40px;
941
+ }
942
+ .vertical-slider {
943
  writing-mode: bt-lr;
944
  transform: rotate(-90deg);
945
+ height: 200px;
946
  width: 30px;
947
  }
948
  .hour-label {
949
  text-align: center;
950
  font-size: 12px;
951
  margin-top: 5px;
952
+ font-weight: bold;
953
+ }
954
+ .stSlider > div > div > div > div {
955
+ background-color: #4CAF50 !important;
956
+ }
957
+ .stSlider > div > div > div > div > div {
958
+ background-color: #2196F3 !important;
959
  }
960
  </style>
961
  """, unsafe_allow_html=True)
962
 
963
  def display_schedules_tab():
964
+ """Display the schedules tab content with vertical sliders resembling a column chart."""
965
  st.subheader("Schedule Management")
966
 
967
+ # Initialize editor state
968
+ if "schedule_editor" not in st.session_state:
969
+ st.session_state.schedule_editor = {}
970
 
971
+ editor_state = st.session_state.get("schedule_editor", {})
972
+ is_edit = editor_state.get("is_edit", False)
 
 
 
 
 
973
 
974
+ # Schedule name and description
975
+ name = st.text_input(
976
+ "Schedule Name",
977
+ value=editor_state.get("name", ""),
978
+ help="Enter a unique name for this schedule."
979
+ )
980
+
981
+ description = st.text_area(
982
+ "Description",
983
+ value=editor_state.get("description", ""),
984
+ help="Brief description of this schedule."
985
+ )
986
+
987
+ # Template selection for new schedules
988
+ if not is_edit:
989
+ template = st.selectbox(
990
+ "Start from Template",
991
+ ["Custom"] + list(DEFAULT_SCHEDULE_TEMPLATES.keys()),
992
+ help="Select a template to start with, or choose Custom for blank schedule.",
993
+ key="schedule_template"
 
994
  )
995
+ else:
996
+ template = "Custom"
997
+
998
+ # Initialize schedule values
999
+ if template != "Custom" and not is_edit:
1000
+ weekday_values = DEFAULT_SCHEDULE_TEMPLATES[template]["weekday"]
1001
+ weekend_values = DEFAULT_SCHEDULE_TEMPLATES[template]["weekend"]
1002
+ else:
1003
+ weekday_values = editor_state.get("weekday", [0.0] * 24)
1004
+ weekend_values = editor_state.get("weekend", [0.0] * 24)
1005
+
1006
+ # Weekday schedule with vertical sliders
1007
+ st.write("**Weekday Schedule**")
1008
+ st.markdown('<div class="slider-container">', unsafe_allow_html=True)
1009
+ weekday_schedule = []
1010
+ for hour in range(24):
1011
+ with st.container():
1012
+ st.markdown('<div class="slider-item">', unsafe_allow_html=True)
1013
+ val = st.slider(
1014
+ label="",
1015
+ min_value=0.0,
1016
+ max_value=1.0,
1017
+ step=0.01,
1018
+ value=float(weekday_values[hour]),
1019
+ key=f"weekday_{hour}",
1020
+ help=f"Hour {hour}:00 weekday value",
1021
+ format="%.2f"
1022
  )
1023
+ st.markdown(f'<div class="hour-label">{hour:02d}:00</div>', unsafe_allow_html=True)
1024
+ st.markdown('</div>', unsafe_allow_html=True)
1025
+ weekday_schedule.append(val)
1026
+ st.markdown('</div>', unsafe_allow_html=True)
1027
+
1028
+ # Weekend schedule with vertical sliders
1029
+ st.write("**Weekend Schedule**")
1030
+ st.markdown('<div class="slider-container">', unsafe_allow_html=True)
1031
+ weekend_schedule = []
1032
+ for hour in range(24):
1033
+ with st.container():
1034
+ st.markdown('<div class="slider-item">', unsafe_allow_html=True)
1035
+ val = st.slider(
1036
+ label="",
1037
+ min_value=0.0,
1038
+ max_value=1.0,
1039
+ step=0.01,
1040
+ value=float(weekend_values[hour]),
1041
+ key=f"weekend_{hour}",
1042
+ help=f"Hour {hour}:00 weekend value",
1043
+ format="%.2f"
1044
+ )
1045
+ st.markdown(f'<div class="hour-label">{hour:02d}:00</div>', unsafe_allow_html=True)
1046
+ st.markdown('</div>', unsafe_allow_html=True)
1047
+ weekend_schedule.append(val)
1048
+ st.markdown('</div>', unsafe_allow_html=True)
1049
+
1050
+ # Display saved schedules table
1051
+ st.write("**Saved Schedules**")
1052
+ schedules = st.session_state.project_data["internal_loads"]["schedules"]
1053
+ if schedules:
1054
+ display_schedules_table(schedules)
1055
+ else:
1056
+ st.write("No schedules available.")
1057
+
1058
+ # Action buttons
1059
+ col1, col2 = st.columns(2)
1060
+ with col1:
1061
+ submit_label = "Update Schedule" if is_edit else "Add Schedule"
1062
+ if st.button(submit_label):
1063
+ # Validate inputs
1064
+ if not name.strip():
1065
+ st.error("Schedule name is required.")
1066
+ elif name in schedules and not is_edit:
1067
+ st.error("A schedule with this name already exists.")
1068
+ else:
1069
+ # Create schedule data
1070
+ schedule_data = {
1071
+ "description": description,
1072
+ "weekday": weekday_schedule,
1073
+ "weekend": weekend_schedule
1074
+ }
1075
+
1076
+ # Update or add schedule
1077
+ if is_edit and st.session_state.schedule_editor.get("original_name"):
1078
+ original_name = st.session_state.schedule_editor["original_name"]
1079
+ if original_name != name and original_name in schedules:
1080
+ del schedules[original_name]
1081
+ schedules[name] = schedule_data
1082
+ st.success(f"Schedule '{name}' updated successfully!")
 
 
 
 
 
 
 
 
1083
  else:
1084
+ schedules[name] = schedule_data
1085
+ st.success(f"Schedule '{name}' added successfully!")
1086
+
1087
+ # Clear editor state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1088
  st.session_state.schedule_editor = {}
1089
  st.session_state.module_rerun_flags["internal_loads"] = True
1090
  st.rerun()
1091
+
1092
+ with col2:
1093
+ if st.button("Cancel"):
1094
+ st.session_state.schedule_editor = {}
1095
+ st.session_state.module_rerun_flags["internal_loads"] = True
1096
+ st.rerun()
1097
 
1098
  def is_schedule_in_use(schedule_name: str) -> bool:
1099
  """