Update app.py
Browse files
app.py
CHANGED
|
@@ -124,6 +124,11 @@ def fetch_database_records(std, ver, cat, table_type):
|
|
| 124 |
|
| 125 |
real_anchors = [c for c in df.columns if any(a.lower() == c.lower() for a in anchors)]
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
final_cols = []
|
| 128 |
for d in displays:
|
| 129 |
for c in df.columns:
|
|
@@ -432,84 +437,86 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 432 |
s = s[:-2]
|
| 433 |
return s.replace(" ", "")
|
| 434 |
|
|
|
|
| 435 |
internal_rename_b = {c: f"{c}_INTERNAL_BASE" for c in df_base.columns if c != 'merge_key'}
|
| 436 |
internal_rename_c = {c: f"{c}_INTERNAL_COMP" for c in df_comp.columns if c != 'merge_key'}
|
| 437 |
|
| 438 |
df_base['merge_key'] = df_base[real_anchors_b].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 439 |
df_comp['merge_key'] = df_comp[real_anchors_c].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 440 |
|
| 441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
else:
|
| 447 |
-
conn_map = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
|
| 448 |
-
registry_query = """
|
| 449 |
-
SELECT Target_Table FROM Mapping_registry
|
| 450 |
-
WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?
|
| 451 |
-
LIMIT 1
|
| 452 |
-
"""
|
| 453 |
-
reg_df = pd.read_sql(registry_query, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 454 |
|
| 455 |
-
target_table_name = "
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
if val and val.lower() not in ["none", "nan"]:
|
| 459 |
-
target_table_name = val
|
| 460 |
-
|
| 461 |
-
try:
|
| 462 |
-
q_fw = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?"
|
| 463 |
-
df_fw = pd.read_sql(q_fw, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 464 |
-
|
| 465 |
-
q_rv = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Comp_std)=? AND TRIM(Comp_ver)=? AND TRIM(Base_std)=? AND TRIM(Base_ver)=?"
|
| 466 |
-
df_rv = pd.read_sql(q_rv, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 467 |
-
except Exception as sql_e:
|
| 468 |
-
conn_map.close()
|
| 469 |
-
return pd.DataFrame({"Error": [f"λ§€ν '{target_table_name}'μ μ¬λ λ° μ€ν¨νμ΅λλ€. ν
μ΄λΈ μ΄λ¦μ νμΈνμΈμ: {str(sql_e)}"]})
|
| 470 |
conn_map.close()
|
|
|
|
|
|
|
| 471 |
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
df_fw[b_col] = df_fw[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 479 |
-
df_fw[c_col] = df_fw[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 480 |
-
df_fw = df_fw[(df_fw[b_col] == type_b.strip().upper()) & (df_fw[c_col] == type_c.strip().upper())]
|
| 481 |
-
|
| 482 |
-
if not df_rv.empty:
|
| 483 |
-
df_rv[b_col] = df_rv[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 484 |
-
df_rv[c_col] = df_rv[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 485 |
-
df_rv = df_rv[(df_rv[c_col] == type_b.strip().upper()) & (df_rv[b_col] == type_c.strip().upper())]
|
| 486 |
-
|
| 487 |
-
b_sec = cols_fw_lower.get('base_section', 'Base_section')
|
| 488 |
-
c_sec = cols_fw_lower.get('comp_section', 'Comp_section')
|
| 489 |
|
| 490 |
-
|
| 491 |
-
|
|
|
|
| 492 |
|
| 493 |
-
df_fw = df_fw[[b_sec, c_sec]].rename(columns={b_sec: 'Base_section', c_sec: 'Comp_section'})
|
| 494 |
if not df_rv.empty:
|
| 495 |
-
df_rv = df_rv[
|
| 496 |
-
|
| 497 |
-
df_rv =
|
| 498 |
|
| 499 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
|
| 514 |
df_base = df_base.rename(columns=internal_rename_b)
|
| 515 |
df_comp = df_comp.rename(columns=internal_rename_c)
|
|
@@ -556,6 +563,7 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 556 |
if final_df.empty:
|
| 557 |
return pd.DataFrame({"Info": ["π‘ μ ννμ 쑰건 κ°μ λ³κ²½λ λ΄μ©μ΄ μμ΅λλ€. (100% λμΌ)"]})
|
| 558 |
|
|
|
|
| 559 |
final_rename_map = {}
|
| 560 |
for col in final_df.columns:
|
| 561 |
if col.endswith("_INTERNAL_BASE"):
|
|
@@ -575,6 +583,8 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 575 |
|
| 576 |
return pd.DataFrame({"Info": ["쑰건μ μ ννμΈμ."]})
|
| 577 |
except Exception as e:
|
|
|
|
|
|
|
| 578 |
error_msg = str(e)
|
| 579 |
if "database is locked" in error_msg.lower():
|
| 580 |
return pd.DataFrame({"Error": ["π¨ DBκ° μ 겨μμ΅λλ€! μΌλμΌμ 'DB Browser' νλ‘κ·Έλ¨μ μμ ν μ’
λ£ν λ€ λ€μ μ‘°νν΄ μ£ΌμΈμ."]})
|
|
|
|
| 124 |
|
| 125 |
real_anchors = [c for c in df.columns if any(a.lower() == c.lower() for a in anchors)]
|
| 126 |
|
| 127 |
+
# π‘ [ν΅μ¬ λ‘μ§] Status μ΄μ UI μλ¨μ νκΈ°λλ―λ‘, μλ νμμλ κΉλνκ² μμ ν©λλ€!
|
| 128 |
+
status_cols = [c for c in df.columns if c.lower() == 'status']
|
| 129 |
+
if status_cols:
|
| 130 |
+
df = df.drop(columns=status_cols)
|
| 131 |
+
|
| 132 |
final_cols = []
|
| 133 |
for d in displays:
|
| 134 |
for c in df.columns:
|
|
|
|
| 437 |
s = s[:-2]
|
| 438 |
return s.replace(" ", "")
|
| 439 |
|
| 440 |
+
# π‘ [ν΅μ¬ λ‘μ§] λ΄λΆ μ²λ¦¬μ© μμ 꼬리νλ₯Ό λ¬μ KeyErrorλ₯Ό μμ² μ°¨λ¨ν©λλ€!
|
| 441 |
internal_rename_b = {c: f"{c}_INTERNAL_BASE" for c in df_base.columns if c != 'merge_key'}
|
| 442 |
internal_rename_c = {c: f"{c}_INTERNAL_COMP" for c in df_comp.columns if c != 'merge_key'}
|
| 443 |
|
| 444 |
df_base['merge_key'] = df_base[real_anchors_b].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 445 |
df_comp['merge_key'] = df_comp[real_anchors_c].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 446 |
|
| 447 |
+
# π‘ [ν΅μ¬ λ‘μ§] λ§€ν μ₯λΆ(Mapping_registry)λ₯Ό 무쑰건 μ΅μ°μ μΌλ‘ κ²μ¬ν©λλ€.
|
| 448 |
+
conn_map = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
|
| 449 |
+
registry_query = """
|
| 450 |
+
SELECT Target_Table FROM Mapping_registry
|
| 451 |
+
WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?
|
| 452 |
+
LIMIT 1
|
| 453 |
+
"""
|
| 454 |
+
reg_df = pd.read_sql(registry_query, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 455 |
+
|
| 456 |
+
target_table_name = "Mapping_table"
|
| 457 |
+
if not reg_df.empty and pd.notna(reg_df.iloc[0]['Target_Table']):
|
| 458 |
+
val = str(reg_df.iloc[0]['Target_Table']).strip()
|
| 459 |
+
if val and val.lower() not in ["none", "nan"]:
|
| 460 |
+
target_table_name = val
|
| 461 |
|
| 462 |
+
try:
|
| 463 |
+
q_fw = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?"
|
| 464 |
+
df_fw = pd.read_sql(q_fw, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
+
q_rv = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Comp_std)=? AND TRIM(Comp_ver)=? AND TRIM(Base_std)=? AND TRIM(Base_ver)=?"
|
| 467 |
+
df_rv = pd.read_sql(q_rv, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 468 |
+
except Exception as sql_e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
conn_map.close()
|
| 470 |
+
return pd.DataFrame({"Error": [f"λ§€ν μ₯λΆ '{target_table_name}'μ μ¬λ λ° μ€ν¨νμ΅λλ€. ν
μ΄λΈ μ΄λ¦μ νμΈνοΏ½οΏ½μ: {str(sql_e)}"]})
|
| 471 |
+
conn_map.close()
|
| 472 |
|
| 473 |
+
cols_fw_lower = {c.lower(): c for c in df_fw.columns}
|
| 474 |
+
|
| 475 |
+
if 'base_type' in cols_fw_lower and 'comp_type' in cols_fw_lower:
|
| 476 |
+
b_col = cols_fw_lower['base_type']
|
| 477 |
+
c_col = cols_fw_lower['comp_type']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
+
df_fw[b_col] = df_fw[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 480 |
+
df_fw[c_col] = df_fw[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 481 |
+
df_fw = df_fw[(df_fw[b_col] == type_b.strip().upper()) & (df_fw[c_col] == type_c.strip().upper())]
|
| 482 |
|
|
|
|
| 483 |
if not df_rv.empty:
|
| 484 |
+
df_rv[b_col] = df_rv[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 485 |
+
df_rv[c_col] = df_rv[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 486 |
+
df_rv = df_rv[(df_rv[c_col] == type_b.strip().upper()) & (df_rv[b_col] == type_c.strip().upper())]
|
| 487 |
|
| 488 |
+
b_sec = cols_fw_lower.get('base_section', 'Base_section')
|
| 489 |
+
c_sec = cols_fw_lower.get('comp_section', 'Comp_section')
|
| 490 |
+
|
| 491 |
+
if b_sec not in df_fw.columns or c_sec not in df_fw.columns:
|
| 492 |
+
return pd.DataFrame({"Error": [f"'{target_table_name}' μ₯λΆμ '{b_sec}' λλ '{c_sec}' μ΄μ΄ μμ΅λλ€. λμλ¬Έμλ₯Ό νμΈνμΈμ."]})
|
| 493 |
+
|
| 494 |
+
df_fw = df_fw[[b_sec, c_sec]].rename(columns={b_sec: 'Base_section', c_sec: 'Comp_section'})
|
| 495 |
+
if not df_rv.empty:
|
| 496 |
+
df_rv = df_rv[[b_sec, c_sec]].rename(columns={b_sec: 'Comp_section', c_sec: 'Base_section'})
|
| 497 |
+
else:
|
| 498 |
+
df_rv = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
| 499 |
|
| 500 |
+
df_mapping = pd.concat([df_fw, df_rv], ignore_index=True)
|
| 501 |
+
|
| 502 |
+
if not df_mapping.empty:
|
| 503 |
+
df_mapping['Base_section'] = df_mapping['Base_section'].astype(str).str.replace('\n', ',').str.split(',')
|
| 504 |
+
df_mapping['Comp_section'] = df_mapping['Comp_section'].astype(str).str.replace('\n', ',').str.split(',')
|
| 505 |
+
df_mapping = df_mapping.explode('Base_section').explode('Comp_section')
|
| 506 |
+
|
| 507 |
+
df_mapping['Base_section'] = df_mapping['Base_section'].apply(clean_key_val)
|
| 508 |
+
df_mapping['Comp_section'] = df_mapping['Comp_section'].apply(clean_key_val)
|
| 509 |
+
|
| 510 |
+
df_mapping = df_mapping[(df_mapping['Base_section'] != "") & (df_mapping['Comp_section'] != "")]
|
| 511 |
+
bridge = df_mapping.dropna().drop_duplicates()
|
| 512 |
+
else:
|
| 513 |
+
bridge = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
| 514 |
+
|
| 515 |
+
# π‘ [ν΅μ¬ λ‘μ§] λ§€ν μ₯λΆκ° μλ²½νκ² λΉμ΄μμ λλ§(μ¦, μ무κ²λ μ μΌμ λλ§) μ΅νμ μλ¨μΌλ‘ 1:1 λ³ν©
|
| 516 |
+
is_same_std = (base_std.strip().upper() == comp_std.strip().upper() and type_b.strip().upper() == type_c.strip().upper())
|
| 517 |
+
if bridge.empty and is_same_std:
|
| 518 |
+
all_keys = list(set(df_base['merge_key']).union(set(df_comp['merge_key'])))
|
| 519 |
+
bridge = pd.DataFrame({'Base_section': all_keys, 'Comp_section': all_keys})
|
| 520 |
|
| 521 |
df_base = df_base.rename(columns=internal_rename_b)
|
| 522 |
df_comp = df_comp.rename(columns=internal_rename_c)
|
|
|
|
| 563 |
if final_df.empty:
|
| 564 |
return pd.DataFrame({"Info": ["π‘ μ ννμ 쑰건 κ°μ λ³κ²½λ λ΄μ©μ΄ μμ΅λλ€. (100% λμΌ)"]})
|
| 565 |
|
| 566 |
+
# π‘ [ν΅μ¬ λ‘μ§] νλ©΄ μΆλ ₯ μ§μ μ μ§μ λΆν μμ 꼬리νλ₯Ό κΉλνκ² λΌμ΄λ²λ¦½λλ€.
|
| 567 |
final_rename_map = {}
|
| 568 |
for col in final_df.columns:
|
| 569 |
if col.endswith("_INTERNAL_BASE"):
|
|
|
|
| 583 |
|
| 584 |
return pd.DataFrame({"Info": ["쑰건μ μ ννμΈμ."]})
|
| 585 |
except Exception as e:
|
| 586 |
+
import traceback
|
| 587 |
+
traceback.print_exc()
|
| 588 |
error_msg = str(e)
|
| 589 |
if "database is locked" in error_msg.lower():
|
| 590 |
return pd.DataFrame({"Error": ["π¨ DBκ° μ 겨μμ΅λλ€! μΌλμΌμ 'DB Browser' νλ‘κ·Έλ¨μ μμ ν μ’
λ£ν λ€ λ€μ μ‘°νν΄ μ£ΌμΈμ."]})
|