QIDNLF commited on
Commit
5422ee0
ยท
verified ยท
1 Parent(s): c8b9484

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -24
app.py CHANGED
@@ -80,26 +80,43 @@ def update_base_category_dropdown(standard, version):
80
 
81
  try:
82
  conn = sqlite3.connect(db_path)
83
- # 1. DB ๋‚ด์˜ ๋ฌผ๋ฆฌ์  ํ…Œ์ด๋ธ” ๋ชฉ๋ก (TableA, TableB_C ๋“ฑ)
84
  tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
85
- main_table = f"{standard}_{version}"
86
 
 
 
 
 
 
 
87
  pattern = re.compile(f"^{standard}[_\\s-]*{version}[_\\s-]*", re.IGNORECASE)
88
  for t in tables:
89
  if t == main_table or t.lower() in ['mapping_registry', 'mapping_table', 'table_config']: continue
90
  short_name = pattern.sub("", t).strip(" _")
91
- choices.append(short_name)
92
-
93
- # 2. ๋ณธ๋ฌธ ๋‚ด์˜ ์กฐํ•ญ ๋ฒˆํ˜ธ ๋ฆฌ์ŠคํŠธ (2.1, 3.2 ๋“ฑ)
94
- cols = pd.read_sql(f"PRAGMA table_info([{main_table}])", conn)['name'].tolist()
95
- lower_cols = [c.lower() for c in cols]
96
- if 'chapter' in lower_cols and 'category' in lower_cols:
97
- df = pd.read_sql(f"SELECT DISTINCT chapter, category FROM [{main_table}]", conn)
98
- for _, row in df.iterrows():
99
- if row['chapter'] and row['category']:
100
- choices.append(f"{row['chapter']}.{row['category']}")
 
 
 
 
 
 
 
 
 
 
101
  conn.close()
102
- except Exception: traceback.print_exc()
 
 
 
103
  return gr.update(choices=choices, value=None, interactive=True)
104
 
105
  def update_comp_standard_dropdown(base_std, base_ver):
@@ -157,23 +174,60 @@ def update_comp_category_dropdown(base_std, base_ver, base_cat, comp_std, comp_v
157
  if not all([base_std, base_ver, base_cat, comp_std, comp_ver]):
158
  return gr.update(choices=[], value=None, interactive=False)
159
 
160
- # ํ˜„์žฌ ์„ ํƒ๋œ ๊ธฐ์ค€์˜ ํƒ€์ž… ํŒ๋ณ„
161
- base_type = "Main" if base_cat == "ALL" or (base_cat and base_cat[0].isdigit()) else base_cat
162
 
163
  try:
164
  conn = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
165
- # Registry์—์„œ ํ—ˆ์šฉ๋œ Comp_Type๋งŒ ์ฟผ๋ฆฌ
166
- query = """
167
- SELECT DISTINCT Comp_Type FROM Mapping_registry
168
- WHERE Base_std=? AND Base_ver=? AND Base_Type=? AND Comp_std=? AND Comp_ver=?
169
- """
170
  df = pd.read_sql(query, conn, params=[base_std, base_ver, base_type, comp_std, comp_ver])
171
  conn.close()
172
 
173
- choices = df['Comp_Type'].tolist()
174
- # ๋งŒ์•ฝ Registry์— ์—†๋‹ค๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ๋‹นํ™ฉํ•˜์ง€ ์•Š๊ฒŒ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋‚˜ ๋นˆ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜
175
- return gr.update(choices=choices, value=choices[0] if choices else None, interactive=True)
176
- except Exception:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  return gr.update(choices=[], value=None)
178
 
179
  def reset_base_selections():
 
80
 
81
  try:
82
  conn = sqlite3.connect(db_path)
 
83
  tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
 
84
 
85
+ main_table = f"{standard}_{version}"
86
+ # [๋ณต์›] ๋ฉ”์ธ ํ…Œ์ด๋ธ” ์ด๋ฆ„ ์•ˆ์ „์žฅ์น˜
87
+ if main_table not in tables:
88
+ main_table = tables[0] if tables else None
89
+
90
+ # 1. ๋ถ€์† ํ…Œ์ด๋ธ”(TableA ๋“ฑ) ๋ฆฌ์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ
91
  pattern = re.compile(f"^{standard}[_\\s-]*{version}[_\\s-]*", re.IGNORECASE)
92
  for t in tables:
93
  if t == main_table or t.lower() in ['mapping_registry', 'mapping_table', 'table_config']: continue
94
  short_name = pattern.sub("", t).strip(" _")
95
+ if short_name and short_name not in choices:
96
+ choices.append(short_name)
97
+ elif t not in choices:
98
+ choices.append(t)
99
+
100
+ # 2. ๋ณธ๋ฌธ ์กฐํ•ญ ๋ฆฌ์ŠคํŠธ(1. Scope ๋“ฑ) ๊ฐ€์ ธ์˜ค๊ธฐ
101
+ if main_table:
102
+ cols = pd.read_sql(f"PRAGMA table_info([{main_table}])", conn)['name'].tolist()
103
+ lower_cols = [c.lower() for c in cols]
104
+ if 'chapter' in lower_cols and 'category' in lower_cols:
105
+ ch_col = cols[lower_cols.index('chapter')]
106
+ ca_col = cols[lower_cols.index('category')]
107
+
108
+ df = pd.read_sql(f"SELECT DISTINCT [{ch_col}], [{ca_col}] FROM [{main_table}]", conn)
109
+ for _, row in df.iterrows():
110
+ ch = str(row[ch_col]).strip()
111
+ ca = str(row[ca_col]).strip()
112
+ # [๋ณต์›] None, nan ๊ฐ’ ํ•„ํ„ฐ๋ง
113
+ if ch and ca and ch.lower() not in ['none', 'nan'] and ca.lower() not in ['none', 'nan']:
114
+ choices.append(f"{ch}.{ca}")
115
  conn.close()
116
+ except Exception:
117
+ import traceback
118
+ traceback.print_exc()
119
+
120
  return gr.update(choices=choices, value=None, interactive=True)
121
 
122
  def update_comp_standard_dropdown(base_std, base_ver):
 
174
  if not all([base_std, base_ver, base_cat, comp_std, comp_ver]):
175
  return gr.update(choices=[], value=None, interactive=False)
176
 
177
+ # ๊ธฐ์ค€ ๋ฒ•๊ทœ์˜ ํƒ€์ž… ํŒ๋ณ„
178
+ base_type = "Main" if base_cat == "ALL" or (base_cat and base_cat[0].isdigit() and "." in base_cat) else base_cat
179
 
180
  try:
181
  conn = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
182
+ # Registry์—์„œ ํ—ˆ์šฉ๋œ Comp_Type ์กฐํšŒ
183
+ query = "SELECT DISTINCT Comp_Type FROM Mapping_registry WHERE Base_std=? AND Base_ver=? AND Base_Type=? AND Comp_std=? AND Comp_ver=?"
 
 
 
184
  df = pd.read_sql(query, conn, params=[base_std, base_ver, base_type, comp_std, comp_ver])
185
  conn.close()
186
 
187
+ allowed_types = df['Comp_Type'].dropna().tolist()
188
+ if not allowed_types:
189
+ return gr.update(choices=[], value=None)
190
+
191
+ final_choices = []
192
+
193
+ # [ํ•ต์‹ฌ ์ˆ˜์ •] Comp_Type์ด 'Main'์ธ ๊ฒฝ์šฐ, ์‹ค์ œ DB๋ฅผ ์—ด์–ด์„œ ALL๊ณผ ์„ธ๋ถ€ ์กฐํ•ญ์„ ๊ฐ€์ ธ์˜ด!
194
+ if "Main" in allowed_types:
195
+ final_choices.append("ALL")
196
+ comp_db_path = os.path.join(UPLOAD_DIR, f"{comp_std}_{comp_ver}.db")
197
+
198
+ if os.path.exists(comp_db_path):
199
+ c_conn = sqlite3.connect(comp_db_path)
200
+ c_tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", c_conn)['name'].tolist()
201
+
202
+ c_main_table = f"{comp_std}_{comp_ver}"
203
+ if c_main_table not in c_tables:
204
+ c_main_table = c_tables[0] if c_tables else None
205
+
206
+ if c_main_table:
207
+ cols = pd.read_sql(f"PRAGMA table_info([{c_main_table}])", c_conn)['name'].tolist()
208
+ lower_cols = [c.lower() for c in cols]
209
+ if 'chapter' in lower_cols and 'category' in lower_cols:
210
+ ch_col = cols[lower_cols.index('chapter')]
211
+ ca_col = cols[lower_cols.index('category')]
212
+
213
+ c_df = pd.read_sql(f"SELECT DISTINCT [{ch_col}], [{ca_col}] FROM [{c_main_table}]", c_conn)
214
+ for _, row in c_df.iterrows():
215
+ ch = str(row[ch_col]).strip()
216
+ ca = str(row[ca_col]).strip()
217
+ if ch and ca and ch.lower() not in ['none', 'nan'] and ca.lower() not in ['none', 'nan']:
218
+ final_choices.append(f"{ch}.{ca}")
219
+ c_conn.close()
220
+
221
+ # Main์ด ์•„๋‹Œ ๋‹ค๋ฅธ ํƒ€์ž…(TableA ๋“ฑ)์€ ๊ทธ๋Œ€๋กœ ๋ชฉ๋ก์— ์ถ”๊ฐ€
222
+ for t in allowed_types:
223
+ if t != "Main":
224
+ final_choices.append(t)
225
+
226
+ return gr.update(choices=final_choices, value=final_choices[0] if final_choices else None, interactive=True)
227
+
228
+ except Exception as e:
229
+ import traceback
230
+ traceback.print_exc()
231
  return gr.update(choices=[], value=None)
232
 
233
  def reset_base_selections():