mabuseif commited on
Commit
50e226e
·
verified ·
1 Parent(s): bb1e87d

Update app/internal_loads.py

Browse files
Files changed (1) hide show
  1. app/internal_loads.py +63 -47
app/internal_loads.py CHANGED
@@ -943,70 +943,82 @@ def display_schedules_tab():
943
  editor_state = st.session_state.schedule_editor
944
  schedules = st.session_state.project_data["internal_loads"]["schedules"]
945
 
946
- # ---------------------- UI FORM for metadata ----------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
947
  with st.form("schedule_form"):
948
  name = st.text_input("Schedule Name", value=editor_state.get("name", ""))
949
  description = st.text_area("Description", value=editor_state.get("description", ""))
950
- template_options = list(DEFAULT_SCHEDULE_TEMPLATES.keys())
951
- selected_template = st.selectbox(
952
- "Select Template",
953
- options=["None"] + template_options,
954
- index=template_options.index(editor_state.get("template", "custom")) + 1
955
- if editor_state.get("template") in template_options else 0
956
- )
957
 
958
- # Load selected template into sliders
959
- if selected_template != editor_state.get("template", "None"):
960
- st.session_state.schedule_editor["template"] = selected_template
961
- if selected_template != "None":
962
- tpl = DEFAULT_SCHEDULE_TEMPLATES[selected_template]
963
- st.session_state.schedule_editor["weekday"] = tpl["weekday"]
964
- st.session_state.schedule_editor["weekend"] = tpl["weekend"]
965
 
966
- submitted = st.form_submit_button("Save Schedule")
 
 
 
 
 
 
 
967
 
968
- weekday_values = st.session_state.schedule_editor.get("weekday", [0.0] * 24)
969
- weekend_values = st.session_state.schedule_editor.get("weekend", [0.0] * 24)
970
 
971
- # ---------------------- UI SLIDERS ----------------------
972
- st.markdown("### Schedule Profile")
973
- col1, col2 = st.columns(2)
974
 
975
- with col1:
976
- st.markdown("#### Weekday")
977
- weekday_result = []
978
- for hour in range(24):
979
  val = st.slider(
980
- label=f"{hour:02d}:00",
 
981
  min_value=0.0,
982
  max_value=1.0,
983
  step=0.1,
984
- value=weekday_values[hour],
985
- key=f"weekday_slider_{hour}",
986
- label_visibility="collapsed", # cleaner interface
987
- help=f"Weekday value for {hour:02d}:00"
988
  )
989
- weekday_result.append(val)
990
- st.markdown(f"<small style='font-size:10px;'>{hour:02d}:00 – {val:.1f}</small>", unsafe_allow_html=True)
991
 
992
- with col2:
993
- st.markdown("#### Weekend")
994
- weekend_result = []
995
- for hour in range(24):
996
  val = st.slider(
997
- label=f"{hour:02d}:00",
 
998
  min_value=0.0,
999
  max_value=1.0,
1000
  step=0.1,
1001
- value=weekend_values[hour],
1002
- key=f"weekend_slider_{hour}",
1003
- label_visibility="collapsed",
1004
- help=f"Weekend value for {hour:02d}:00"
1005
  )
1006
- weekend_result.append(val)
1007
- st.markdown(f"<small style='font-size:10px;'>{hour:02d}:00 – {val:.1f}</small>", unsafe_allow_html=True)
1008
 
1009
- # ---------------------- Handle Save ----------------------
1010
  if submitted:
1011
  if not name.strip():
1012
  st.error("Schedule name is required.")
@@ -1015,13 +1027,17 @@ def display_schedules_tab():
1015
  else:
1016
  schedules[name] = {
1017
  "description": description,
1018
- "weekday": weekday_result,
1019
- "weekend": weekend_result
1020
  }
1021
 
1022
- # Reset editor to default state
1023
  st.session_state.schedule_editor = DEFAULT_STATE.copy()
 
 
 
1024
  st.success(f"Schedule '{name}' saved successfully.")
 
1025
 
1026
  # ---------------------- Show Saved ----------------------
1027
  st.markdown("### Saved Schedules")
 
943
  editor_state = st.session_state.schedule_editor
944
  schedules = st.session_state.project_data["internal_loads"]["schedules"]
945
 
946
+ # ---------------------- Handle template change (force update) ----------------------
947
+ template_options = list(DEFAULT_SCHEDULE_TEMPLATES.keys())
948
+ selected_template = st.selectbox(
949
+ "Select Template",
950
+ options=["None"] + template_options,
951
+ index=template_options.index(editor_state.get("template", "custom")) + 1
952
+ if editor_state.get("template") in template_options else 0,
953
+ help="Choose a base schedule to prefill values."
954
+ )
955
+
956
+ if selected_template != editor_state.get("template", "None"):
957
+ st.session_state.schedule_editor["template"] = selected_template
958
+ if selected_template != "None":
959
+ tpl = DEFAULT_SCHEDULE_TEMPLATES[selected_template]
960
+ st.session_state.schedule_editor["weekday"] = tpl["weekday"]
961
+ st.session_state.schedule_editor["weekend"] = tpl["weekend"]
962
+
963
+ # Update individual slider keys to force redraw
964
+ for hour in range(24):
965
+ st.session_state[f"weekday_slider_{hour}"] = tpl["weekday"][hour]
966
+ st.session_state[f"weekend_slider_{hour}"] = tpl["weekend"][hour]
967
+
968
+ st.rerun() # Rerun to refresh sliders after template update
969
+
970
+ # ---------------------- UI FORM for name/description only ----------------------
971
  with st.form("schedule_form"):
972
  name = st.text_input("Schedule Name", value=editor_state.get("name", ""))
973
  description = st.text_area("Description", value=editor_state.get("description", ""))
974
+ submitted = st.form_submit_button("Save Schedule")
 
 
 
 
 
 
975
 
976
+ # ---------------------- SLIDERS LAYOUT ----------------------
977
+ st.markdown("### Schedule Sliders")
 
 
 
 
 
978
 
979
+ # Table headers
980
+ col_hour, col_wd, col_we = st.columns([0.7, 2.0, 2.0])
981
+ with col_hour:
982
+ st.markdown("**Hour**")
983
+ with col_wd:
984
+ st.markdown("**Weekday**")
985
+ with col_we:
986
+ st.markdown("**Weekend**")
987
 
988
+ weekday_values = []
989
+ weekend_values = []
990
 
991
+ for hour in range(24):
992
+ col_hour, col_wd, col_we = st.columns([0.7, 2.0, 2.0])
 
993
 
994
+ with col_hour:
995
+ st.markdown(f"<div style='text-align:right; font-size:11px'>{hour:02d}</div>", unsafe_allow_html=True)
996
+
997
+ with col_wd:
998
  val = st.slider(
999
+ label=f"Weekday {hour:02d}",
1000
+ key=f"weekday_slider_{hour}",
1001
  min_value=0.0,
1002
  max_value=1.0,
1003
  step=0.1,
1004
+ value=st.session_state.get(f"weekday_slider_{hour}", 0.0),
1005
+ label_visibility="collapsed"
 
 
1006
  )
1007
+ weekday_values.append(val)
 
1008
 
1009
+ with col_we:
 
 
 
1010
  val = st.slider(
1011
+ label=f"Weekend {hour:02d}",
1012
+ key=f"weekend_slider_{hour}",
1013
  min_value=0.0,
1014
  max_value=1.0,
1015
  step=0.1,
1016
+ value=st.session_state.get(f"weekend_slider_{hour}", 0.0),
1017
+ label_visibility="collapsed"
 
 
1018
  )
1019
+ weekend_values.append(val)
 
1020
 
1021
+ # ---------------------- Save logic ----------------------
1022
  if submitted:
1023
  if not name.strip():
1024
  st.error("Schedule name is required.")
 
1027
  else:
1028
  schedules[name] = {
1029
  "description": description,
1030
+ "weekday": weekday_values,
1031
+ "weekend": weekend_values
1032
  }
1033
 
1034
+ # Reset safely without losing structure
1035
  st.session_state.schedule_editor = DEFAULT_STATE.copy()
1036
+ for hour in range(24):
1037
+ st.session_state.pop(f"weekday_slider_{hour}", None)
1038
+ st.session_state.pop(f"weekend_slider_{hour}", None)
1039
  st.success(f"Schedule '{name}' saved successfully.")
1040
+ st.rerun()
1041
 
1042
  # ---------------------- Show Saved ----------------------
1043
  st.markdown("### Saved Schedules")