QIDNLF commited on
Commit
bc975f4
ยท
verified ยท
1 Parent(s): 1928550

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -38
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
- target = next(d for d in db_registry if d["standard"] == standard and d["version"] == version)
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
- print("MAIN TABLE:", main_t, flush=True)
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=[], value=None, interactive=False)
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
- f"SELECT DISTINCT [{ch}], [{ca}] FROM [{main_t}]",
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] standard: {standard}, version: {version}")
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 base_std or not base_ver or not comp_std:
457
  return gr.update(choices=[], value=None, interactive=False)
458
 
459
  try:
460
- conn = sqlite3.connect("mapping.db")
461
- query = """
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
- if df.empty:
 
 
 
 
 
 
 
 
 
 
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
  # --------------------------