QIDNLF commited on
Commit
3dca055
·
verified ·
1 Parent(s): 6e0dae0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -6
app.py CHANGED
@@ -64,21 +64,48 @@ def update_category_dd(standard, version):
64
  target_db = next(db for db in db_registry if db["standard"] == standard and db["version"] == version)
65
  conn = sqlite3.connect(target_db["path"])
66
  all_tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
 
 
67
  main_t = f"{standard}_{version}"
68
- if main_t not in all_tables: main_t = all_tables[0]
 
 
 
69
 
70
- cols = pd.read_sql(f"PRAGMA table_info([{main_t}])", conn)['name'].tolist()
71
- if 'chapter' in cols and 'category' in cols:
72
- df_cat = pd.read_sql(f"SELECT DISTINCT chapter, category FROM [{main_t}]", conn)
73
- cats = [f"{row['chapter']}.{row['category']}" for _, row in df_cat.iterrows()]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  cats.sort(key=natural_sort_key)
75
  choices.extend(cats)
76
 
 
77
  prefix = f"{standard}_{version}_"
78
  sub_tables = [t.replace(prefix, "") for t in all_tables if t != main_t]
79
  choices.extend(sorted(sub_tables))
 
80
  conn.close()
81
- except: pass
 
 
 
82
  return gr.Dropdown(choices=choices, value=None)
83
 
84
  # 3️⃣ 데이터 조회 로직 (정렬 로직 제거 버전)
 
64
  target_db = next(db for db in db_registry if db["standard"] == standard and db["version"] == version)
65
  conn = sqlite3.connect(target_db["path"])
66
  all_tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
67
+
68
+ # 1. 메인 테이블 결정 로직 강화
69
  main_t = f"{standard}_{version}"
70
+ if main_t not in all_tables:
71
+ # 파일명과 완벽히 일치하는 테이블이 없으면, standard와 version을 포함하는 가장 짧은 이름을 선택
72
+ candidates = [t for t in all_tables if standard in t and version in t]
73
+ main_t = min(candidates, key=len) if candidates else all_tables[0]
74
 
75
+ # 2. 컬럼 정보 가져오기 (대소문자 무시 처리)
76
+ cols_info = pd.read_sql(f"PRAGMA table_info([{main_t}])", conn)
77
+ actual_cols = cols_info['name'].tolist()
78
+ lower_cols = [c.lower() for c in actual_cols] # 비교용 소문자 리스트
79
+
80
+ # 3. Chapter / Category 목록 생성
81
+ if 'chapter' in lower_cols and 'category' in lower_cols:
82
+ # 실제 DB에 저장된 대소문자 그대로의 컬럼명을 찾음
83
+ col_chapter = actual_cols[lower_cols.index('chapter')]
84
+ col_category = actual_cols[lower_cols.index('category')]
85
+
86
+ # DISTINCT 쿼리로 목록 추출
87
+ df_cat = pd.read_sql(f"SELECT DISTINCT [{col_chapter}], [{col_category}] FROM [{main_t}]", conn)
88
+
89
+ cats = []
90
+ for _, row in df_cat.iterrows():
91
+ ch = str(row[col_chapter]) if row[col_chapter] is not None else ""
92
+ ca = str(row[col_category]) if row[col_category] is not None else ""
93
+ if ch or ca: # 둘 중 하나라도 내용이 있으면 추가
94
+ cats.append(f"{ch}.{ca}")
95
+
96
  cats.sort(key=natural_sort_key)
97
  choices.extend(cats)
98
 
99
+ # 4. 부속 테이블(Annex 등) 추가
100
  prefix = f"{standard}_{version}_"
101
  sub_tables = [t.replace(prefix, "") for t in all_tables if t != main_t]
102
  choices.extend(sorted(sub_tables))
103
+
104
  conn.close()
105
+ except Exception as e:
106
+ print(f"Error: {e}")
107
+ pass
108
+
109
  return gr.Dropdown(choices=choices, value=None)
110
 
111
  # 3️⃣ 데이터 조회 로직 (정렬 로직 제거 버전)