Spaces:
Sleeping
Sleeping
Update app/internal_loads.py
Browse files- app/internal_loads.py +68 -50
app/internal_loads.py
CHANGED
@@ -923,28 +923,28 @@ def display_ventilation_infiltration_tab():
|
|
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
|
947 |
-
description = st.text_area("Description", value=editor_state
|
948 |
template_options = list(DEFAULT_SCHEDULE_TEMPLATES.keys())
|
949 |
selected_template = st.selectbox(
|
950 |
"Select Template",
|
@@ -953,8 +953,8 @@ def display_schedules_tab():
|
|
953 |
if editor_state.get("template") in template_options else 0
|
954 |
)
|
955 |
|
956 |
-
#
|
957 |
-
if selected_template != editor_state
|
958 |
st.session_state.schedule_editor["template"] = selected_template
|
959 |
if selected_template != "None":
|
960 |
tpl = DEFAULT_SCHEDULE_TEMPLATES[selected_template]
|
@@ -964,56 +964,74 @@ def display_schedules_tab():
|
|
964 |
st.session_state.schedule_editor["weekday"] = tpl["weekday"]
|
965 |
st.session_state.schedule_editor["weekend"] = tpl["weekend"]
|
966 |
|
967 |
-
st.
|
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 |
-
|
983 |
-
|
984 |
-
|
985 |
-
|
986 |
-
|
987 |
-
|
988 |
-
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
-
|
993 |
-
|
994 |
-
|
995 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
996 |
|
997 |
-
|
998 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("
|
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.
|
1013 |
if schedules:
|
1014 |
display_schedules_table(schedules)
|
1015 |
else:
|
1016 |
-
st.info("No schedules saved
|
1017 |
|
1018 |
def is_schedule_in_use(schedule_name: str) -> bool:
|
1019 |
"""
|
|
|
923 |
st.session_state.module_rerun_flags["internal_loads"] = True
|
924 |
st.rerun()
|
925 |
|
|
|
|
|
|
|
926 |
def display_schedules_tab():
|
927 |
st.subheader("Schedules Editor")
|
928 |
|
929 |
+
DEFAULT_STATE = {
|
930 |
+
"name": "",
|
931 |
+
"description": "",
|
932 |
+
"template": "None",
|
933 |
+
"weekday": [0.0] * 24,
|
934 |
+
"weekend": [0.0] * 24,
|
935 |
+
"is_edit": False
|
936 |
+
}
|
937 |
+
|
938 |
if "schedule_editor" not in st.session_state:
|
939 |
+
st.session_state.schedule_editor = DEFAULT_STATE.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
940 |
|
941 |
editor_state = st.session_state.schedule_editor
|
942 |
schedules = st.session_state.project_data["internal_loads"]["schedules"]
|
943 |
|
944 |
+
# Form for metadata only
|
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", ""))
|
948 |
template_options = list(DEFAULT_SCHEDULE_TEMPLATES.keys())
|
949 |
selected_template = st.selectbox(
|
950 |
"Select Template",
|
|
|
953 |
if editor_state.get("template") in template_options else 0
|
954 |
)
|
955 |
|
956 |
+
# If template changed, update values and sliders
|
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]
|
|
|
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 |
+
# Display weekday sliders
|
970 |
+
st.markdown("### Weekday Schedule")
|
971 |
+
cols = st.columns(24)
|
972 |
+
weekday_values = []
|
973 |
+
for hour, col in enumerate(cols):
|
974 |
+
with col:
|
975 |
+
val = vertical_slider(
|
976 |
+
label="",
|
977 |
+
key=f"weekday_{hour}",
|
978 |
+
height=150,
|
979 |
+
step=0.1,
|
980 |
+
min_value=0.0,
|
981 |
+
max_value=1.0,
|
982 |
+
default_value=st.session_state.get(f"weekday_{hour}", 0.0),
|
983 |
+
slider_color="#4CAF50",
|
984 |
+
track_color="#e0e0e0",
|
985 |
+
thumb_color="#333",
|
986 |
+
value_always_visible=False
|
987 |
+
)
|
988 |
+
st.markdown(f"<div style='text-align:center; font-size:10px'>{hour:02d}:00</div>", unsafe_allow_html=True)
|
989 |
+
weekday_values.append(val)
|
990 |
|
991 |
+
st.markdown("### Weekend Schedule")
|
992 |
+
cols = st.columns(24)
|
993 |
+
weekend_values = []
|
994 |
+
for hour, col in enumerate(cols):
|
995 |
+
with col:
|
996 |
+
val = vertical_slider(
|
997 |
+
label="",
|
998 |
+
key=f"weekend_{hour}",
|
999 |
+
height=150,
|
1000 |
+
step=0.1,
|
1001 |
+
min_value=0.0,
|
1002 |
+
max_value=1.0,
|
1003 |
+
default_value=st.session_state.get(f"weekend_{hour}", 0.0),
|
1004 |
+
slider_color="#2196F3",
|
1005 |
+
track_color="#e0e0e0",
|
1006 |
+
thumb_color="#333",
|
1007 |
+
value_always_visible=False
|
1008 |
+
)
|
1009 |
+
st.markdown(f"<div style='text-align:center; font-size:10px'>{hour:02d}:00</div>", unsafe_allow_html=True)
|
1010 |
+
weekend_values.append(val)
|
1011 |
+
|
1012 |
+
# Handle saving
|
1013 |
+
if submitted:
|
1014 |
+
try:
|
1015 |
if not name.strip():
|
1016 |
st.error("Schedule name is required.")
|
1017 |
elif name in schedules and not editor_state.get("is_edit"):
|
1018 |
+
st.error("Schedule already exists.")
|
1019 |
else:
|
1020 |
schedules[name] = {
|
1021 |
"description": description,
|
1022 |
"weekday": weekday_values,
|
1023 |
"weekend": weekend_values
|
1024 |
}
|
1025 |
+
st.session_state.schedule_editor = DEFAULT_STATE.copy()
|
1026 |
+
st.success(f"Schedule '{name}' saved successfully.")
|
1027 |
+
except Exception as e:
|
1028 |
+
st.error(f"An error occurred: {e}")
|
1029 |
|
1030 |
+
st.markdown("### Saved Schedules")
|
1031 |
if schedules:
|
1032 |
display_schedules_table(schedules)
|
1033 |
else:
|
1034 |
+
st.info("No schedules saved.")
|
1035 |
|
1036 |
def is_schedule_in_use(schedule_name: str) -> bool:
|
1037 |
"""
|