Update app.py
Browse files
app.py
CHANGED
|
@@ -83,34 +83,31 @@ def update_version_dd(standard):
|
|
| 83 |
return gr.Dropdown(choices=versions)
|
| 84 |
|
| 85 |
def update_category_dd(standard, version):
|
| 86 |
-
print("DEBUG category:", standard, version, flush=True)
|
| 87 |
-
|
| 88 |
if not standard or not version:
|
| 89 |
return gr.update(choices=[], value=None, interactive=False)
|
| 90 |
|
| 91 |
choices = ["ALL"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
try:
|
| 94 |
-
|
| 95 |
-
conn = sqlite3.connect(target["path"])
|
| 96 |
conn.text_factory = safe_text_factory
|
| 97 |
|
| 98 |
-
tables = pd.read_sql(
|
| 99 |
-
"SELECT name FROM sqlite_master WHERE type='table';",
|
| 100 |
-
conn
|
| 101 |
-
)['name'].tolist()
|
| 102 |
-
|
| 103 |
-
print("TABLES:", tables, flush=True)
|
| 104 |
-
|
| 105 |
main_t = f"{standard}_{version}"
|
| 106 |
-
|
| 107 |
-
|
| 108 |
if main_t not in tables:
|
| 109 |
-
print("โ ๏ธ main table not found โ fallback", flush=True)
|
| 110 |
main_t = tables[0] if tables else None
|
| 111 |
|
| 112 |
if not main_t:
|
| 113 |
-
return gr.update(choices=
|
| 114 |
|
| 115 |
cols = pd.read_sql(f"PRAGMA table_info([{main_t}])", conn)['name'].tolist()
|
| 116 |
lower = [c.lower() for c in cols]
|
|
@@ -118,23 +115,20 @@ def update_category_dd(standard, version):
|
|
| 118 |
if 'chapter' in lower and 'category' in lower:
|
| 119 |
ch = cols[lower.index('chapter')]
|
| 120 |
ca = cols[lower.index('category')]
|
| 121 |
-
|
| 122 |
-
df = pd.read_sql(
|
| 123 |
-
|
| 124 |
-
conn
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
for _, r in df.iterrows():
|
| 128 |
chapter = str(r[ch]).strip()
|
| 129 |
category = str(r[ca]).strip()
|
| 130 |
-
|
| 131 |
-
if chapter and category:
|
| 132 |
choices.append(f"{chapter}.{category}")
|
| 133 |
|
| 134 |
conn.close()
|
| 135 |
|
| 136 |
except Exception as e:
|
| 137 |
-
print(f"[Error in update_category_dd]
|
| 138 |
traceback.print_exc()
|
| 139 |
|
| 140 |
return gr.update(
|
|
@@ -453,32 +447,34 @@ def update_comp_standard(base_std, base_ver):
|
|
| 453 |
return gr.Dropdown(choices=[], interactive=False)
|
| 454 |
|
| 455 |
def update_comp_version(base_std, base_ver, comp_std):
|
| 456 |
-
if not
|
| 457 |
return gr.update(choices=[], value=None, interactive=False)
|
| 458 |
|
| 459 |
try:
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
SELECT DISTINCT Comp_ver
|
| 463 |
-
FROM Mapping_registry
|
| 464 |
-
WHERE Base_std=? AND Base_ver=? AND Comp_std=?
|
| 465 |
-
"""
|
| 466 |
df = pd.read_sql(query, conn, params=[base_std, base_ver, comp_std])
|
| 467 |
conn.close()
|
| 468 |
|
| 469 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
return gr.update(choices=[], value=None, interactive=False)
|
| 471 |
|
| 472 |
-
return gr.update(
|
| 473 |
-
choices=sorted(df['Comp_ver'].tolist()),
|
| 474 |
-
value=None,
|
| 475 |
-
interactive=True
|
| 476 |
-
)
|
| 477 |
|
| 478 |
except Exception as e:
|
| 479 |
print("[update_comp_version error]", e)
|
| 480 |
return gr.update(choices=[], value=None, interactive=False)
|
| 481 |
-
|
| 482 |
# --------------------------
|
| 483 |
# UI
|
| 484 |
# --------------------------
|
|
|
|
| 83 |
return gr.Dropdown(choices=versions)
|
| 84 |
|
| 85 |
def update_category_dd(standard, version):
|
|
|
|
|
|
|
| 86 |
if not standard or not version:
|
| 87 |
return gr.update(choices=[], value=None, interactive=False)
|
| 88 |
|
| 89 |
choices = ["ALL"]
|
| 90 |
+
|
| 91 |
+
# db_registry ๋์ ์ค์๊ฐ ํ์ผ ๊ฒฝ๋ก ์์ฑ
|
| 92 |
+
db_path = os.path.join(UPLOAD_DIR, f"{standard}_{version}.db")
|
| 93 |
+
|
| 94 |
+
# ํ์ผ์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์๋ฌ ๋ด์ง ์๊ณ ALL๋ง ๋์
|
| 95 |
+
if not os.path.exists(db_path):
|
| 96 |
+
print(f"[๊ฒฝ๊ณ ] {db_path} ํ์ผ์ด ํด๋์ ์์ต๋๋ค!")
|
| 97 |
+
return gr.update(choices=choices, value=None, interactive=True)
|
| 98 |
|
| 99 |
try:
|
| 100 |
+
conn = sqlite3.connect(db_path)
|
|
|
|
| 101 |
conn.text_factory = safe_text_factory
|
| 102 |
|
| 103 |
+
tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
main_t = f"{standard}_{version}"
|
| 105 |
+
|
|
|
|
| 106 |
if main_t not in tables:
|
|
|
|
| 107 |
main_t = tables[0] if tables else None
|
| 108 |
|
| 109 |
if not main_t:
|
| 110 |
+
return gr.update(choices=choices, value=None, interactive=True)
|
| 111 |
|
| 112 |
cols = pd.read_sql(f"PRAGMA table_info([{main_t}])", conn)['name'].tolist()
|
| 113 |
lower = [c.lower() for c in cols]
|
|
|
|
| 115 |
if 'chapter' in lower and 'category' in lower:
|
| 116 |
ch = cols[lower.index('chapter')]
|
| 117 |
ca = cols[lower.index('category')]
|
| 118 |
+
|
| 119 |
+
df = pd.read_sql(f"SELECT DISTINCT [{ch}], [{ca}] FROM [{main_t}]", conn)
|
| 120 |
+
|
|
|
|
|
|
|
|
|
|
| 121 |
for _, r in df.iterrows():
|
| 122 |
chapter = str(r[ch]).strip()
|
| 123 |
category = str(r[ca]).strip()
|
| 124 |
+
# None์ด๋ ๋น ๊ฐ ํํฐ๋ง
|
| 125 |
+
if chapter and category and chapter.lower() != 'none' and category.lower() != 'none':
|
| 126 |
choices.append(f"{chapter}.{category}")
|
| 127 |
|
| 128 |
conn.close()
|
| 129 |
|
| 130 |
except Exception as e:
|
| 131 |
+
print(f"[Error in update_category_dd] {e}")
|
| 132 |
traceback.print_exc()
|
| 133 |
|
| 134 |
return gr.update(
|
|
|
|
| 447 |
return gr.Dropdown(choices=[], interactive=False)
|
| 448 |
|
| 449 |
def update_comp_version(base_std, base_ver, comp_std):
|
| 450 |
+
if not comp_std:
|
| 451 |
return gr.update(choices=[], value=None, interactive=False)
|
| 452 |
|
| 453 |
try:
|
| 454 |
+
# 1. ๋งคํ DB ์ฐ์ ์กฐํ
|
| 455 |
+
conn = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
|
| 456 |
+
query = "SELECT DISTINCT Comp_ver FROM Mapping_registry WHERE Base_std=? AND Base_ver=? AND Comp_std=?"
|
|
|
|
|
|
|
|
|
|
| 457 |
df = pd.read_sql(query, conn, params=[base_std, base_ver, comp_std])
|
| 458 |
conn.close()
|
| 459 |
|
| 460 |
+
choices = sorted(df['Comp_ver'].dropna().unique().tolist())
|
| 461 |
+
|
| 462 |
+
# 2. ๋ง์ฝ ๋งคํ DB์์ ๋ฒ์ ์ ๋ชป ์ฐพ์๋ค๋ฉด, ์ค์ ํด๋๋ฅผ ๋ค์ ธ์ ์ฐพ์์ด (์ค์๊ฐ)
|
| 463 |
+
if not choices:
|
| 464 |
+
for f in os.listdir(UPLOAD_DIR):
|
| 465 |
+
if f.startswith(comp_std + "_") and f.endswith(".db"):
|
| 466 |
+
ver = f.replace(comp_std + "_", "").replace(".db", "")
|
| 467 |
+
choices.append(ver)
|
| 468 |
+
choices = sorted(list(set(choices)))
|
| 469 |
+
|
| 470 |
+
if not choices:
|
| 471 |
return gr.update(choices=[], value=None, interactive=False)
|
| 472 |
|
| 473 |
+
return gr.update(choices=choices, value=None, interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
|
| 475 |
except Exception as e:
|
| 476 |
print("[update_comp_version error]", e)
|
| 477 |
return gr.update(choices=[], value=None, interactive=False)
|
|
|
|
| 478 |
# --------------------------
|
| 479 |
# UI
|
| 480 |
# --------------------------
|