QIDNLF commited on
Commit
f602d9f
ยท
verified ยท
1 Parent(s): baf176f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
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
- # DB์— Target_Table์ด ์ ํ˜€์žˆ์œผ๋ฉด ๊ทธ๊ฑธ ์“ฐ๊ณ , ์—†๊ฑฐ๋‚˜ ๋น„์–ด์žˆ์œผ๋ฉด ๊ธฐ๋ณธ ์žฅ๋ถ€(Mapping_table)๋ฅผ ์”๋‹ˆ๋‹ค.
449
- target_table_name = reg_df.iloc[0]['Target_Table'] if not reg_df.empty and pd.notna(reg_df.iloc[0]['Target_Table']) else "Mapping_table"
 
 
 
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
- return pd.DataFrame({"Error": [f"์‹œ์Šคํ…œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"]})
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