Spaces:
Sleeping
Sleeping
Update app/construction.py
Browse files- app/construction.py +38 -13
app/construction.py
CHANGED
@@ -189,19 +189,22 @@ def initialize_construction():
|
|
189 |
}
|
190 |
if "components" not in st.session_state.project_data:
|
191 |
st.session_state.project_data["components"] = {}
|
|
|
|
|
192 |
|
193 |
def display_constructions_tables(construction_library: ConstructionLibrary):
|
194 |
"""Display library and project constructions tables."""
|
195 |
st.subheader("Library Constructions")
|
196 |
with st.container():
|
197 |
library_constructions = list(construction_library.library_constructions.items())
|
198 |
-
cols = st.columns([2, 1, 1, 1])
|
199 |
cols[0].write("**Name**")
|
200 |
cols[1].write("**Thermal Mass Category**")
|
201 |
cols[2].write("**U-Value (W/m虏路K)**")
|
202 |
cols[3].write("**Copy**")
|
|
|
203 |
for name, construction in library_constructions:
|
204 |
-
cols = st.columns([2, 1, 1, 1])
|
205 |
cols[0].write(name)
|
206 |
cols[1].write(construction.get("thermal_mass_category", "Low"))
|
207 |
cols[2].write(f"{construction.get('u_value', 0.0):.3f}")
|
@@ -222,6 +225,10 @@ def display_constructions_tables(construction_library: ConstructionLibrary):
|
|
222 |
if material_name in st.session_state.project_data["materials"]["library"]:
|
223 |
# Copy library material to project
|
224 |
library_material = st.session_state.project_data["materials"]["library"][material_name]
|
|
|
|
|
|
|
|
|
225 |
new_mat_name = f"{material_name}_Project"
|
226 |
counter = 1
|
227 |
while new_mat_name in st.session_state.project_data["materials"]["project"] or new_mat_name in st.session_state.project_data["materials"]["library"]:
|
@@ -234,12 +241,12 @@ def display_constructions_tables(construction_library: ConstructionLibrary):
|
|
234 |
new_material = Material(
|
235 |
name=new_mat_name,
|
236 |
category=MaterialCategory[category_str],
|
237 |
-
conductivity=library_material["
|
238 |
-
density=library_material["density"],
|
239 |
-
specific_heat=library_material["specific_heat"],
|
240 |
default_thickness=library_material["thickness_range"]["default"],
|
241 |
embodied_carbon=library_material["embodied_carbon"],
|
242 |
-
solar_absorption=library_material.get("
|
243 |
price=library_material["cost"]["material"],
|
244 |
emissivity=library_material.get("emissivity", 0.9),
|
245 |
is_library=False
|
@@ -269,6 +276,20 @@ def display_constructions_tables(construction_library: ConstructionLibrary):
|
|
269 |
st.session_state.construction_rerun_pending = True
|
270 |
else:
|
271 |
st.error(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
|
273 |
st.subheader("Project Constructions")
|
274 |
with st.container():
|
@@ -314,7 +335,7 @@ def display_constructions_tables(construction_library: ConstructionLibrary):
|
|
314 |
|
315 |
def display_construction_editor(construction_library: ConstructionLibrary):
|
316 |
"""Display the construction editor form."""
|
317 |
-
is_preview = st.session_state.get("rerun_trigger", "").startswith("preview_cons_")
|
318 |
materials = get_available_materials()
|
319 |
material_objects = {name: m for name, m in materials.items() if isinstance(m, Material)}
|
320 |
if is_preview:
|
@@ -331,11 +352,15 @@ def display_construction_editor(construction_library: ConstructionLibrary):
|
|
331 |
with st.container():
|
332 |
with st.form("construction_editor_form", clear_on_submit=False):
|
333 |
editor_state = st.session_state.get("construction_editor", {})
|
334 |
-
form_state = st.session_state.get("construction_form_state", {
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
|
|
|
|
|
|
|
|
339 |
is_edit = editor_state.get("is_edit", False)
|
340 |
original_name = editor_state.get("original_name", "")
|
341 |
name = st.text_input(
|
@@ -470,7 +495,7 @@ def display_construction_editor(construction_library: ConstructionLibrary):
|
|
470 |
"layers": [{"material_name": default_material, "thickness": 0.1}]
|
471 |
}
|
472 |
st.session_state.construction_action = {"action": None, "id": None}
|
473 |
-
st.session_state.rerun_trigger =
|
474 |
st.session_state.construction_rerun_pending = True
|
475 |
else:
|
476 |
st.error(f"Failed to save construction: {message}")
|
|
|
189 |
}
|
190 |
if "components" not in st.session_state.project_data:
|
191 |
st.session_state.project_data["components"] = {}
|
192 |
+
if "rerun_trigger" not in st.session_state:
|
193 |
+
st.session_state.rerun_trigger = ""
|
194 |
|
195 |
def display_constructions_tables(construction_library: ConstructionLibrary):
|
196 |
"""Display library and project constructions tables."""
|
197 |
st.subheader("Library Constructions")
|
198 |
with st.container():
|
199 |
library_constructions = list(construction_library.library_constructions.items())
|
200 |
+
cols = st.columns([2, 1, 1, 1, 1])
|
201 |
cols[0].write("**Name**")
|
202 |
cols[1].write("**Thermal Mass Category**")
|
203 |
cols[2].write("**U-Value (W/m虏路K)**")
|
204 |
cols[3].write("**Copy**")
|
205 |
+
cols[4].write("**Preview**")
|
206 |
for name, construction in library_constructions:
|
207 |
+
cols = st.columns([2, 1, 1, 1, 1])
|
208 |
cols[0].write(name)
|
209 |
cols[1].write(construction.get("thermal_mass_category", "Low"))
|
210 |
cols[2].write(f"{construction.get('u_value', 0.0):.3f}")
|
|
|
225 |
if material_name in st.session_state.project_data["materials"]["library"]:
|
226 |
# Copy library material to project
|
227 |
library_material = st.session_state.project_data["materials"]["library"][material_name]
|
228 |
+
if "thermal_properties" not in library_material:
|
229 |
+
st.error(f"Material '{material_name}' is missing thermal properties")
|
230 |
+
logger.error(f"Missing thermal properties for material: {material_name}")
|
231 |
+
break
|
232 |
new_mat_name = f"{material_name}_Project"
|
233 |
counter = 1
|
234 |
while new_mat_name in st.session_state.project_data["materials"]["project"] or new_mat_name in st.session_state.project_data["materials"]["library"]:
|
|
|
241 |
new_material = Material(
|
242 |
name=new_mat_name,
|
243 |
category=MaterialCategory[category_str],
|
244 |
+
conductivity=library_material["thermal_properties"]["conductivity"],
|
245 |
+
density=library_material["thermal_properties"]["density"],
|
246 |
+
specific_heat=library_material["thermal_properties"]["specific_heat"],
|
247 |
default_thickness=library_material["thickness_range"]["default"],
|
248 |
embodied_carbon=library_material["embodied_carbon"],
|
249 |
+
solar_absorption=library_material.get("absorptivity", 0.6),
|
250 |
price=library_material["cost"]["material"],
|
251 |
emissivity=library_material.get("emissivity", 0.9),
|
252 |
is_library=False
|
|
|
276 |
st.session_state.construction_rerun_pending = True
|
277 |
else:
|
278 |
st.error(message)
|
279 |
+
if cols[4].button("Preview", key=f"preview_lib_cons_{name}"):
|
280 |
+
if st.session_state.get("rerun_trigger") != f"preview_cons_{name}":
|
281 |
+
st.session_state.rerun_trigger = f"preview_cons_{name}"
|
282 |
+
st.session_state.construction_editor = {
|
283 |
+
"name": name,
|
284 |
+
"layers": [{"material_name": layer["material"], "thickness": layer["thickness"]} for layer in construction["layers"]],
|
285 |
+
"is_edit": False
|
286 |
+
}
|
287 |
+
st.session_state.construction_form_state = {
|
288 |
+
"name": name,
|
289 |
+
"num_layers": len(construction["layers"]),
|
290 |
+
"layers": [{"material_name": layer["material"], "thickness": layer["thickness"]} for layer in construction["layers"]]
|
291 |
+
}
|
292 |
+
st.session_state.construction_rerun_pending = True
|
293 |
|
294 |
st.subheader("Project Constructions")
|
295 |
with st.container():
|
|
|
335 |
|
336 |
def display_construction_editor(construction_library: ConstructionLibrary):
|
337 |
"""Display the construction editor form."""
|
338 |
+
is_preview = st.session_state.get("rerun_trigger", "") and st.session_state.get("rerun_trigger", "").startswith("preview_cons_")
|
339 |
materials = get_available_materials()
|
340 |
material_objects = {name: m for name, m in materials.items() if isinstance(m, Material)}
|
341 |
if is_preview:
|
|
|
352 |
with st.container():
|
353 |
with st.form("construction_editor_form", clear_on_submit=False):
|
354 |
editor_state = st.session_state.get("construction_editor", {})
|
355 |
+
form_state = st.session_state.get("construction_form_state", {})
|
356 |
+
if not form_state or not isinstance(form_state, dict) or not form_state.get("layers"):
|
357 |
+
form_state = {
|
358 |
+
"name": "",
|
359 |
+
"num_layers": 1,
|
360 |
+
"layers": [{"material_name": material_names[0] if material_names else "", "thickness": 0.1}]
|
361 |
+
}
|
362 |
+
st.session_state.construction_form_state = form_state
|
363 |
+
logger.debug(f"Form state: {form_state}")
|
364 |
is_edit = editor_state.get("is_edit", False)
|
365 |
original_name = editor_state.get("original_name", "")
|
366 |
name = st.text_input(
|
|
|
495 |
"layers": [{"material_name": default_material, "thickness": 0.1}]
|
496 |
}
|
497 |
st.session_state.construction_action = {"action": None, "id": None}
|
498 |
+
st.session_state.rerun_trigger = ""
|
499 |
st.session_state.construction_rerun_pending = True
|
500 |
else:
|
501 |
st.error(f"Failed to save construction: {message}")
|