Spaces:
Sleeping
Sleeping
Update app/component_selection.py
Browse files- app/component_selection.py +120 -120
app/component_selection.py
CHANGED
|
@@ -376,129 +376,129 @@ class ComponentSelectionInterface:
|
|
| 376 |
st.info(f"No {type_name} components defined yet.")
|
| 377 |
|
| 378 |
def _display_add_component_form(self, session_state: Any, component_type: ComponentType) -> None:
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
# Add a flag to prevent infinite rerun
|
| 383 |
-
if f"add_{type_name}_submitted" not in session_state:
|
| 384 |
-
session_state[f"add_{type_name}_submitted"] = False
|
| 385 |
-
|
| 386 |
-
with st.form(f"add_{type_name}_form"):
|
| 387 |
-
col1, col2 = st.columns(2)
|
| 388 |
-
with col1:
|
| 389 |
-
name = st.text_input("Name", f"New {type_name.capitalize()}")
|
| 390 |
-
area = st.number_input("Area (m²)", min_value=0.0, value=1.0, step=0.1)
|
| 391 |
-
if component_type not in [ComponentType.ROOF, ComponentType.FLOOR]:
|
| 392 |
-
orientation = st.selectbox("Orientation", [o.value for o in Orientation], index=0)
|
| 393 |
-
else:
|
| 394 |
-
orientation = Orientation.HORIZONTAL.value
|
| 395 |
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
preset_options = {comp.name: comp.id for comp in preset_components}
|
| 400 |
-
selected_preset = st.selectbox(f"Select Preset {type_name.capitalize()}", options=list(preset_options.keys()))
|
| 401 |
-
component = self.component_library.get_component(preset_options[selected_preset])
|
| 402 |
-
u_value = st.number_input("U-Value (W/m²·K)", value=component.u_value, disabled=True)
|
| 403 |
-
if component_type == ComponentType.WALL:
|
| 404 |
-
st.text_input("Wall Type", value=component.wall_type, disabled=True)
|
| 405 |
-
st.text_input("Wall Group", value=component.wall_group, disabled=True)
|
| 406 |
-
elif component_type == ComponentType.ROOF:
|
| 407 |
-
st.text_input("Roof Type", value=component.roof_type, disabled=True)
|
| 408 |
-
st.text_input("Roof Group", value=component.roof_group, disabled=True)
|
| 409 |
-
elif component_type == ComponentType.FLOOR:
|
| 410 |
-
st.text_input("Floor Type", value=component.floor_type, disabled=True)
|
| 411 |
-
elif component_type == ComponentType.WINDOW:
|
| 412 |
-
st.number_input("SHGC", value=component.shgc, disabled=True)
|
| 413 |
-
st.number_input("VT", value=component.vt, disabled=True)
|
| 414 |
-
st.text_input("Window Type", value=component.window_type, disabled=True)
|
| 415 |
-
elif component_type == ComponentType.DOOR:
|
| 416 |
-
st.text_input("Door Type", value=component.door_type, disabled=True)
|
| 417 |
-
else:
|
| 418 |
-
u_value = st.number_input("U-Value (W/m²·K)", min_value=0.0, value=0.5, step=0.01)
|
| 419 |
-
if component_type == ComponentType.WALL:
|
| 420 |
-
wall_type = st.text_input("Wall Type", "Custom")
|
| 421 |
-
wall_group = st.selectbox("Wall Group", ["A", "B", "C", "D", "E", "F", "G", "H"], index=1)
|
| 422 |
-
elif component_type == ComponentType.ROOF:
|
| 423 |
-
roof_type = st.text_input("Roof Type", "Custom")
|
| 424 |
-
roof_group = st.selectbox("Roof Group", ["A", "B", "C", "D", "E", "F", "G"], index=2)
|
| 425 |
-
elif component_type == ComponentType.FLOOR:
|
| 426 |
-
floor_type = st.text_input("Floor Type", "Custom")
|
| 427 |
-
elif component_type == ComponentType.WINDOW:
|
| 428 |
-
shgc = st.number_input("SHGC", min_value=0.0, max_value=1.0, value=0.7, step=0.01)
|
| 429 |
-
vt = st.number_input("VT", min_value=0.0, max_value=1.0, value=0.7, step=0.01)
|
| 430 |
-
window_type = st.text_input("Window Type", "Custom")
|
| 431 |
-
glazing_layers = st.selectbox("Glazing Layers", [1, 2, 3], index=1)
|
| 432 |
-
gas_fill = st.selectbox("Gas Fill", ["Air", "Argon", "Krypton"], index=0)
|
| 433 |
-
low_e_coating = st.checkbox("Low-E Coating")
|
| 434 |
-
shading_options = {d.name: d.id for d in self.shading_system.shading_devices.values()}
|
| 435 |
-
shading_device = st.selectbox("Shading Device", options=list(shading_options.keys()), index=0)
|
| 436 |
-
coverage = st.number_input("Coverage (%)", min_value=0.0, max_value=100.0, value=100.0, step=5.0)
|
| 437 |
-
device = self.shading_system.get_device(shading_options[shading_device])
|
| 438 |
-
device.coverage_percentage = coverage
|
| 439 |
-
st.write(f"Effective SHGC: {self.shading_system.calculate_effective_shgc(shgc, shading_options[shading_device]):.2f}")
|
| 440 |
-
elif component_type == ComponentType.DOOR:
|
| 441 |
-
door_type = st.text_input("Door Type", "Custom")
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
st.
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
)
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
)
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
|
| 503 |
def _display_components_table(self, session_state: Any, component_type: ComponentType, components: List[BuildingComponent]) -> None:
|
| 504 |
type_name = component_type.value.lower()
|
|
|
|
| 376 |
st.info(f"No {type_name} components defined yet.")
|
| 377 |
|
| 378 |
def _display_add_component_form(self, session_state: Any, component_type: ComponentType) -> None:
|
| 379 |
+
type_name = component_type.value.lower()
|
| 380 |
+
preset_components = self.component_library.get_preset_components_by_type(component_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
+
# Add a flag to prevent infinite rerun
|
| 383 |
+
if f"add_{type_name}_submitted" not in session_state:
|
| 384 |
+
session_state[f"add_{type_name}_submitted"] = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
+
with st.form(f"add_{type_name}_form"):
|
| 387 |
+
col1, col2 = st.columns(2)
|
| 388 |
+
with col1:
|
| 389 |
+
name = st.text_input("Name", f"New {type_name.capitalize()}")
|
| 390 |
+
area = st.number_input("Area (m²)", min_value=0.0, value=1.0, step=0.1)
|
| 391 |
+
if component_type not in [ComponentType.ROOF, ComponentType.FLOOR]:
|
| 392 |
+
orientation = st.selectbox("Orientation", [o.value for o in Orientation], index=0)
|
| 393 |
+
else:
|
| 394 |
+
orientation = Orientation.HORIZONTAL.value
|
| 395 |
+
|
| 396 |
+
with col2:
|
| 397 |
+
selection_method = st.radio(f"{type_name.capitalize()} Selection Method", ["Select from Presets", "Custom Properties"])
|
| 398 |
+
if selection_method == "Select from Presets" and preset_components:
|
| 399 |
+
preset_options = {comp.name: comp.id for comp in preset_components}
|
| 400 |
+
selected_preset = st.selectbox(f"Select Preset {type_name.capitalize()}", options=list(preset_options.keys()))
|
| 401 |
+
component = self.component_library.get_component(preset_options[selected_preset])
|
| 402 |
+
u_value = st.number_input("U-Value (W/m²·K)", value=component.u_value, disabled=True)
|
| 403 |
+
if component_type == ComponentType.WALL:
|
| 404 |
+
st.text_input("Wall Type", value=component.wall_type, disabled=True)
|
| 405 |
+
st.text_input("Wall Group", value=component.wall_group, disabled=True)
|
| 406 |
+
elif component_type == ComponentType.ROOF:
|
| 407 |
+
st.text_input("Roof Type", value=component.roof_type, disabled=True)
|
| 408 |
+
st.text_input("Roof Group", value=component.roof_group, disabled=True)
|
| 409 |
+
elif component_type == ComponentType.FLOOR:
|
| 410 |
+
st.text_input("Floor Type", value=component.floor_type, disabled=True)
|
| 411 |
+
elif component_type == ComponentType.WINDOW:
|
| 412 |
+
st.number_input("SHGC", value=component.shgc, disabled=True)
|
| 413 |
+
st.number_input("VT", value=component.vt, disabled=True)
|
| 414 |
+
st.text_input("Window Type", value=component.window_type, disabled=True)
|
| 415 |
+
elif component_type == ComponentType.DOOR:
|
| 416 |
+
st.text_input("Door Type", value=component.door_type, disabled=True)
|
| 417 |
+
else:
|
| 418 |
+
u_value = st.number_input("U-Value (W/m²·K)", min_value=0.0, value=0.5, step=0.01)
|
| 419 |
+
if component_type == ComponentType.WALL:
|
| 420 |
+
wall_type = st.text_input("Wall Type", "Custom")
|
| 421 |
+
wall_group = st.selectbox("Wall Group", ["A", "B", "C", "D", "E", "F", "G", "H"], index=1)
|
| 422 |
+
elif component_type == ComponentType.ROOF:
|
| 423 |
+
roof_type = st.text_input("Roof Type", "Custom")
|
| 424 |
+
roof_group = st.selectbox("Roof Group", ["A", "B", "C", "D", "E", "F", "G"], index=2)
|
| 425 |
+
elif component_type == ComponentType.FLOOR:
|
| 426 |
+
floor_type = st.text_input("Floor Type", "Custom")
|
| 427 |
+
elif component_type == ComponentType.WINDOW:
|
| 428 |
+
shgc = st.number_input("SHGC", min_value=0.0, max_value=1.0, value=0.7, step=0.01)
|
| 429 |
+
vt = st.number_input("VT", min_value=0.0, max_value=1.0, value=0.7, step=0.01)
|
| 430 |
+
window_type = st.text_input("Window Type", "Custom")
|
| 431 |
+
glazing_layers = st.selectbox("Glazing Layers", [1, 2, 3], index=1)
|
| 432 |
+
gas_fill = st.selectbox("Gas Fill", ["Air", "Argon", "Krypton"], index=0)
|
| 433 |
+
low_e_coating = st.checkbox("Low-E Coating")
|
| 434 |
+
shading_options = {d.name: d.id for d in self.shading_system.shading_devices.values()}
|
| 435 |
+
shading_device = st.selectbox("Shading Device", options=list(shading_options.keys()), index=0)
|
| 436 |
+
coverage = st.number_input("Coverage (%)", min_value=0.0, max_value=100.0, value=100.0, step=5.0)
|
| 437 |
+
device = self.shading_system.get_device(shading_options[shading_device])
|
| 438 |
+
device.coverage_percentage = coverage
|
| 439 |
+
st.write(f"Effective SHGC: {self.shading_system.calculate_effective_shgc(shgc, shading_options[shading_device]):.2f}")
|
| 440 |
+
elif component_type == ComponentType.DOOR:
|
| 441 |
+
door_type = st.text_input("Door Type", "Custom")
|
| 442 |
+
|
| 443 |
+
submitted = st.form_submit_button("Add Component")
|
| 444 |
+
if submitted and not session_state[f"add_{type_name}_submitted"]:
|
| 445 |
+
if not name:
|
| 446 |
+
st.error(f"{type_name.capitalize()} name is required!")
|
| 447 |
+
elif area <= 0:
|
| 448 |
+
st.error(f"{type_name.capitalize()} area must be greater than zero!")
|
| 449 |
+
elif u_value <= 0:
|
| 450 |
+
st.error(f"{type_name.capitalize()} U-value must be greater than zero!")
|
| 451 |
+
else:
|
| 452 |
+
try:
|
| 453 |
+
if selection_method == "Select from Presets" and preset_components:
|
| 454 |
+
component_id = preset_options[selected_preset]
|
| 455 |
+
component = self.component_library.get_component(component_id)
|
| 456 |
+
# Create a copy of __dict__ and remove 'id' before unpacking
|
| 457 |
+
preset_attrs = component.__dict__.copy()
|
| 458 |
+
preset_attrs.pop('id', None) # Remove 'id' to avoid duplication
|
| 459 |
+
new_component = component.__class__(
|
| 460 |
+
id=str(uuid.uuid4()), name=name, area=area,
|
| 461 |
+
orientation=Orientation(orientation), **preset_attrs
|
| 462 |
)
|
| 463 |
+
else:
|
| 464 |
+
if component_type == ComponentType.WALL:
|
| 465 |
+
new_component = Wall(
|
| 466 |
+
id=str(uuid.uuid4()), name=name, u_value=u_value, area=area,
|
| 467 |
+
orientation=Orientation(orientation), wall_type=wall_type, wall_group=wall_group
|
| 468 |
+
)
|
| 469 |
+
elif component_type == ComponentType.ROOF:
|
| 470 |
+
new_component = Roof(
|
| 471 |
+
id=str(uuid.uuid4()), name=name, u_value=u_value, area=area,
|
| 472 |
+
orientation=Orientation(orientation), roof_type=roof_type, roof_group=roof_group
|
| 473 |
+
)
|
| 474 |
+
elif component_type == ComponentType.FLOOR:
|
| 475 |
+
new_component = Floor(
|
| 476 |
+
id=str(uuid.uuid4()), name=name, u_value=u_value, area=area,
|
| 477 |
+
orientation=Orientation(orientation), floor_type=floor_type
|
| 478 |
+
)
|
| 479 |
+
elif component_type == ComponentType.WINDOW:
|
| 480 |
+
new_component = Window(
|
| 481 |
+
id=str(uuid.uuid4()), name=name, u_value=u_value, area=area,
|
| 482 |
+
orientation=Orientation(orientation), shgc=shgc, vt=vt, window_type=window_type,
|
| 483 |
+
glazing_layers=glazing_layers, gas_fill=gas_fill, low_e_coating=low_e_coating,
|
| 484 |
+
shading_device_id=shading_options[shading_device]
|
| 485 |
+
)
|
| 486 |
+
elif component_type == ComponentType.DOOR:
|
| 487 |
+
new_component = Door(
|
| 488 |
+
id=str(uuid.uuid4()), name=name, u_value=u_value, area=area,
|
| 489 |
+
orientation=Orientation(orientation), door_type=door_type
|
| 490 |
+
)
|
| 491 |
+
self.component_library.add_component(new_component)
|
| 492 |
+
session_state.components[type_name + 's'].append(new_component)
|
| 493 |
+
st.success(f"Added {new_component.name}")
|
| 494 |
+
session_state[f"add_{type_name}_submitted"] = True # Set flag to prevent rerun loop
|
| 495 |
+
st.rerun()
|
| 496 |
+
except ValueError as e:
|
| 497 |
+
st.error(f"Error: {str(e)}")
|
| 498 |
+
|
| 499 |
+
# Reset the flag after rerun to allow new submissions
|
| 500 |
+
if session_state[f"add_{type_name}_submitted"]:
|
| 501 |
+
session_state[f"add_{type_name}_submitted"] = False
|
| 502 |
|
| 503 |
def _display_components_table(self, session_state: Any, component_type: ComponentType, components: List[BuildingComponent]) -> None:
|
| 504 |
type_name = component_type.value.lower()
|