mabuseif commited on
Commit
585a6b6
·
verified ·
1 Parent(s): 7ab75fc

Update app/materials_library.py

Browse files
Files changed (1) hide show
  1. app/materials_library.py +45 -26
app/materials_library.py CHANGED
@@ -56,7 +56,7 @@ DEFAULT_MATERIALS = {
56
  "density": 1920.0,
57
  "specific_heat": 840.0,
58
  "default_thickness": 0.1,
59
- "embodied_carbon": 240.0,
60
  "cost": 180.0,
61
  "absorptivity": 0.65,
62
  "emissivity": 0.90,
@@ -350,6 +350,10 @@ def display_materials_page():
350
  Display the material library page.
351
  This is the main function called by main.py when the Material Library page is selected.
352
  """
 
 
 
 
353
  st.title("Material Library")
354
 
355
  # Display help information in an expandable section
@@ -373,12 +377,17 @@ def display_materials_page():
373
  with col1:
374
  if st.button("Back to Climate Data", key="back_to_climate"):
375
  st.session_state.current_page = "Climate Data"
376
- st.rerun()
377
 
378
  with col2:
379
  if st.button("Continue to Construction", key="continue_to_construction"):
380
  st.session_state.current_page = "Construction"
381
- st.rerun()
 
 
 
 
 
382
 
383
  def display_materials_tab():
384
  """Display the materials tab content."""
@@ -419,7 +428,8 @@ def display_materials_tab():
419
  cols[0].write(name)
420
  cols[1].write(thermal_mass_category)
421
  cols[2].write(f"{u_value:.3f}")
422
- if cols[3].button("Preview", key=f"preview_lib_mat_{name}"):
 
423
  st.session_state.material_editor = {
424
  "name": name,
425
  "category": material["category"],
@@ -438,7 +448,8 @@ def display_materials_tab():
438
  }
439
  st.session_state.active_tab = "Materials"
440
  st.success(f"Previewing material '{name}'")
441
- if cols[4].button("Copy", key=f"copy_lib_mat_{name}"):
 
442
  new_name = f"{name}_Project"
443
  counter = 1
444
  while new_name in st.session_state.project_data["materials"]["project"] or new_name in library_materials:
@@ -447,7 +458,7 @@ def display_materials_tab():
447
  st.session_state.project_data["materials"]["project"][new_name] = material.copy()
448
  st.success(f"Material '{new_name}' copied to project.")
449
  logger.info(f"Copied library material '{name}' as '{new_name}' to project")
450
- st.rerun()
451
  else:
452
  st.info("No materials found in the selected category.")
453
 
@@ -479,7 +490,8 @@ def display_materials_tab():
479
  cols[0].write(name)
480
  cols[1].write(thermal_mass_category)
481
  cols[2].write(f"{u_value:.3f}")
482
- if cols[3].button("Edit", key=f"edit_proj_mat_{name}"):
 
483
  st.session_state.material_editor = {
484
  "name": name,
485
  "category": material["category"],
@@ -498,7 +510,8 @@ def display_materials_tab():
498
  }
499
  st.session_state.active_tab = "Materials"
500
  st.success(f"Editing material '{name}'")
501
- if cols[4].button("Delete", key=f"delete_proj_mat_{name}"):
 
502
  is_in_use = check_material_in_use(name)
503
  if is_in_use:
504
  st.error(f"Cannot delete material '{name}' because it is in use in constructions.")
@@ -506,7 +519,7 @@ def display_materials_tab():
506
  del st.session_state.project_data["materials"]["project"][name]
507
  st.success(f"Material '{name}' deleted from project.")
508
  logger.info(f"Deleted material '{name}' from project")
509
- st.rerun()
510
  else:
511
  st.info("No project materials in the selected category.")
512
 
@@ -577,11 +590,14 @@ def display_fenestrations_tab():
577
 
578
  for fenestration in filtered_fenestrations:
579
  cols = st.columns([2, 1, 1, 1, 1])
580
- name = [k for k, v in library_fenestrations.items() if v == fenestration][0]
 
 
581
  cols[0].write(name)
582
  cols[1].write(fenestration["type"])
583
  cols[2].write(f"{fenestration['u_value']:.2f}")
584
- if cols[3].button("Preview", key=f"preview_lib_fen_{name}"):
 
585
  st.session_state.fenestration_editor = {
586
  "name": name,
587
  "type": fenestration["type"],
@@ -597,7 +613,8 @@ def display_fenestrations_tab():
597
  }
598
  st.session_state.active_tab = "Fenestrations"
599
  st.success(f"Previewing fenestration '{name}'")
600
- if cols[4].button("Copy", key=f"copy_lib_fen_{name}"):
 
601
  new_name = f"{name}_Project"
602
  counter = 1
603
  while new_name in st.session_state.project_data["fenestrations"]["project"] or new_name in library_fenestrations:
@@ -606,7 +623,7 @@ def display_fenestrations_tab():
606
  st.session_state.project_data["fenestrations"]["project"][new_name] = fenestration.copy()
607
  st.success(f"Fenestration '{new_name}' copied to project.")
608
  logger.info(f"Copied library fenestration '{name}' as '{new_name}' to project")
609
- st.rerun()
610
  else:
611
  st.info("No fenestrations found in the selected type.")
612
 
@@ -635,7 +652,8 @@ def display_fenestrations_tab():
635
  cols[0].write(name)
636
  cols[1].write(fenestration["type"])
637
  cols[2].write(f"{fenestration['u_value']:.2f}")
638
- if cols[3].button("Edit", key=f"edit_proj_fen_{name}"):
 
639
  st.session_state.fenestration_editor = {
640
  "name": name,
641
  "type": fenestration["type"],
@@ -651,7 +669,8 @@ def display_fenestrations_tab():
651
  }
652
  st.session_state.active_tab = "Fenestrations"
653
  st.success(f"Editing fenestration '{name}'")
654
- if cols[4].button("Delete", key=f"delete_proj_fen_{name}"):
 
655
  is_in_use = check_fenestration_in_use(name)
656
  if is_in_use:
657
  st.error(f"Cannot delete fenestration '{name}' because it is in use in components.")
@@ -659,7 +678,7 @@ def display_fenestrations_tab():
659
  del st.session_state.project_data["fenestrations"]["project"][name]
660
  st.success(f"Fenestration '{name}' deleted from project.")
661
  logger.info(f"Deleted fenestration '{name}' from project")
662
- st.rerun()
663
  else:
664
  st.info("No project fenestrations in the selected type.")
665
 
@@ -894,7 +913,7 @@ def display_material_editor():
894
 
895
  with col2:
896
  clear_button = st.form_submit_button("Clear Form")
897
-
898
  # Handle form submission
899
  if submit_button and not is_library:
900
  # Validate inputs
@@ -936,14 +955,14 @@ def display_material_editor():
936
  st.success(f"Material '{name}' added to your project.")
937
  logger.info(f"Added new material '{name}' to project")
938
 
939
- # Reset editor
940
  reset_material_editor()
941
- st.rerun()
942
-
943
  # Handle clear button
944
  if clear_button:
945
  reset_material_editor()
946
- st.rerun()
947
 
948
  def display_fenestration_editor():
949
  """Display the fenestration editor form."""
@@ -1053,7 +1072,7 @@ def display_fenestration_editor():
1053
 
1054
  with col2:
1055
  clear_button = st.form_submit_button("Clear Form")
1056
-
1057
  # Handle form submission
1058
  if submit_button and not is_library:
1059
  # Validate inputs
@@ -1090,14 +1109,14 @@ def display_fenestration_editor():
1090
  st.success(f"Fenestration '{name}' added to your project.")
1091
  logger.info(f"Added new fenestration '{name}' to project")
1092
 
1093
- # Reset editor
1094
  reset_fenestration_editor()
1095
- st.rerun()
1096
-
1097
  # Handle clear button
1098
  if clear_button:
1099
  reset_fenestration_editor()
1100
- st.rerun()
1101
 
1102
  def validate_material(
1103
  name: str, category: str, thermal_conductivity: float, density: float,
 
56
  "density": 1920.0,
57
  "specific_heat": 840.0,
58
  "default_thickness": 0.1,
59
+ "embodied_carbon": 240,
60
  "cost": 180.0,
61
  "absorptivity": 0.65,
62
  "emissivity": 0.90,
 
350
  Display the material library page.
351
  This is the main function called by main.py when the Material Library page is selected.
352
  """
353
+ # Initialize rerun flag
354
+ if "needs_rerun" not in st.session_state:
355
+ st.session_state.needs_rerun = False
356
+
357
  st.title("Material Library")
358
 
359
  # Display help information in an expandable section
 
377
  with col1:
378
  if st.button("Back to Climate Data", key="back_to_climate"):
379
  st.session_state.current_page = "Climate Data"
380
+ st.session_state.needs_rerun = True
381
 
382
  with col2:
383
  if st.button("Continue to Construction", key="continue_to_construction"):
384
  st.session_state.current_page = "Construction"
385
+ st.session_state.needs_rerun = True
386
+
387
+ # Trigger rerun if needed
388
+ if st.session_state.needs_rerun:
389
+ st.session_state.needs_rerun = False
390
+ st.rerun()
391
 
392
  def display_materials_tab():
393
  """Display the materials tab content."""
 
428
  cols[0].write(name)
429
  cols[1].write(thermal_mass_category)
430
  cols[2].write(f"{u_value:.3f}")
431
+ unique_id = str(uuid.uuid4())
432
+ if cols[3].button("Preview", key=f"preview_lib_mat_{name}_{unique_id}"):
433
  st.session_state.material_editor = {
434
  "name": name,
435
  "category": material["category"],
 
448
  }
449
  st.session_state.active_tab = "Materials"
450
  st.success(f"Previewing material '{name}'")
451
+ unique_id = str(uuid.uuid4())
452
+ if cols[4].button("Copy", key=f"copy_lib_mat_{name}_{unique_id}"):
453
  new_name = f"{name}_Project"
454
  counter = 1
455
  while new_name in st.session_state.project_data["materials"]["project"] or new_name in library_materials:
 
458
  st.session_state.project_data["materials"]["project"][new_name] = material.copy()
459
  st.success(f"Material '{new_name}' copied to project.")
460
  logger.info(f"Copied library material '{name}' as '{new_name}' to project")
461
+ st.session_state.needs_rerun = True
462
  else:
463
  st.info("No materials found in the selected category.")
464
 
 
490
  cols[0].write(name)
491
  cols[1].write(thermal_mass_category)
492
  cols[2].write(f"{u_value:.3f}")
493
+ unique_id = str(uuid.uuid4())
494
+ if cols[3].button("Edit", key=f"edit_proj_mat_{name}_{unique_id}"):
495
  st.session_state.material_editor = {
496
  "name": name,
497
  "category": material["category"],
 
510
  }
511
  st.session_state.active_tab = "Materials"
512
  st.success(f"Editing material '{name}'")
513
+ unique_id = str(uuid.uuid4())
514
+ if cols[4].button("Delete", key=f"delete_proj_mat_{name}_{unique_id}"):
515
  is_in_use = check_material_in_use(name)
516
  if is_in_use:
517
  st.error(f"Cannot delete material '{name}' because it is in use in constructions.")
 
519
  del st.session_state.project_data["materials"]["project"][name]
520
  st.success(f"Material '{name}' deleted from project.")
521
  logger.info(f"Deleted material '{name}' from project")
522
+ st.session_state.needs_rerun = True
523
  else:
524
  st.info("No project materials in the selected category.")
525
 
 
590
 
591
  for fenestration in filtered_fenestrations:
592
  cols = st.columns([2, 1, 1, 1, 1])
593
+ name = [k for k
594
+
595
+ , v in library_fenestrations.items() if v == fenestration][0]
596
  cols[0].write(name)
597
  cols[1].write(fenestration["type"])
598
  cols[2].write(f"{fenestration['u_value']:.2f}")
599
+ unique_id = str(uuid.uuid4())
600
+ if cols[3].button("Preview", key=f"preview_lib_fen_{name}_{unique_id}"):
601
  st.session_state.fenestration_editor = {
602
  "name": name,
603
  "type": fenestration["type"],
 
613
  }
614
  st.session_state.active_tab = "Fenestrations"
615
  st.success(f"Previewing fenestration '{name}'")
616
+ unique_id = str(uuid.uuid4())
617
+ if cols[4].button("Copy", key=f"copy_lib_fen_{name}_{unique_id}"):
618
  new_name = f"{name}_Project"
619
  counter = 1
620
  while new_name in st.session_state.project_data["fenestrations"]["project"] or new_name in library_fenestrations:
 
623
  st.session_state.project_data["fenestrations"]["project"][new_name] = fenestration.copy()
624
  st.success(f"Fenestration '{new_name}' copied to project.")
625
  logger.info(f"Copied library fenestration '{name}' as '{new_name}' to project")
626
+ st.session_state.needs_rerun = True
627
  else:
628
  st.info("No fenestrations found in the selected type.")
629
 
 
652
  cols[0].write(name)
653
  cols[1].write(fenestration["type"])
654
  cols[2].write(f"{fenestration['u_value']:.2f}")
655
+ unique_id = str(uuid.uuid4())
656
+ if cols[3].button("Edit", key=f"edit_proj_fen_{name}_{unique_id}"):
657
  st.session_state.fenestration_editor = {
658
  "name": name,
659
  "type": fenestration["type"],
 
669
  }
670
  st.session_state.active_tab = "Fenestrations"
671
  st.success(f"Editing fenestration '{name}'")
672
+ unique_id = str(uuid.uuid4())
673
+ if cols[4].button("Delete", key=f"delete_proj_fen_{name}_{unique_id}"):
674
  is_in_use = check_fenestration_in_use(name)
675
  if is_in_use:
676
  st.error(f"Cannot delete fenestration '{name}' because it is in use in components.")
 
678
  del st.session_state.project_data["fenestrations"]["project"][name]
679
  st.success(f"Fenestration '{name}' deleted from project.")
680
  logger.info(f"Deleted fenestration '{name}' from project")
681
+ st.session_state.needs_rerun = True
682
  else:
683
  st.info("No project fenestrations in the selected type.")
684
 
 
913
 
914
  with col2:
915
  clear_button = st.form_submit_button("Clear Form")
916
+
917
  # Handle form submission
918
  if submit_button and not is_library:
919
  # Validate inputs
 
955
  st.success(f"Material '{name}' added to your project.")
956
  logger.info(f"Added new material '{name}' to project")
957
 
958
+ # Reset editor and flag for rerun
959
  reset_material_editor()
960
+ st.session_state.needs_rerun = True
961
+
962
  # Handle clear button
963
  if clear_button:
964
  reset_material_editor()
965
+ st.session_state.needs_rerun = True
966
 
967
  def display_fenestration_editor():
968
  """Display the fenestration editor form."""
 
1072
 
1073
  with col2:
1074
  clear_button = st.form_submit_button("Clear Form")
1075
+
1076
  # Handle form submission
1077
  if submit_button and not is_library:
1078
  # Validate inputs
 
1109
  st.success(f"Fenestration '{name}' added to your project.")
1110
  logger.info(f"Added new fenestration '{name}' to project")
1111
 
1112
+ # Reset editor and flag for rerun
1113
  reset_fenestration_editor()
1114
+ st.session_state.needs_rerun = True
1115
+
1116
  # Handle clear button
1117
  if clear_button:
1118
  reset_fenestration_editor()
1119
+ st.session_state.needs_rerun = True
1120
 
1121
  def validate_material(
1122
  name: str, category: str, thermal_conductivity: float, density: float,