mabuseif commited on
Commit
3bfee80
·
verified ·
1 Parent(s): 1263c0f

Update app/internal_loads.py

Browse files
Files changed (1) hide show
  1. app/internal_loads.py +64 -132
app/internal_loads.py CHANGED
@@ -923,161 +923,93 @@ def display_ventilation_infiltration_tab():
923
  st.session_state.module_rerun_flags["internal_loads"] = True
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
 
943
- selected_template = st.selectbox(
944
- "Select Template",
945
- options=["None"] + template_options,
946
- index=template_options.index(editor_state.get("template", "custom")) + 1
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]
954
- st.session_state.schedule_editor = {
955
- "template": selected_template,
956
- "weekday": template_data["weekday"],
957
- "weekend": template_data["weekend"],
958
- "name": name,
959
- "description": description,
960
- "is_edit": is_edit,
961
- }
962
- else:
963
- st.session_state.schedule_editor = {
964
- "template": "None",
965
- "weekday": [0.0] * 24,
966
- "weekend": [0.0] * 24,
967
- "name": name,
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 {
986
- display: flex;
987
- flex-direction: row;
988
- justify-content: flex-start;
989
- align-items: flex-end;
990
- gap: 6px;
991
- padding: 10px;
992
- overflow-x: auto;
993
- min-width: 100%;
994
- }
995
- .slider-item {
996
- display: flex;
997
- flex-direction: column;
998
- align-items: center;
999
- width: 45px;
1000
- }
1001
- .hour-label {
1002
- text-align: center;
1003
- font-size: 11px;
1004
- margin-top: 3px;
1005
- font-weight: bold;
1006
- white-space: nowrap;
1007
- }
1008
- .stVerticalSlider {
1009
- height: 150px !important;
1010
- }
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:
1056
- st.error("Schedule already exists.")
1057
  else:
1058
- schedule_data = {
1059
  "description": description,
1060
- "weekday": weekday_schedule,
1061
- "weekend": weekend_schedule
1062
  }
1063
-
1064
- if is_edit:
1065
- original_name = editor_state.get("original_name", name)
1066
- if original_name in schedules:
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
-
1074
- with col2:
1075
- if st.button("Cancel"):
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)
1083
  else:
 
923
  st.session_state.module_rerun_flags["internal_loads"] = True
924
  st.rerun()
925
 
926
+ import streamlit as st
927
+ from streamlit_vertical_slider import vertical_slider
 
928
 
929
+ def display_schedules_tab():
930
  st.subheader("Schedules Editor")
931
 
932
  if "schedule_editor" not in st.session_state:
933
+ st.session_state.schedule_editor = {
934
+ "name": "",
935
+ "description": "",
936
+ "template": "None",
937
+ "weekday": [0.0]*24,
938
+ "weekend": [0.0]*24,
939
+ "is_edit": False
940
+ }
941
 
942
  editor_state = st.session_state.schedule_editor
943
+ schedules = st.session_state.project_data["internal_loads"]["schedules"]
 
 
944
 
945
+ with st.form("schedule_form"):
946
+ name = st.text_input("Schedule Name", value=editor_state["name"])
947
+ description = st.text_area("Description", value=editor_state["description"])
948
+ template_options = list(DEFAULT_SCHEDULE_TEMPLATES.keys())
949
+ selected_template = st.selectbox(
950
+ "Select Template",
951
+ options=["None"] + template_options,
952
+ index=template_options.index(editor_state.get("template", "custom")) + 1
953
+ if editor_state.get("template") in template_options else 0
954
+ )
955
 
956
+ # Load template values and write into st.session_state directly
957
+ if selected_template != editor_state["template"]:
958
+ st.session_state.schedule_editor["template"] = selected_template
959
+ if selected_template != "None":
960
+ tpl = DEFAULT_SCHEDULE_TEMPLATES[selected_template]
961
+ for hour in range(24):
962
+ st.session_state[f"weekday_{hour}"] = tpl["weekday"][hour]
963
+ st.session_state[f"weekend_{hour}"] = tpl["weekend"][hour]
964
+ st.session_state.schedule_editor["weekday"] = tpl["weekday"]
965
+ st.session_state.schedule_editor["weekend"] = tpl["weekend"]
966
 
967
+ st.markdown("### Weekday Schedule")
968
+ cols = st.columns(24)
969
+ weekday_values = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
970
  for hour in range(24):
971
+ with cols[hour]:
972
+ val = st.slider(
973
+ label=f"{hour:02d}",
974
+ min_value=0.0,
975
+ max_value=1.0,
976
+ value=st.session_state.get(f"weekday_{hour}", 0.0),
977
+ step=0.1,
978
+ key=f"weekday_{hour}"
979
+ )
980
+ weekday_values.append(val)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
981
 
982
+ st.markdown("### Weekend Schedule")
983
+ cols = st.columns(24)
984
+ weekend_values = []
985
  for hour in range(24):
986
+ with cols[hour]:
987
+ val = st.slider(
988
+ label=f"{hour:02d}",
 
 
 
 
989
  min_value=0.0,
990
  max_value=1.0,
991
+ value=st.session_state.get(f"weekend_{hour}", 0.0),
992
+ step=0.1,
993
+ key=f"weekend_{hour}"
 
 
994
  )
995
+ weekend_values.append(val)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
996
 
997
+ submitted = st.form_submit_button("Save Schedule")
998
+ if submitted:
 
999
  if not name.strip():
1000
  st.error("Schedule name is required.")
1001
+ elif name in schedules and not editor_state.get("is_edit"):
1002
+ st.error("A schedule with this name already exists.")
1003
  else:
1004
+ schedules[name] = {
1005
  "description": description,
1006
+ "weekday": weekday_values,
1007
+ "weekend": weekend_values
1008
  }
 
 
 
 
 
 
 
1009
  st.session_state.schedule_editor = {}
1010
+ st.success(f"Schedule '{name}' saved successfully!")
 
 
 
 
 
 
1011
 
1012
+ st.write("### Saved Schedules")
 
1013
  if schedules:
1014
  display_schedules_table(schedules)
1015
  else: