mabuseif commited on
Commit
1263c0f
·
verified ·
1 Parent(s): 00ba1a9

Update app/internal_loads.py

Browse files
Files changed (1) hide show
  1. app/internal_loads.py +29 -34
app/internal_loads.py CHANGED
@@ -924,28 +924,19 @@ def display_ventilation_infiltration_tab():
924
  st.rerun()
925
 
926
  def display_schedules_tab():
927
- """
928
- Display the schedules tab content with vertical sliders for hourly schedules.
929
- """
930
  import streamlit as st
931
  from streamlit_vertical_slider import vertical_slider
932
 
933
  st.subheader("Schedules Editor")
934
 
935
- # Initialise schedule editor state
936
  if "schedule_editor" not in st.session_state:
937
  st.session_state.schedule_editor = {}
938
 
939
- if "template_selected" not in st.session_state:
940
- st.session_state.template_selected = False
941
-
942
  editor_state = st.session_state.schedule_editor
943
  is_edit = editor_state.get("is_edit", False)
944
 
945
- # Load available templates
946
  template_options = list(DEFAULT_SCHEDULE_TEMPLATES.keys())
947
 
948
- # Text inputs
949
  name = st.text_input("Schedule Name", value=editor_state.get("name", ""))
950
  description = st.text_area("Description", value=editor_state.get("description", ""))
951
 
@@ -956,7 +947,7 @@ def display_schedules_tab():
956
  if editor_state.get("template") in template_options else 0
957
  )
958
 
959
- # Set flag and rerun if new template is selected
960
  if selected_template != editor_state.get("template", "None"):
961
  if selected_template != "None":
962
  template_data = DEFAULT_SCHEDULE_TEMPLATES[selected_template]
@@ -977,13 +968,18 @@ def display_schedules_tab():
977
  "description": description,
978
  "is_edit": is_edit,
979
  }
 
 
 
 
 
 
980
  st.rerun()
981
 
982
- # Get values from editor state
983
  weekday_values = editor_state.get("weekday", [0.0] * 24)
984
  weekend_values = editor_state.get("weekend", [0.0] * 24)
985
 
986
- # CSS styling
987
  st.markdown("""
988
  <style>
989
  .slider-container {
@@ -1015,44 +1011,45 @@ def display_schedules_tab():
1015
  </style>
1016
  """, unsafe_allow_html=True)
1017
 
1018
- # Reusable vertical slider row
1019
  def render_sliders(prefix, values, colour):
1020
  st.markdown('<div class="slider-container">', unsafe_allow_html=True)
1021
- results = []
1022
  for hour in range(24):
1023
  st.markdown('<div class="slider-item">', unsafe_allow_html=True)
1024
- val = vertical_slider(
1025
- label="",
1026
- key=f"{prefix}_{hour}",
1027
- height=150,
1028
- step=0.01,
1029
- min_value=0.0,
1030
- max_value=1.0,
1031
- default_value=values[hour],
1032
- slider_color=colour,
1033
- track_color="#e0e0e0",
1034
- thumb_color="#333",
1035
- value_always_visible=False
 
 
1036
  )
1037
  st.markdown(f'<div class="hour-label">{hour:02d}:00</div>', unsafe_allow_html=True)
1038
  st.markdown('</div>', unsafe_allow_html=True)
1039
- results.append(val)
1040
  st.markdown('</div>', unsafe_allow_html=True)
1041
- return results
1042
 
1043
- # Render sliders
1044
  st.write("**Weekday Schedule**")
1045
  weekday_schedule = render_sliders("weekday", weekday_values, "#4CAF50")
1046
 
1047
  st.write("**Weekend Schedule**")
1048
  weekend_schedule = render_sliders("weekend", weekend_values, "#2196F3")
1049
 
1050
- # Save or update
1051
  schedules = st.session_state.project_data["internal_loads"]["schedules"]
1052
  col1, col2 = st.columns(2)
1053
 
1054
  with col1:
1055
- if st.button("Save" if is_edit else "Add"):
 
1056
  if not name.strip():
1057
  st.error("Schedule name is required.")
1058
  elif name in schedules and not is_edit:
@@ -1070,9 +1067,7 @@ def display_schedules_tab():
1070
  del schedules[original_name]
1071
  schedules[name] = schedule_data
1072
 
1073
- # Reset state
1074
  st.session_state.schedule_editor = {}
1075
- st.session_state.module_rerun_flags["internal_loads"] = True
1076
  st.success(f"Schedule '{name}' saved.")
1077
  st.rerun()
1078
 
@@ -1081,7 +1076,7 @@ def display_schedules_tab():
1081
  st.session_state.schedule_editor = {}
1082
  st.rerun()
1083
 
1084
- # Show saved schedules (optional)
1085
  st.write("**Saved Schedules**")
1086
  if schedules:
1087
  display_schedules_table(schedules)
 
924
  st.rerun()
925
 
926
  def display_schedules_tab():
 
 
 
927
  import streamlit as st
928
  from streamlit_vertical_slider import vertical_slider
929
 
930
  st.subheader("Schedules Editor")
931
 
 
932
  if "schedule_editor" not in st.session_state:
933
  st.session_state.schedule_editor = {}
934
 
 
 
 
935
  editor_state = st.session_state.schedule_editor
936
  is_edit = editor_state.get("is_edit", False)
937
 
 
938
  template_options = list(DEFAULT_SCHEDULE_TEMPLATES.keys())
939
 
 
940
  name = st.text_input("Schedule Name", value=editor_state.get("name", ""))
941
  description = st.text_area("Description", value=editor_state.get("description", ""))
942
 
 
947
  if editor_state.get("template") in template_options else 0
948
  )
949
 
950
+ # If template changed, load new values and reset sliders
951
  if selected_template != editor_state.get("template", "None"):
952
  if selected_template != "None":
953
  template_data = DEFAULT_SCHEDULE_TEMPLATES[selected_template]
 
968
  "description": description,
969
  "is_edit": is_edit,
970
  }
971
+
972
+ # Clear existing slider keys
973
+ for hour in range(24):
974
+ st.session_state.pop(f"weekday_{hour}", None)
975
+ st.session_state.pop(f"weekend_{hour}", None)
976
+
977
  st.rerun()
978
 
 
979
  weekday_values = editor_state.get("weekday", [0.0] * 24)
980
  weekend_values = editor_state.get("weekend", [0.0] * 24)
981
 
982
+ # CSS layout and visual fix
983
  st.markdown("""
984
  <style>
985
  .slider-container {
 
1011
  </style>
1012
  """, unsafe_allow_html=True)
1013
 
 
1014
  def render_sliders(prefix, values, colour):
1015
  st.markdown('<div class="slider-container">', unsafe_allow_html=True)
1016
+ sliders = []
1017
  for hour in range(24):
1018
  st.markdown('<div class="slider-item">', unsafe_allow_html=True)
1019
+ sliders.append(
1020
+ vertical_slider(
1021
+ label="",
1022
+ key=f"{prefix}_{hour}",
1023
+ height=150,
1024
+ step=0.01,
1025
+ min_value=0.0,
1026
+ max_value=1.0,
1027
+ default_value=values[hour],
1028
+ slider_color=colour,
1029
+ track_color="#e0e0e0",
1030
+ thumb_color="#333",
1031
+ value_always_visible=False
1032
+ )
1033
  )
1034
  st.markdown(f'<div class="hour-label">{hour:02d}:00</div>', unsafe_allow_html=True)
1035
  st.markdown('</div>', unsafe_allow_html=True)
 
1036
  st.markdown('</div>', unsafe_allow_html=True)
1037
+ return sliders
1038
 
1039
+ # Render weekday/weekend sliders
1040
  st.write("**Weekday Schedule**")
1041
  weekday_schedule = render_sliders("weekday", weekday_values, "#4CAF50")
1042
 
1043
  st.write("**Weekend Schedule**")
1044
  weekend_schedule = render_sliders("weekend", weekend_values, "#2196F3")
1045
 
1046
+ # Save / update schedule
1047
  schedules = st.session_state.project_data["internal_loads"]["schedules"]
1048
  col1, col2 = st.columns(2)
1049
 
1050
  with col1:
1051
+ submit_label = "Update Schedule" if is_edit else "Add Schedule"
1052
+ if st.button(submit_label):
1053
  if not name.strip():
1054
  st.error("Schedule name is required.")
1055
  elif name in schedules and not is_edit:
 
1067
  del schedules[original_name]
1068
  schedules[name] = schedule_data
1069
 
 
1070
  st.session_state.schedule_editor = {}
 
1071
  st.success(f"Schedule '{name}' saved.")
1072
  st.rerun()
1073
 
 
1076
  st.session_state.schedule_editor = {}
1077
  st.rerun()
1078
 
1079
+ # Display saved schedules
1080
  st.write("**Saved Schedules**")
1081
  if schedules:
1082
  display_schedules_table(schedules)