Update page_files/Categorized_Search.py
Browse files
page_files/Categorized_Search.py
CHANGED
|
@@ -12,6 +12,40 @@ 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 |
|
| 16 |
# _class -> RDS category table
|
| 17 |
_CLASS_TABLE = {"Composites": "Composites_materials", "Fibers": "Fibers", "Polymers": "Polymers"}
|
|
@@ -84,12 +118,24 @@ def _load_plot_bytes(selected_abbr, property_choice, result_df):
|
|
| 84 |
for where, params in attempts:
|
| 85 |
try:
|
| 86 |
with engine.connect() as conn:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
except Exception:
|
| 94 |
continue
|
| 95 |
return None
|
|
|
|
| 12 |
import streamlit.components.v1 as components
|
| 13 |
import time
|
| 14 |
t0 = time.time()
|
| 15 |
+
import os
|
| 16 |
+
from urllib.parse import urlparse
|
| 17 |
+
|
| 18 |
+
S3_BUCKET = os.getenv("S3_BUCKET", "imagesstorageaims")
|
| 19 |
+
S3_REGION = os.getenv("AWS_REGION", "us-east-2")
|
| 20 |
+
|
| 21 |
+
_s3_client = None
|
| 22 |
+
def _s3():
|
| 23 |
+
global _s3_client
|
| 24 |
+
if _s3_client is None:
|
| 25 |
+
import boto3
|
| 26 |
+
_s3_client = boto3.client(
|
| 27 |
+
"s3",
|
| 28 |
+
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
|
| 29 |
+
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
|
| 30 |
+
region_name=S3_REGION,
|
| 31 |
+
)
|
| 32 |
+
return _s3_client
|
| 33 |
+
|
| 34 |
+
def _s3_bytes(ref: str):
|
| 35 |
+
"""ref = S3 key, or full s3:// / https URL -> object bytes, or None."""
|
| 36 |
+
if not ref:
|
| 37 |
+
return None
|
| 38 |
+
try:
|
| 39 |
+
if ref.startswith("s3://"):
|
| 40 |
+
p = urlparse(ref); bucket, key = p.netloc, p.path.lstrip("/")
|
| 41 |
+
elif ref.startswith("http"):
|
| 42 |
+
p = urlparse(ref); bucket = p.netloc.split(".s3")[0]; key = p.path.lstrip("/")
|
| 43 |
+
else:
|
| 44 |
+
bucket, key = S3_BUCKET, ref
|
| 45 |
+
return _s3().get_object(Bucket=bucket, Key=key)["Body"].read()
|
| 46 |
+
except Exception as e:
|
| 47 |
+
st.caption(f"(S3 fetch failed: {e})")
|
| 48 |
+
return None
|
| 49 |
|
| 50 |
# _class -> RDS category table
|
| 51 |
_CLASS_TABLE = {"Composites": "Composites_materials", "Fibers": "Fibers", "Polymers": "Polymers"}
|
|
|
|
| 118 |
for where, params in attempts:
|
| 119 |
try:
|
| 120 |
with engine.connect() as conn:
|
| 121 |
+
try:
|
| 122 |
+
row = conn.execute(text(
|
| 123 |
+
f'SELECT image_url, image FROM "{tbl}" '
|
| 124 |
+
f'WHERE {where} LIMIT 1'), params).fetchone()
|
| 125 |
+
url, blob = (row[0], row[1]) if row else (None, None)
|
| 126 |
+
except Exception:
|
| 127 |
+
# image_url column not present yet -> blob only
|
| 128 |
+
blob = conn.execute(text(
|
| 129 |
+
f'SELECT image FROM "{tbl}" '
|
| 130 |
+
f'WHERE {where} AND image IS NOT NULL LIMIT 1'),
|
| 131 |
+
params).scalar()
|
| 132 |
+
url = None
|
| 133 |
+
if url:
|
| 134 |
+
b = _s3_bytes(url)
|
| 135 |
+
if b:
|
| 136 |
+
return b
|
| 137 |
+
if blob:
|
| 138 |
+
return bytes(blob) # memoryview -> bytes for st.image
|
| 139 |
except Exception:
|
| 140 |
continue
|
| 141 |
return None
|