Update app.py
Browse files
app.py
CHANGED
|
@@ -434,10 +434,9 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 434 |
df_comp['merge_key'] = df_comp[real_anchors_c].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 435 |
|
| 436 |
# =========================================================
|
| 437 |
-
# ๐ก [
|
| 438 |
# =========================================================
|
| 439 |
conn_map = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
|
| 440 |
-
|
| 441 |
registry_query = """
|
| 442 |
SELECT Target_Table FROM Mapping_registry
|
| 443 |
WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?
|
|
@@ -445,16 +444,25 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 445 |
"""
|
| 446 |
reg_df = pd.read_sql(registry_query, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 447 |
|
| 448 |
-
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
-
q_fw = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?"
|
| 452 |
-
df_fw = pd.read_sql(q_fw, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 453 |
-
|
| 454 |
-
q_rv = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Comp_std)=? AND TRIM(Comp_ver)=? AND TRIM(Base_std)=? AND TRIM(Base_ver)=?"
|
| 455 |
-
df_rv = pd.read_sql(q_rv, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 456 |
-
conn_map.close()
|
| 457 |
# =========================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
cols_fw_lower = {c.lower(): c for c in df_fw.columns}
|
| 460 |
|
|
@@ -474,6 +482,9 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 474 |
b_sec = cols_fw_lower.get('base_section', 'Base_section')
|
| 475 |
c_sec = cols_fw_lower.get('comp_section', 'Comp_section')
|
| 476 |
|
|
|
|
|
|
|
|
|
|
| 477 |
df_fw = df_fw[[b_sec, c_sec]].rename(columns={b_sec: 'Base_section', c_sec: 'Comp_section'})
|
| 478 |
if not df_rv.empty:
|
| 479 |
df_rv = df_rv[[b_sec, c_sec]].rename(columns={b_sec: 'Comp_section', c_sec: 'Base_section'})
|
|
@@ -489,6 +500,11 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 489 |
|
| 490 |
df_mapping['Base_section'] = df_mapping['Base_section'].apply(clean_key_val)
|
| 491 |
df_mapping['Comp_section'] = df_mapping['Comp_section'].apply(clean_key_val)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
df_mapping = df_mapping.dropna().drop_duplicates()
|
| 493 |
else:
|
| 494 |
df_mapping = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
|
@@ -523,16 +539,12 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 523 |
for c in rename_b.values(): row_dict[c] = str(row[c]) if has_b and not pd.isna(row[c]) else ""
|
| 524 |
for c in rename_c.values(): row_dict[c] = str(row[c]) if has_c and not pd.isna(row[c]) else ""
|
| 525 |
|
| 526 |
-
# =======================================================
|
| 527 |
-
# ๐ก [๊น๊นํ ๋น์นธ ๊ฒ์ฌ] ์์ชฝ ๋ค ๊ธ์๊ฐ ์ฑ์์ ธ ์๋์ง ํ์ธ!
|
| 528 |
-
# =======================================================
|
| 529 |
if mapped_only:
|
| 530 |
b_has_val = any(str(row_dict[c]).strip() not in ["", "nan", "None", " "] for c in rename_b.values())
|
| 531 |
c_has_val = any(str(row_dict[c]).strip() not in ["", "nan", "None", " "] for c in rename_c.values())
|
| 532 |
|
| 533 |
if not (b_has_val and c_has_val):
|
| 534 |
continue
|
| 535 |
-
# =======================================================
|
| 536 |
|
| 537 |
if has_b and has_c:
|
| 538 |
for orig_col in rename_b.keys():
|
|
@@ -565,8 +577,11 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 565 |
except Exception as e:
|
| 566 |
import traceback
|
| 567 |
traceback.print_exc()
|
| 568 |
-
|
| 569 |
-
|
|
|
|
|
|
|
|
|
|
| 570 |
|
| 571 |
# ==========================================
|
| 572 |
# 4. UI Layout & Event Binding
|
|
|
|
| 434 |
df_comp['merge_key'] = df_comp[real_anchors_c].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 435 |
|
| 436 |
# =========================================================
|
| 437 |
+
# ๐ก [๋ฐฉ์ด๋ง 1] Target_Table ์ด๋ฆ ๊น๋ํ๊ฒ ๊ฐ์ ธ์ค๊ธฐ
|
| 438 |
# =========================================================
|
| 439 |
conn_map = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
|
|
|
|
| 440 |
registry_query = """
|
| 441 |
SELECT Target_Table FROM Mapping_registry
|
| 442 |
WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?
|
|
|
|
| 444 |
"""
|
| 445 |
reg_df = pd.read_sql(registry_query, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 446 |
|
| 447 |
+
target_table_name = "Mapping_table"
|
| 448 |
+
if not reg_df.empty and pd.notna(reg_df.iloc[0]['Target_Table']):
|
| 449 |
+
val = str(reg_df.iloc[0]['Target_Table']).strip()
|
| 450 |
+
if val and val.lower() not in ["none", "nan"]:
|
| 451 |
+
target_table_name = val
|
| 452 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
# =========================================================
|
| 454 |
+
# ๐ก [๋ฐฉ์ด๋ง 2] ๋งคํ ์ฅ๋ถ ์์ ํ๊ฒ ์ฝ์ด์ค๊ธฐ (์คํ๋๋ฉด ์๋ฌ ๋์)
|
| 455 |
+
# =========================================================
|
| 456 |
+
try:
|
| 457 |
+
q_fw = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?"
|
| 458 |
+
df_fw = pd.read_sql(q_fw, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 459 |
+
|
| 460 |
+
q_rv = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Comp_std)=? AND TRIM(Comp_ver)=? AND TRIM(Base_std)=? AND TRIM(Base_ver)=?"
|
| 461 |
+
df_rv = pd.read_sql(q_rv, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 462 |
+
except Exception as sql_e:
|
| 463 |
+
conn_map.close()
|
| 464 |
+
return pd.DataFrame({"Error": [f"๋งคํ ์ฅ๋ถ '{target_table_name}'์ ์ฌ๋ ๋ฐ ์คํจํ์ต๋๋ค. ํ
์ด๋ธ ์ด๋ฆ์ ํ์ธํ์ธ์: {str(sql_e)}"]})
|
| 465 |
+
conn_map.close()
|
| 466 |
|
| 467 |
cols_fw_lower = {c.lower(): c for c in df_fw.columns}
|
| 468 |
|
|
|
|
| 482 |
b_sec = cols_fw_lower.get('base_section', 'Base_section')
|
| 483 |
c_sec = cols_fw_lower.get('comp_section', 'Comp_section')
|
| 484 |
|
| 485 |
+
if b_sec not in df_fw.columns or c_sec not in df_fw.columns:
|
| 486 |
+
return pd.DataFrame({"Error": [f"'{target_table_name}' ์ฅ๋ถ์ '{b_sec}' ๋๋ '{c_sec}' ์ด์ด ์์ต๋๋ค. ๋์๋ฌธ์๋ฅผ ํ์ธํ์ธ์."]})
|
| 487 |
+
|
| 488 |
df_fw = df_fw[[b_sec, c_sec]].rename(columns={b_sec: 'Base_section', c_sec: 'Comp_section'})
|
| 489 |
if not df_rv.empty:
|
| 490 |
df_rv = df_rv[[b_sec, c_sec]].rename(columns={b_sec: 'Comp_section', c_sec: 'Base_section'})
|
|
|
|
| 500 |
|
| 501 |
df_mapping['Base_section'] = df_mapping['Base_section'].apply(clean_key_val)
|
| 502 |
df_mapping['Comp_section'] = df_mapping['Comp_section'].apply(clean_key_val)
|
| 503 |
+
|
| 504 |
+
# =========================================================
|
| 505 |
+
# ๐ฃ [๋ฐฉ์ด๋ง 3] ์นดํ
์์ ํญ๋ฐ ๋ฐฉ์ง: ๋น์นธ๋ผ๋ฆฌ ์ฐ๊ฒฐ๋ ๊ฐ์ง ๋งคํ ์ญ์
|
| 506 |
+
# =========================================================
|
| 507 |
+
df_mapping = df_mapping[(df_mapping['Base_section'] != "") & (df_mapping['Comp_section'] != "")]
|
| 508 |
df_mapping = df_mapping.dropna().drop_duplicates()
|
| 509 |
else:
|
| 510 |
df_mapping = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
|
|
|
| 539 |
for c in rename_b.values(): row_dict[c] = str(row[c]) if has_b and not pd.isna(row[c]) else ""
|
| 540 |
for c in rename_c.values(): row_dict[c] = str(row[c]) if has_c and not pd.isna(row[c]) else ""
|
| 541 |
|
|
|
|
|
|
|
|
|
|
| 542 |
if mapped_only:
|
| 543 |
b_has_val = any(str(row_dict[c]).strip() not in ["", "nan", "None", " "] for c in rename_b.values())
|
| 544 |
c_has_val = any(str(row_dict[c]).strip() not in ["", "nan", "None", " "] for c in rename_c.values())
|
| 545 |
|
| 546 |
if not (b_has_val and c_has_val):
|
| 547 |
continue
|
|
|
|
| 548 |
|
| 549 |
if has_b and has_c:
|
| 550 |
for orig_col in rename_b.keys():
|
|
|
|
| 577 |
except Exception as e:
|
| 578 |
import traceback
|
| 579 |
traceback.print_exc()
|
| 580 |
+
# ์์คํ
์๋ฌ ๋์ ํ๋ฉด์ ๊น๋ํ๊ฒ ์์ธ์ ๋์์ค๋๋ค.
|
| 581 |
+
error_msg = str(e)
|
| 582 |
+
if "database is locked" in error_msg.lower():
|
| 583 |
+
return pd.DataFrame({"Error": ["๐จ DB๊ฐ ์ ๊ฒจ์์ต๋๋ค! ์ผ๋์ผ์ 'DB Browser' ํ๋ก๊ทธ๋จ์ ์์ ํ ์ข
๋ฃํ ๋ค ๋ค์ ์กฐํํด ์ฃผ์ธ์."]})
|
| 584 |
+
return pd.DataFrame({"Error": [f"์์คํ
์ค๋ฅ ๋ฐ์: {error_msg}"]})
|
| 585 |
|
| 586 |
# ==========================================
|
| 587 |
# 4. UI Layout & Event Binding
|