Spaces:
Sleeping
Sleeping
Update app/component_selection.py
Browse files- app/component_selection.py +36 -32
app/component_selection.py
CHANGED
|
@@ -371,18 +371,16 @@ class ComponentSelectionInterface:
|
|
| 371 |
if components:
|
| 372 |
st.subheader(f"Existing {type_name.capitalize()} Components")
|
| 373 |
self._display_components_table(session_state, component_type, components)
|
| 374 |
-
# Removed the misplaced Download Walls Table button from here
|
| 375 |
|
| 376 |
def _display_add_wall_form(self, session_state: Any) -> None:
|
| 377 |
-
|
| 378 |
-
st.
|
| 379 |
-
wall_method = st.radio("Add Wall Method", ["Manual Entry", "Excel Upload"])
|
| 380 |
|
| 381 |
if wall_method == "Manual Entry":
|
| 382 |
if "add_wall_submitted" not in session_state:
|
| 383 |
session_state.add_wall_submitted = False
|
| 384 |
|
| 385 |
-
with st.form("add_wall_form"):
|
| 386 |
col1, col2 = st.columns(2)
|
| 387 |
with col1:
|
| 388 |
name = st.text_input("Name", "New Wall")
|
|
@@ -391,10 +389,13 @@ class ComponentSelectionInterface:
|
|
| 391 |
with col2:
|
| 392 |
preset_walls = self.component_library.get_preset_components_by_type(ComponentType.WALL)
|
| 393 |
wall_options = {comp.name: comp for comp in preset_walls}
|
| 394 |
-
selected_wall = st.selectbox("Select Wall Type", options=list(wall_options.keys()))
|
| 395 |
component = wall_options[selected_wall]
|
| 396 |
-
#
|
| 397 |
-
|
|
|
|
|
|
|
|
|
|
| 398 |
shading_percentage = st.slider("Shading Percentage (%)", min_value=0.0, max_value=100.0, value=0.0, step=5.0)
|
| 399 |
|
| 400 |
submitted = st.form_submit_button("Add Wall")
|
|
@@ -414,22 +415,29 @@ class ComponentSelectionInterface:
|
|
| 414 |
self.component_library.add_component(new_wall)
|
| 415 |
session_state.components['walls'].append(new_wall)
|
| 416 |
st.success(f"Added {new_wall.name}")
|
| 417 |
-
session_state.add_wall_submitted = True
|
| 418 |
st.rerun()
|
| 419 |
|
| 420 |
-
# Reset the flag after submission
|
| 421 |
if session_state.add_wall_submitted:
|
| 422 |
session_state.add_wall_submitted = False
|
| 423 |
|
| 424 |
-
elif wall_method == "
|
| 425 |
-
st.info("Upload
|
| 426 |
-
uploaded_file = st.file_uploader("Upload Walls
|
| 427 |
-
required_cols = ["Name", "Area (m²)", "U-Value (W/m²·K)", "Orientation", "Wall Type", "
|
| 428 |
-
if uploaded_file and st.button("Process
|
| 429 |
-
|
|
|
|
|
|
|
|
|
|
| 430 |
if not all(col in df.columns for col in required_cols):
|
| 431 |
-
st.error("
|
| 432 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
for _, row in df.iterrows():
|
| 434 |
try:
|
| 435 |
new_wall = Wall(
|
|
@@ -442,18 +450,17 @@ class ComponentSelectionInterface:
|
|
| 442 |
session_state.components['walls'].append(new_wall)
|
| 443 |
except ValueError as e:
|
| 444 |
st.error(f"Error in row: {row['Name']} - {str(e)}")
|
| 445 |
-
st.success("Walls uploaded successfully!")
|
| 446 |
st.rerun()
|
| 447 |
# Provide a downloadable template
|
| 448 |
template_df = pd.DataFrame(columns=required_cols)
|
| 449 |
buffer = io.BytesIO()
|
| 450 |
-
|
| 451 |
-
template_df.to_excel(writer, index=False)
|
| 452 |
st.download_button(
|
| 453 |
-
label="Download Wall Template",
|
| 454 |
data=buffer.getvalue(),
|
| 455 |
-
file_name="wall_template.
|
| 456 |
-
mime="
|
| 457 |
)
|
| 458 |
|
| 459 |
def _display_add_component_form(self, session_state: Any, component_type: ComponentType) -> None:
|
|
@@ -463,7 +470,7 @@ class ComponentSelectionInterface:
|
|
| 463 |
if f"add_{type_name}_submitted" not in session_state:
|
| 464 |
session_state[f"add_{type_name}_submitted"] = False
|
| 465 |
|
| 466 |
-
with st.form(f"add_{type_name}_form"):
|
| 467 |
col1, col2 = st.columns(2)
|
| 468 |
with col1:
|
| 469 |
name = st.text_input("Name", f"New {type_name.capitalize()}")
|
|
@@ -570,7 +577,7 @@ class ComponentSelectionInterface:
|
|
| 570 |
for comp in components:
|
| 571 |
row = {"Name": comp.name, "Area (m²)": comp.area, "U-Value (W/m²·K)": comp.u_value, "Orientation": comp.orientation.value, "ID": comp.id}
|
| 572 |
if component_type == ComponentType.WALL:
|
| 573 |
-
row.update({"Wall Type": comp.wall_type, "
|
| 574 |
elif component_type == ComponentType.ROOF:
|
| 575 |
row.update({"Roof Type": comp.roof_type, "Roof Group": comp.roof_group})
|
| 576 |
elif component_type == ComponentType.FLOOR:
|
|
@@ -587,12 +594,9 @@ class ComponentSelectionInterface:
|
|
| 587 |
st.dataframe(df[display_cols], use_container_width=True)
|
| 588 |
|
| 589 |
for i, row in df.iterrows():
|
| 590 |
-
col1, col2 = st.columns(
|
| 591 |
-
|
| 592 |
-
with
|
| 593 |
-
if st.button("Edit", key=f"edit_{row['ID']}"):
|
| 594 |
-
session_state[f"edit_{type_name}"] = row["ID"]
|
| 595 |
-
st.rerun()
|
| 596 |
with col2:
|
| 597 |
if st.button("Delete", key=f"delete_{row['ID']}"):
|
| 598 |
if not row["ID"].startswith("preset_"):
|
|
@@ -604,7 +608,7 @@ class ComponentSelectionInterface:
|
|
| 604 |
st.warning("Cannot delete preset components.")
|
| 605 |
|
| 606 |
if component_type == ComponentType.WALL:
|
| 607 |
-
csv = df[display_cols].to_csv(index=False)
|
| 608 |
st.download_button(label="Download Walls Table", data=csv, file_name="walls_table.csv", mime="text/csv")
|
| 609 |
|
| 610 |
if f"edit_{type_name}" in session_state and component_type != ComponentType.WALL:
|
|
|
|
| 371 |
if components:
|
| 372 |
st.subheader(f"Existing {type_name.capitalize()} Components")
|
| 373 |
self._display_components_table(session_state, component_type, components)
|
|
|
|
| 374 |
|
| 375 |
def _display_add_wall_form(self, session_state: Any) -> None:
|
| 376 |
+
st.write("Add walls manually or upload a file.")
|
| 377 |
+
wall_method = st.radio("Add Wall Method", ["Manual Entry", "File Upload"])
|
|
|
|
| 378 |
|
| 379 |
if wall_method == "Manual Entry":
|
| 380 |
if "add_wall_submitted" not in session_state:
|
| 381 |
session_state.add_wall_submitted = False
|
| 382 |
|
| 383 |
+
with st.form("add_wall_form", clear_on_submit=True): # Clear form on submit
|
| 384 |
col1, col2 = st.columns(2)
|
| 385 |
with col1:
|
| 386 |
name = st.text_input("Name", "New Wall")
|
|
|
|
| 389 |
with col2:
|
| 390 |
preset_walls = self.component_library.get_preset_components_by_type(ComponentType.WALL)
|
| 391 |
wall_options = {comp.name: comp for comp in preset_walls}
|
| 392 |
+
selected_wall = st.selectbox("Select Wall Type", options=list(wall_options.keys()), key="wall_type_select")
|
| 393 |
component = wall_options[selected_wall]
|
| 394 |
+
# Update U-value immediately when wall type changes
|
| 395 |
+
if "selected_wall" not in session_state or session_state.selected_wall != selected_wall:
|
| 396 |
+
session_state.selected_wall = selected_wall
|
| 397 |
+
session_state.u_value = component.u_value
|
| 398 |
+
u_value = st.number_input("U-Value (W/m²·K)", min_value=0.0, max_value=9.0, value=session_state.get("u_value", component.u_value), step=0.01)
|
| 399 |
shading_percentage = st.slider("Shading Percentage (%)", min_value=0.0, max_value=100.0, value=0.0, step=5.0)
|
| 400 |
|
| 401 |
submitted = st.form_submit_button("Add Wall")
|
|
|
|
| 415 |
self.component_library.add_component(new_wall)
|
| 416 |
session_state.components['walls'].append(new_wall)
|
| 417 |
st.success(f"Added {new_wall.name}")
|
| 418 |
+
session_state.add_wall_submitted = True
|
| 419 |
st.rerun()
|
| 420 |
|
|
|
|
| 421 |
if session_state.add_wall_submitted:
|
| 422 |
session_state.add_wall_submitted = False
|
| 423 |
|
| 424 |
+
elif wall_method == "File Upload":
|
| 425 |
+
st.info("Upload a CSV or Excel file with columns: Name, Area (m²), U-Value (W/m²·K), Orientation, Wall Type, Group, Shading (%)")
|
| 426 |
+
uploaded_file = st.file_uploader("Upload Walls File", type=["csv", "xlsx"], key="wall_upload")
|
| 427 |
+
required_cols = ["Name", "Area (m²)", "U-Value (W/m²·K)", "Orientation", "Wall Type", "Group", "Shading (%)"]
|
| 428 |
+
if uploaded_file and st.button("Process File"):
|
| 429 |
+
if uploaded_file.name.endswith('.csv'):
|
| 430 |
+
df = pd.read_csv(uploaded_file)
|
| 431 |
+
else: # .xlsx
|
| 432 |
+
df = pd.read_excel(uploaded_file)
|
| 433 |
if not all(col in df.columns for col in required_cols):
|
| 434 |
+
st.error("File must contain all required columns!")
|
| 435 |
else:
|
| 436 |
+
# Overwrite existing walls
|
| 437 |
+
session_state.components['walls'] = []
|
| 438 |
+
for comp_id in list(self.component_library.components.keys()):
|
| 439 |
+
if not comp_id.startswith("preset_") and self.component_library.components[comp_id].component_type == ComponentType.WALL:
|
| 440 |
+
self.component_library.remove_component(comp_id)
|
| 441 |
for _, row in df.iterrows():
|
| 442 |
try:
|
| 443 |
new_wall = Wall(
|
|
|
|
| 450 |
session_state.components['walls'].append(new_wall)
|
| 451 |
except ValueError as e:
|
| 452 |
st.error(f"Error in row: {row['Name']} - {str(e)}")
|
| 453 |
+
st.success("Walls uploaded and overwritten successfully!")
|
| 454 |
st.rerun()
|
| 455 |
# Provide a downloadable template
|
| 456 |
template_df = pd.DataFrame(columns=required_cols)
|
| 457 |
buffer = io.BytesIO()
|
| 458 |
+
template_df.to_csv(buffer, index=False)
|
|
|
|
| 459 |
st.download_button(
|
| 460 |
+
label="Download Wall Template (CSV)",
|
| 461 |
data=buffer.getvalue(),
|
| 462 |
+
file_name="wall_template.csv",
|
| 463 |
+
mime="text/csv"
|
| 464 |
)
|
| 465 |
|
| 466 |
def _display_add_component_form(self, session_state: Any, component_type: ComponentType) -> None:
|
|
|
|
| 470 |
if f"add_{type_name}_submitted" not in session_state:
|
| 471 |
session_state[f"add_{type_name}_submitted"] = False
|
| 472 |
|
| 473 |
+
with st.form(f"add_{type_name}_form", clear_on_submit=True): # Clear form on submit
|
| 474 |
col1, col2 = st.columns(2)
|
| 475 |
with col1:
|
| 476 |
name = st.text_input("Name", f"New {type_name.capitalize()}")
|
|
|
|
| 577 |
for comp in components:
|
| 578 |
row = {"Name": comp.name, "Area (m²)": comp.area, "U-Value (W/m²·K)": comp.u_value, "Orientation": comp.orientation.value, "ID": comp.id}
|
| 579 |
if component_type == ComponentType.WALL:
|
| 580 |
+
row.update({"Wall Type": comp.wall_type, "Group": comp.wall_group, "Shading (%)": comp.shading_percentage})
|
| 581 |
elif component_type == ComponentType.ROOF:
|
| 582 |
row.update({"Roof Type": comp.roof_type, "Roof Group": comp.roof_group})
|
| 583 |
elif component_type == ComponentType.FLOOR:
|
|
|
|
| 594 |
st.dataframe(df[display_cols], use_container_width=True)
|
| 595 |
|
| 596 |
for i, row in df.iterrows():
|
| 597 |
+
col1, col2 = st.columns([5, 1]) # Adjusted column widths for better alignment
|
| 598 |
+
with col1:
|
| 599 |
+
st.write("") # Placeholder to align with table row
|
|
|
|
|
|
|
|
|
|
| 600 |
with col2:
|
| 601 |
if st.button("Delete", key=f"delete_{row['ID']}"):
|
| 602 |
if not row["ID"].startswith("preset_"):
|
|
|
|
| 608 |
st.warning("Cannot delete preset components.")
|
| 609 |
|
| 610 |
if component_type == ComponentType.WALL:
|
| 611 |
+
csv = df[display_cols].to_csv(index=False, encoding='utf-8')
|
| 612 |
st.download_button(label="Download Walls Table", data=csv, file_name="walls_table.csv", mime="text/csv")
|
| 613 |
|
| 614 |
if f"edit_{type_name}" in session_state and component_type != ComponentType.WALL:
|