Spaces:
Sleeping
Sleeping
Update app/internal_loads.py
Browse files- app/internal_loads.py +54 -61
app/internal_loads.py
CHANGED
@@ -924,6 +924,8 @@ def display_ventilation_infiltration_tab():
|
|
924 |
st.rerun()
|
925 |
|
926 |
def display_schedules_tab():
|
|
|
|
|
927 |
st.subheader("Schedules Editor")
|
928 |
|
929 |
DEFAULT_STATE = {
|
@@ -941,7 +943,7 @@ def display_schedules_tab():
|
|
941 |
editor_state = st.session_state.schedule_editor
|
942 |
schedules = st.session_state.project_data["internal_loads"]["schedules"]
|
943 |
|
944 |
-
#
|
945 |
with st.form("schedule_form"):
|
946 |
name = st.text_input("Schedule Name", value=editor_state.get("name", ""))
|
947 |
description = st.text_area("Description", value=editor_state.get("description", ""))
|
@@ -953,89 +955,80 @@ def display_schedules_tab():
|
|
953 |
if editor_state.get("template") in template_options else 0
|
954 |
)
|
955 |
|
956 |
-
#
|
957 |
if selected_template != editor_state.get("template", "None"):
|
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 |
submitted = st.form_submit_button("Save Schedule")
|
968 |
|
969 |
-
|
970 |
-
st.
|
971 |
-
|
972 |
-
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
-
|
978 |
-
|
979 |
-
|
980 |
-
|
981 |
-
|
982 |
min_value=0.0,
|
983 |
max_value=1.0,
|
984 |
-
|
985 |
-
|
986 |
-
|
987 |
-
|
988 |
-
|
989 |
)
|
990 |
-
|
991 |
-
|
992 |
|
993 |
-
|
994 |
-
|
995 |
-
|
996 |
-
|
997 |
-
|
998 |
-
|
999 |
-
default_value = st.session_state.get(key)
|
1000 |
-
val = vertical_slider(
|
1001 |
-
label="",
|
1002 |
-
key=key,
|
1003 |
-
height=150,
|
1004 |
-
step=0.1,
|
1005 |
min_value=0.0,
|
1006 |
max_value=1.0,
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
|
1011 |
-
|
1012 |
)
|
1013 |
-
|
1014 |
-
|
1015 |
|
1016 |
-
# Handle
|
1017 |
if submitted:
|
1018 |
-
|
1019 |
-
|
1020 |
-
|
1021 |
-
|
1022 |
-
|
1023 |
-
|
1024 |
-
|
1025 |
-
|
1026 |
-
|
1027 |
-
|
1028 |
-
|
1029 |
-
|
1030 |
-
|
1031 |
-
|
1032 |
-
st.error(f"An error occurred: {e}")
|
1033 |
|
|
|
1034 |
st.markdown("### Saved Schedules")
|
1035 |
if schedules:
|
1036 |
display_schedules_table(schedules)
|
1037 |
else:
|
1038 |
-
st.info("No schedules saved.")
|
1039 |
|
1040 |
def is_schedule_in_use(schedule_name: str) -> bool:
|
1041 |
"""
|
|
|
924 |
st.rerun()
|
925 |
|
926 |
def display_schedules_tab():
|
927 |
+
import streamlit as st
|
928 |
+
|
929 |
st.subheader("Schedules Editor")
|
930 |
|
931 |
DEFAULT_STATE = {
|
|
|
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", ""))
|
|
|
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.")
|
1013 |
+
elif name in schedules and not editor_state.get("is_edit"):
|
1014 |
+
st.error("A schedule with this name already exists.")
|
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")
|
1028 |
if schedules:
|
1029 |
display_schedules_table(schedules)
|
1030 |
else:
|
1031 |
+
st.info("No schedules saved yet.")
|
1032 |
|
1033 |
def is_schedule_in_use(schedule_name: str) -> bool:
|
1034 |
"""
|