File size: 2,979 Bytes
1adc2e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | from db import execute_query, fetch_all
import pandas as pd
EMPTY_MATERIAL_COLUMNS = [
"material_name",
"material_abbreviation",
"section",
"property_name",
"value",
"unit",
"english",
"test_condition",
"comments",
]
def load_material_data(material_type: str) -> pd.DataFrame:
table_map = {
"Polymers": "Polymers",
"Fibers": "Fibers",
"Composites": "Composites_materials",
}
table = table_map.get(material_type)
if not table:
return pd.DataFrame(columns=EMPTY_MATERIAL_COLUMNS)
query = f"""
SELECT
material_name,
material_abbreviation,
section,
property_name,
value,
unit,
english,
test_condition,
comments
FROM "{table}"
"""
try:
rows = fetch_all(query)
except Exception:
return pd.DataFrame(columns=EMPTY_MATERIAL_COLUMNS)
return pd.DataFrame(rows, columns=EMPTY_MATERIAL_COLUMNS)
def get_all_sections():
all_data = pd.concat([
load_material_data("Polymers"),
load_material_data("Fibers"),
load_material_data("Composites"),
], ignore_index=True)
if all_data.empty or "section" not in all_data.columns:
return []
return sorted(all_data["section"].dropna().unique().tolist())
def insert_material_rows(df: pd.DataFrame) -> int:
if df is None or df.empty:
return 0
table_map = {
"Polymer": "Polymers",
"Fiber": "Fibers",
"Composite": "Composites_materials",
}
insert_template = """
INSERT INTO "{table}" (
material_name,
material_abbreviation,
section,
property_name,
value,
unit,
english,
test_condition,
comments
) VALUES (
:material_name,
:material_abbreviation,
:section,
:property_name,
:value,
:unit,
:english,
:test_condition,
:comments
)
"""
inserted = 0
for _, row in df.iterrows():
table = table_map.get(row.get("material_class"))
if not table:
continue
params = {
"material_name": row.get("material_name", ""),
"material_abbreviation": row.get("material_abbreviation", ""),
"section": row.get("section", ""),
"property_name": row.get("property_name", ""),
"value": row.get("value", ""),
"unit": row.get("unit", ""),
"english": row.get("english", ""),
"test_condition": row.get("test_condition", ""),
"comments": row.get("comments", ""),
}
try:
inserted += execute_query(insert_template.format(table=table), params)
except Exception:
return inserted
return inserted
|