gvlktejaswi commited on
Commit
a872331
·
verified ·
1 Parent(s): 6ef072c

Update page_files/Categorized_Search.py

Browse files
Files changed (1) hide show
  1. page_files/Categorized_Search.py +57 -4
page_files/Categorized_Search.py CHANGED
@@ -12,6 +12,57 @@ from data_loader import get_all_sections, load_material_data
12
  import streamlit.components.v1 as components
13
  import time
14
  t0 = time.time()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def clear_search():
16
  """Clear the search box and search term. Called as a callback from other widgets."""
17
  st.session_state._search_term = None
@@ -1127,10 +1178,12 @@ with right_col:
1127
  st.dataframe(result.T, use_container_width=True)
1128
 
1129
  st.subheader("Property Graph")
1130
- image_path = Path("images") / f"{selected_abbr}_{property_choice}.png"
1131
- if image_path.exists():
1132
- image = Image.open(image_path)
1133
- st.image(image, use_container_width=True, caption="Stress strain curve")
 
 
1134
  else:
1135
  st.caption("No plot image available for this material-property pair.")
1136
 
 
12
  import streamlit.components.v1 as components
13
  import time
14
  t0 = time.time()
15
+
16
+ # _class -> RDS category table
17
+ _CLASS_TABLE = {"Composites": "Composites_materials", "Fibers": "Fibers", "Polymers": "Polymers"}
18
+
19
+
20
+ def _load_plot_bytes(selected_abbr, property_choice, result_df):
21
+ """Return the PNG bytes stored in the category table's `image` column for this
22
+ material+property, or None. Reads BYTEA from RDS (no local files, no URLs).
23
+
24
+ Matching is by property_name first, narrowed by material_name/abbreviation when
25
+ available — abbreviation alone is fragile (case/whitespace/mismatch) and was
26
+ causing rows that DO have image bytes to return nothing."""
27
+ table = None
28
+ material_name = None
29
+ if not result_df.empty:
30
+ if "_class" in result_df.columns:
31
+ table = _CLASS_TABLE.get(str(result_df.iloc[0]["_class"]))
32
+ if "material_name" in result_df.columns:
33
+ material_name = str(result_df.iloc[0]["material_name"])
34
+ tables = [table] if table else list(_CLASS_TABLE.values()) # fallback: try all
35
+ try:
36
+ import mapper5
37
+ from sqlalchemy import text
38
+ engine = mapper5.get_db_engine()
39
+ except Exception as e:
40
+ st.caption(f"(plot lookup unavailable: {e})")
41
+ return None
42
+
43
+ # Try progressively looser filters: (abbr+prop) -> (name+prop) -> (prop only)
44
+ attempts = []
45
+ if selected_abbr:
46
+ attempts.append(("material_abbreviation = :a AND property_name = :p",
47
+ {"a": selected_abbr, "p": property_choice}))
48
+ if material_name:
49
+ attempts.append(("material_name = :m AND property_name = :p",
50
+ {"m": material_name, "p": property_choice}))
51
+ attempts.append(("property_name = :p", {"p": property_choice}))
52
+
53
+ for tbl in tables:
54
+ for where, params in attempts:
55
+ try:
56
+ with engine.connect() as conn:
57
+ img = conn.execute(text(
58
+ f'SELECT image FROM "{tbl}" '
59
+ f'WHERE {where} AND image IS NOT NULL LIMIT 1'),
60
+ params).scalar()
61
+ if img:
62
+ return bytes(img) # memoryview -> bytes for st.image
63
+ except Exception:
64
+ continue
65
+ return None
66
  def clear_search():
67
  """Clear the search box and search term. Called as a callback from other widgets."""
68
  st.session_state._search_term = None
 
1178
  st.dataframe(result.T, use_container_width=True)
1179
 
1180
  st.subheader("Property Graph")
1181
+ _img_bytes = _load_plot_bytes(
1182
+ selected_abbr, property_choice, result
1183
+ )
1184
+ if _img_bytes:
1185
+ st.image(_img_bytes, use_container_width=True,
1186
+ caption="Stored plot")
1187
  else:
1188
  st.caption("No plot image available for this material-property pair.")
1189