Update app.py
Browse files
app.py
CHANGED
|
@@ -245,9 +245,15 @@ def fetch_database_records(standard, version, selection):
|
|
| 245 |
conn = sqlite3.connect(db_path)
|
| 246 |
conn.text_factory = decode_sqlite_text
|
| 247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
|
| 249 |
main_table = f"{standard}_{version}"
|
| 250 |
-
|
| 251 |
if main_table not in tables:
|
| 252 |
main_table = tables[0]
|
| 253 |
|
|
@@ -263,45 +269,46 @@ def fetch_database_records(standard, version, selection):
|
|
| 263 |
target_table = t
|
| 264 |
break
|
| 265 |
|
|
|
|
| 266 |
if target_table and target_table != main_table:
|
|
|
|
| 267 |
df = pd.read_sql(f"SELECT * FROM [{target_table}]", conn)
|
| 268 |
-
df = df.drop(columns=[c for c in df.columns if c.lower() == "version"], errors="ignore")
|
| 269 |
-
df.columns = [c.replace("_", " ").title() for c in df.columns]
|
| 270 |
-
|
| 271 |
-
for col in df.columns:
|
| 272 |
-
df[col] = df[col].apply(convert_blob_to_html_img)
|
| 273 |
-
|
| 274 |
-
conn.close()
|
| 275 |
-
return df
|
| 276 |
-
|
| 277 |
-
cursor = conn.cursor()
|
| 278 |
-
cursor.execute(f"PRAGMA table_info([{main_table}])")
|
| 279 |
-
cols = [c[1] for c in cursor.fetchall()]
|
| 280 |
-
lower_cols = [c.lower().strip() for c in cols]
|
| 281 |
-
|
| 282 |
-
real_ch, real_cat = None, None
|
| 283 |
-
for c, lc in zip(cols, lower_cols):
|
| 284 |
-
if lc == "chapter": real_ch = c
|
| 285 |
-
elif lc == "category": real_cat = c
|
| 286 |
-
|
| 287 |
-
if selection == "ALL":
|
| 288 |
-
cursor.execute(f"SELECT * FROM [{main_table}]")
|
| 289 |
-
elif "." in selection and real_ch and real_cat:
|
| 290 |
-
ch, ca = selection.split(".", 1)
|
| 291 |
-
query = f"SELECT * FROM [{main_table}] WHERE REPLACE(TRIM(CAST([{real_ch}] AS TEXT)), ' ', '') = ? AND REPLACE(TRIM(CAST([{real_cat}] AS TEXT)), ' ', '') = ?"
|
| 292 |
-
cursor.execute(query, (ch.replace(" ", ""), ca.replace(" ", "")))
|
| 293 |
else:
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
-
|
| 297 |
-
df
|
| 298 |
|
| 299 |
if display_setting:
|
| 300 |
cols_to_show = [c.strip() for c in display_setting.split(',')]
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
| 304 |
|
|
|
|
| 305 |
for col in df.columns:
|
| 306 |
df[col] = df[col].apply(convert_blob_to_html_img)
|
| 307 |
|
|
@@ -309,6 +316,8 @@ def fetch_database_records(standard, version, selection):
|
|
| 309 |
return df
|
| 310 |
|
| 311 |
except Exception as e:
|
|
|
|
|
|
|
| 312 |
return pd.DataFrame({"Error": [str(e)]})
|
| 313 |
|
| 314 |
def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, comp_cat):
|
|
|
|
| 245 |
conn = sqlite3.connect(db_path)
|
| 246 |
conn.text_factory = decode_sqlite_text
|
| 247 |
|
| 248 |
+
# 1. ํ์ฌ ์ด๋ค ์ข
๋ฅ์ ํ
์ด๋ธ์ ๋ณด๋ ค๋์ง ํ๋ณ (TableA, TableB_C ๋๋ Main)
|
| 249 |
+
# selection์ด "ALL"์ด๊ฑฐ๋ "2.12" ๊ฐ์ ํํ๋ฉด "Main"์ผ๋ก ๊ฐ์ฃผ
|
| 250 |
+
table_type = "Main" if selection == "ALL" or (selection and selection[0].isdigit() and "." in selection) else selection
|
| 251 |
+
|
| 252 |
+
# 2. Table_Config์์ ์ค์ ๊ฐ์ ธ์ค๊ธฐ (๊ฐ์ฅ ์ค์ํ ๋ถ๋ถ!)
|
| 253 |
+
anchor_col, display_setting = get_table_config(table_type)
|
| 254 |
+
|
| 255 |
tables = pd.read_sql("SELECT name FROM sqlite_master WHERE type='table';", conn)['name'].tolist()
|
| 256 |
main_table = f"{standard}_{version}"
|
|
|
|
| 257 |
if main_table not in tables:
|
| 258 |
main_table = tables[0]
|
| 259 |
|
|
|
|
| 269 |
target_table = t
|
| 270 |
break
|
| 271 |
|
| 272 |
+
# 3. ๋ฐ์ดํฐ ๋ก๋ ๋ก์ง
|
| 273 |
if target_table and target_table != main_table:
|
| 274 |
+
# ๋ถ์ ํ
์ด๋ธ(TableA ๋ฑ) ์ฒ๋ฆฌ
|
| 275 |
df = pd.read_sql(f"SELECT * FROM [{target_table}]", conn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
else:
|
| 277 |
+
# ๋ฉ์ธ ํ
์ด๋ธ ์ฒ๋ฆฌ (ALL ๋๋ ์นดํ
๊ณ ๋ฆฌ ํํฐ๋ง)
|
| 278 |
+
cursor = conn.cursor()
|
| 279 |
+
cursor.execute(f"PRAGMA table_info([{main_table}])")
|
| 280 |
+
cols_info = cursor.fetchall()
|
| 281 |
+
cols = [c[1] for c in cols_info]
|
| 282 |
+
lower_cols = [c.lower().strip() for c in cols]
|
| 283 |
+
|
| 284 |
+
real_ch, real_cat = None, None
|
| 285 |
+
for c, lc in zip(cols, lower_cols):
|
| 286 |
+
if lc == "chapter": real_ch = c
|
| 287 |
+
elif lc == "category": real_cat = c
|
| 288 |
+
|
| 289 |
+
if selection == "ALL":
|
| 290 |
+
cursor.execute(f"SELECT * FROM [{main_table}]")
|
| 291 |
+
elif "." in selection and real_ch and real_cat:
|
| 292 |
+
ch, ca = selection.split(".", 1)
|
| 293 |
+
query = f"SELECT * FROM [{main_table}] WHERE REPLACE(TRIM(CAST([{real_ch}] AS TEXT)), ' ', '') = ? AND REPLACE(TRIM(CAST([{real_cat}] AS TEXT)), ' ', '') = ?"
|
| 294 |
+
cursor.execute(query, (ch.replace(" ", ""), ca.replace(" ", "")))
|
| 295 |
+
else:
|
| 296 |
+
cursor.execute(f"SELECT * FROM [{main_table}]")
|
| 297 |
+
|
| 298 |
+
df = pd.DataFrame(cursor.fetchall(), columns=cols)
|
| 299 |
|
| 300 |
+
# 4. [์์ ๋จ] ์ค์ ์ ๋ฐ๋ฅธ ์ปฌ๋ผ ํํฐ๋ง (ํ๋์ฝ๋ฉ ์ ๊ฑฐ)
|
| 301 |
+
df = df.drop(columns=[c for c in df.columns if c.lower() == "version"], errors="ignore")
|
| 302 |
|
| 303 |
if display_setting:
|
| 304 |
cols_to_show = [c.strip() for c in display_setting.split(',')]
|
| 305 |
+
# Anchor(๊ธฐ์ค์ด)๊ฐ ๋ฐ์ดํฐ์ ์๋ค๋ฉด ๋ฐ๋์ ํฌํจ
|
| 306 |
+
if anchor_col in df.columns and anchor_col not in cols_to_show:
|
| 307 |
+
cols_to_show.insert(0, anchor_col)
|
| 308 |
+
# ์กด์ฌํ๋ ์ปฌ๋ผ๋ง ํํฐ๋ง
|
| 309 |
+
df = df[[c for c in cols_to_show if c in df.columns]]
|
| 310 |
|
| 311 |
+
# 5. ์ด๋ฏธ์ง ๋ ๋๋ง ๋ฐ ์ ๋ฆฌ
|
| 312 |
for col in df.columns:
|
| 313 |
df[col] = df[col].apply(convert_blob_to_html_img)
|
| 314 |
|
|
|
|
| 316 |
return df
|
| 317 |
|
| 318 |
except Exception as e:
|
| 319 |
+
import traceback
|
| 320 |
+
traceback.print_exc() # ์๋ฌ ์์ธ ์ถ์ ์ ์ํด ์ถ๊ฐ
|
| 321 |
return pd.DataFrame({"Error": [str(e)]})
|
| 322 |
|
| 323 |
def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, comp_cat):
|