mabuseif commited on
Commit
d05f44e
·
verified ·
1 Parent(s): cf951ab

Update app/component_selection.py

Browse files
Files changed (1) hide show
  1. app/component_selection.py +52 -25
app/component_selection.py CHANGED
@@ -77,8 +77,8 @@ class BuildingComponent:
77
  @dataclass
78
  class Wall(BuildingComponent):
79
  wall_type: str = "Brick"
80
- wall_group: str = "Wall" # Changed from B/C to Wall for visualization
81
- shading_percentage: float = 0.0 # New attribute for shading percentage
82
 
83
  def __post_init__(self):
84
  super().__post_init__()
@@ -380,7 +380,7 @@ class ComponentSelectionInterface:
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")
@@ -392,10 +392,10 @@ class ComponentSelectionInterface:
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")
@@ -430,8 +430,10 @@ class ComponentSelectionInterface:
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'] = []
@@ -441,10 +443,10 @@ class ComponentSelectionInterface:
441
  for _, row in df.iterrows():
442
  try:
443
  new_wall = Wall(
444
- id=str(uuid.uuid4()), name=row["Name"], u_value=row["U-Value (W/m²·K)"],
445
- area=row["Area (m²)"], orientation=Orientation(row["Orientation"]),
446
  wall_type=row["Wall Type"], wall_group="Wall",
447
- shading_percentage=row["Shading (%)"]
448
  )
449
  self.component_library.add_component(new_wall)
450
  session_state.components['walls'].append(new_wall)
@@ -455,7 +457,7 @@ class ComponentSelectionInterface:
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(),
@@ -470,7 +472,7 @@ class ComponentSelectionInterface:
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()}")
@@ -591,25 +593,50 @@ class ComponentSelectionInterface:
591
 
592
  df = pd.DataFrame(data)
593
  display_cols = [col for col in df.columns if col != "ID"]
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_"):
603
  self.component_library.remove_component(row["ID"])
604
- session_state.components[type_name + 's'] = [c for c in components if c.id != row["ID"]]
605
  st.success(f"Deleted {row['Name']}")
606
  st.rerun()
607
  else:
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:
615
  self._display_edit_component_form(session_state, component_type, session_state[f"edit_{type_name}"])
 
77
  @dataclass
78
  class Wall(BuildingComponent):
79
  wall_type: str = "Brick"
80
+ wall_group: str = "Wall"
81
+ shading_percentage: float = 0.0
82
 
83
  def __post_init__(self):
84
  super().__post_init__()
 
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):
384
  col1, col2 = st.columns(2)
385
  with col1:
386
  name = st.text_input("Name", "New Wall")
 
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 "prev_selected_wall" not in session_state or session_state.prev_selected_wall != selected_wall:
396
+ session_state.prev_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=float(session_state.u_value), step=0.01, key=f"u_value_{selected_wall}")
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")
 
430
  df = pd.read_csv(uploaded_file)
431
  else: # .xlsx
432
  df = pd.read_excel(uploaded_file)
433
+ # Normalize column names for comparison
434
+ df.columns = [col.strip() for col in df.columns]
435
+ if not all(col in [c.strip() for c in df.columns] for col in required_cols):
436
+ st.error(f"File must contain all required columns: {', '.join(required_cols)}")
437
  else:
438
  # Overwrite existing walls
439
  session_state.components['walls'] = []
 
443
  for _, row in df.iterrows():
444
  try:
445
  new_wall = Wall(
446
+ id=str(uuid.uuid4()), name=row["Name"], u_value=float(row["U-Value (W/m²·K)"]),
447
+ area=float(row["Area (m²)"]), orientation=Orientation(row["Orientation"]),
448
  wall_type=row["Wall Type"], wall_group="Wall",
449
+ shading_percentage=float(row["Shading (%)"])
450
  )
451
  self.component_library.add_component(new_wall)
452
  session_state.components['walls'].append(new_wall)
 
457
  # Provide a downloadable template
458
  template_df = pd.DataFrame(columns=required_cols)
459
  buffer = io.BytesIO()
460
+ template_df.to_csv(buffer, index=False, encoding='utf-8')
461
  st.download_button(
462
  label="Download Wall Template (CSV)",
463
  data=buffer.getvalue(),
 
472
  if f"add_{type_name}_submitted" not in session_state:
473
  session_state[f"add_{type_name}_submitted"] = False
474
 
475
+ with st.form(f"add_{type_name}_form", clear_on_submit=True):
476
  col1, col2 = st.columns(2)
477
  with col1:
478
  name = st.text_input("Name", f"New {type_name.capitalize()}")
 
593
 
594
  df = pd.DataFrame(data)
595
  display_cols = [col for col in df.columns if col != "ID"]
 
596
 
597
+ if component_type == ComponentType.WALL:
598
+ st.write("Walls Table:")
599
+ headers = display_cols
600
+ st.table([headers]) # Display headers
601
+ for i, row in df.iterrows():
602
+ cols = st.columns([1, 1, 1, 1, 1, 1, 1, 1]) # 7 columns + 1 for delete
603
+ cols[0].write(row["Name"])
604
+ cols[1].write(row["Area (m²)"])
605
+ cols[2].write(row["U-Value (W/m²·K)"])
606
+ cols[3].write(row["Orientation"])
607
+ cols[4].write(row["Wall Type"])
608
+ cols[5].write(row["Group"])
609
+ cols[6].write(row["Shading (%)"])
610
+ if cols[7].button("Delete", key=f"delete_{row['ID']}"):
611
  if not row["ID"].startswith("preset_"):
612
  self.component_library.remove_component(row["ID"])
613
+ session_state.components['walls'] = [c for c in components if c.id != row["ID"]]
614
  st.success(f"Deleted {row['Name']}")
615
  st.rerun()
616
  else:
617
  st.warning("Cannot delete preset components.")
618
+ # Download walls table as CSV with correct headings
619
+ csv_df = df[display_cols]
620
+ csv_buffer = io.BytesIO()
621
+ csv_df.to_csv(csv_buffer, index=False, encoding='utf-8')
622
+ st.download_button(label="Download Walls Table", data=csv_buffer.getvalue(), file_name="walls_table.csv", mime="text/csv")
623
+ else:
624
+ st.dataframe(df[display_cols], use_container_width=True)
625
+ for i, row in df.iterrows():
626
+ col1, col2 = st.columns(2)
627
+ with col1:
628
+ if st.button("Edit", key=f"edit_{row['ID']}"):
629
+ session_state[f"edit_{type_name}"] = row["ID"]
630
+ st.rerun()
631
+ with col2:
632
+ if st.button("Delete", key=f"delete_{row['ID']}"):
633
+ if not row["ID"].startswith("preset_"):
634
+ self.component_library.remove_component(row["ID"])
635
+ session_state.components[type_name + 's'] = [c for c in components if c.id != row["ID"]]
636
+ st.success(f"Deleted {row['Name']}")
637
+ st.rerun()
638
+ else:
639
+ st.warning("Cannot delete preset components.")
640
 
641
  if f"edit_{type_name}" in session_state and component_type != ComponentType.WALL:
642
  self._display_edit_component_form(session_state, component_type, session_state[f"edit_{type_name}"])