mabuseif commited on
Commit
5e350cf
·
verified ·
1 Parent(s): d72b619

Update app/internal_loads.py

Browse files
Files changed (1) hide show
  1. app/internal_loads.py +71 -139
app/internal_loads.py CHANGED
@@ -934,8 +934,7 @@ def display_schedules_tab():
934
  "template": "None",
935
  "weekday": [0.0] * 24,
936
  "weekend": [0.0] * 24,
937
- "is_edit": False,
938
- "original_name": ""
939
  }
940
 
941
  if "schedule_editor" not in st.session_state:
@@ -963,162 +962,95 @@ def display_schedules_tab():
963
  for hour in range(24):
964
  st.session_state[f"weekday_slider_{hour}"] = tpl["weekday"][hour]
965
  st.session_state[f"weekend_slider_{hour}"] = tpl["weekend"][hour]
966
- st.rerun()
967
 
968
- # ---------------------- UI FORM for name/description and actions ----------------------
969
  with st.form("schedule_form"):
970
- name = st.text_input("Schedule Name", value=editor_state.get("name", ""), disabled=editor_state.get("is_edit", False))
971
  description = st.text_area("Description", value=editor_state.get("description", ""))
 
972
 
973
- # Action buttons
974
- col1, col2, col3 = st.columns(3)
975
- with col1:
976
- submit_label = "Update Schedule" if editor_state.get("is_edit", False) else "Save Schedule"
977
- submit = st.form_submit_button(submit_label)
978
- with col2:
979
- cancel = st.form_submit_button("Cancel")
980
- with col3:
981
- delete = st.form_submit_button("Delete") if editor_state.get("is_edit", False) and name not in DEFAULT_SCHEDULE_TEMPLATES else None
982
-
983
- # ---------------------- SLIDERS LAYOUT ----------------------
984
- st.markdown("### Schedule Sliders")
985
-
986
- # Table headers
987
- col_hour, col_wd, col_we = st.columns([0.4, 2.0, 2.0])
988
- with col_hour:
989
- st.markdown("<div style='text-align:center; font-weight:bold; font-size:18px;'>Hour</div>", unsafe_allow_html=True)
990
- with col_wd:
991
- st.markdown("<div style='text-align:center; font-weight:bold; font-size:18px;'>Weekday</div>", unsafe_allow_html=True)
992
- with col_we:
993
- st.markdown("<div style='text-align:center; font-weight:bold; font-size:18px;'>Weekend</div>", unsafe_allow_html=True)
994
-
995
- hide_elements = """
996
- <style>
997
- div[data-testid="stSliderTickBarMin"],
998
- div[data-testid="stSliderTickBarMax"] {
999
- display: none;
1000
- }
1001
- </style>
1002
- """
1003
- st.markdown(hide_elements, unsafe_allow_html=True)
1004
 
1005
- weekday_values = []
1006
- weekend_values = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1007
 
1008
- for hour in range(24):
1009
- col_hour, col_wd, col_we = st.columns([0.4, 2.0, 2.0])
1010
 
1011
- with col_hour:
1012
- st.markdown(f"<div style='text-align:center; font-size:12px'>{hour:02d}</div>", unsafe_allow_html=True)
1013
 
1014
- with col_wd:
1015
- val = st.slider(
1016
- label=f"Weekday {hour:02d}",
1017
- key=f"weekday_slider_{hour}",
1018
- min_value=0.0,
1019
- max_value=1.0,
1020
- step=0.1,
1021
- value=st.session_state.get(f"weekday_slider_{hour}", 0.0),
1022
- label_visibility="collapsed",
1023
- format=None
1024
- )
1025
- weekday_values.append(val)
1026
 
1027
- with col_we:
1028
- val = st.slider(
1029
- label=f"Weekend {hour:02d}",
1030
- key=f"weekend_slider_{hour}",
1031
- min_value=0.0,
1032
- max_value=1.0,
1033
- step=0.1,
1034
- value=st.session_state.get(f"weekend_slider_{hour}", 0.0),
1035
- label_visibility="collapsed",
1036
- format=None
1037
- )
1038
- weekend_values.append(val)
1039
 
1040
- # ---------------------- Save/Update logic ----------------------
1041
- if submit:
1042
- if not name.strip():
1043
- st.error("Schedule name is required.")
1044
- elif name in schedules and not editor_state.get("is_edit", False):
1045
- st.error("A schedule with this name already exists.")
1046
- else:
1047
- schedules[name] = {
1048
- "description": description,
1049
- "weekday": weekday_values,
1050
- "weekend": weekend_values
1051
- }
1052
- if editor_state.get("is_edit", False) and editor_state.get("original_name") != name:
1053
- if editor_state.get("original_name") in schedules:
1054
- del schedules[editor_state["original_name"]]
1055
- st.session_state.schedule_editor = DEFAULT_STATE.copy()
1056
- for hour in range(24):
1057
- st.session_state.pop(f"weekday_slider_{hour}", None)
1058
- st.session_state.pop(f"weekend_slider_{hour}", None)
1059
- st.success(f"Schedule '{name}' {'updated' if editor_state.get('is_edit', False) else 'saved'} successfully.")
1060
- st.session_state.module_rerun_flags["internal_loads"] = True
1061
- st.rerun()
1062
 
1063
- # ---------------------- Delete logic ----------------------
1064
- if delete and editor_state.get("is_edit", False):
1065
- if name in DEFAULT_SCHEDULE_TEMPLATES:
1066
- st.error("Cannot delete default schedule templates.")
1067
- elif is_schedule_in_use(name):
1068
- st.error(f"Cannot delete schedule '{name}' because it is in use.")
1069
- else:
1070
- del schedules[name]
1071
- st.session_state.schedule_editor = DEFAULT_STATE.copy()
1072
- for hour in range(24):
1073
- st.session_state.pop(f"weekday_slider_{hour}", None)
1074
- st.session_state.pop(f"weekend_slider_{hour}", None)
1075
- st.success(f"Schedule '{name}' deleted successfully.")
1076
- st.session_state.module_rerun_flags["internal_loads"] = True
1077
- st.rerun()
1078
 
1079
- # ---------------------- Cancel logic ----------------------
1080
- if cancel:
1081
  st.session_state.schedule_editor = DEFAULT_STATE.copy()
1082
  for hour in range(24):
1083
  st.session_state.pop(f"weekday_slider_{hour}", None)
1084
  st.session_state.pop(f"weekend_slider_{hour}", None)
1085
- st.session_state.module_rerun_flags["internal_loads"] = True
1086
- st.rerun()
1087
-
1088
  # ---------------------- Show Saved ----------------------
1089
  st.markdown("### Saved Schedules")
1090
  if schedules:
1091
- table_data = [
1092
- {"Name": name, "Description": schedule.get("description", "No description")}
1093
- for name, schedule in schedules.items()
1094
- ]
1095
- df = pd.DataFrame(table_data)
1096
- st.dataframe(df, use_container_width=True)
1097
-
1098
- # Action buttons for selecting/editing schedules
1099
- st.write("**Select Schedule to Edit:**")
1100
- for i, row in enumerate(table_data):
1101
- schedule_name = row["Name"]
1102
- col1, col2 = st.columns([2, 1])
1103
- with col1:
1104
- st.write(f"{i+1}. {schedule_name}")
1105
- with col2:
1106
- if st.button("Edit", key=f"edit_schedule_{schedule_name}_{i}"):
1107
- schedule_data = schedules[schedule_name]
1108
- st.session_state.schedule_editor = {
1109
- "is_edit": True,
1110
- "original_name": schedule_name,
1111
- "name": schedule_name,
1112
- "description": schedule_data.get("description", ""),
1113
- "weekday": schedule_data.get("weekday", [0.0] * 24),
1114
- "weekend": schedule_data.get("weekend", [0.0] * 24),
1115
- "template": "None"
1116
- }
1117
- for hour in range(24):
1118
- st.session_state[f"weekday_slider_{hour}"] = schedule_data.get("weekday", [0.0] * 24)[hour]
1119
- st.session_state[f"weekend_slider_{hour}"] = schedule_data.get("weekend", [0.0] * 24)[hour]
1120
- st.session_state.module_rerun_flags["internal_loads"] = True
1121
- st.rerun()
1122
  else:
1123
  st.info("No schedules saved yet.")
1124
 
 
934
  "template": "None",
935
  "weekday": [0.0] * 24,
936
  "weekend": [0.0] * 24,
937
+ "is_edit": False
 
938
  }
939
 
940
  if "schedule_editor" not in st.session_state:
 
962
  for hour in range(24):
963
  st.session_state[f"weekday_slider_{hour}"] = tpl["weekday"][hour]
964
  st.session_state[f"weekend_slider_{hour}"] = tpl["weekend"][hour]
965
+ st.experimental_rerun()
966
 
967
+ # ---------------------- UI FORM for name/description only ----------------------
968
  with st.form("schedule_form"):
969
+ name = st.text_input("Schedule Name", value=editor_state.get("name", ""))
970
  description = st.text_area("Description", value=editor_state.get("description", ""))
971
+ submitted = st.form_submit_button("Save Schedule")
972
 
973
+ # ---------------------- SLIDERS LAYOUT ----------------------
974
+ st.markdown("### Schedule Sliders")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
975
 
976
+ # Table headers
977
+ col_hour, col_wd, col_we = st.columns([0.4, 2.0, 2.0])
978
+ with col_hour:
979
+ st.markdown("<div style='text-align:center; font-weight:bold; font-size:18px;'>Hour</div>", unsafe_allow_html=True)
980
+ with col_wd:
981
+ st.markdown("<div style='text-align:center; font-weight:bold; font-size:18px;'>Weekday</div>", unsafe_allow_html=True)
982
+ with col_we:
983
+ st.markdown("<div style='text-align:center; font-weight:bold; font-size:18px;'>Weekend</div>", unsafe_allow_html=True)
984
+
985
+ hide_elements = """
986
+ <style>
987
+ div[data-testid="stSliderTickBarMin"],
988
+ div[data-testid="stSliderTickBarMax"] {
989
+ display: none;
990
+ }
991
+ </style>
992
+ """
993
+
994
+ st.markdown(hide_elements, unsafe_allow_html=True)
995
 
996
+ weekday_values = []
997
+ weekend_values = []
998
 
999
+ for hour in range(24):
1000
+ col_hour, col_wd, col_we = st.columns([0.4, 2.0, 2.0])
1001
 
1002
+ with col_hour:
1003
+ st.markdown(f"<div style='text-align:center; font-size:12px'>{hour:02d}</div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
1004
 
1005
+ with col_wd:
1006
+ val = st.slider(
1007
+ label=f"Weekday {hour:02d}",
1008
+ key=f"weekday_slider_{hour}",
1009
+ min_value=0.0,
1010
+ max_value=1.0,
1011
+ step=0.1,
1012
+ value=st.session_state.get(f"weekday_slider_{hour}", 0.0),
1013
+ label_visibility="collapsed",
1014
+ format=None # hides value text below slider
1015
+ )
1016
+ weekday_values.append(val)
1017
 
1018
+ with col_we:
1019
+ val = st.slider(
1020
+ label=f"Weekend {hour:02d}",
1021
+ key=f"weekend_slider_{hour}",
1022
+ min_value=0.0,
1023
+ max_value=1.0,
1024
+ step=0.1,
1025
+ value=st.session_state.get(f"weekend_slider_{hour}", 0.0),
1026
+ label_visibility="collapsed",
1027
+ format=None
1028
+ )
1029
+ weekend_values.append(val)
 
 
 
 
 
 
 
 
 
 
1030
 
1031
+ # ---------------------- Save logic ----------------------
1032
+ if submitted:
1033
+ if not name.strip():
1034
+ st.error("Schedule name is required.")
1035
+ elif name in schedules and not editor_state.get("is_edit"):
1036
+ st.error("A schedule with this name already exists.")
1037
+ else:
1038
+ schedules[name] = {
1039
+ "description": description,
1040
+ "weekday": weekday_values,
1041
+ "weekend": weekend_values
1042
+ }
 
 
 
1043
 
 
 
1044
  st.session_state.schedule_editor = DEFAULT_STATE.copy()
1045
  for hour in range(24):
1046
  st.session_state.pop(f"weekday_slider_{hour}", None)
1047
  st.session_state.pop(f"weekend_slider_{hour}", None)
1048
+ st.success(f"Schedule '{name}' saved successfully.")
1049
+
 
1050
  # ---------------------- Show Saved ----------------------
1051
  st.markdown("### Saved Schedules")
1052
  if schedules:
1053
+ display_schedules_table(schedules)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1054
  else:
1055
  st.info("No schedules saved yet.")
1056