Update app.py
Browse files
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 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
conn.close()
|
| 102 |
-
except Exception:
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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():
|