QIDNLF commited on
Commit
d0879fb
ยท
verified ยท
1 Parent(s): 342f4f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -32
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
- cursor.execute(f"SELECT * FROM [{main_table}]")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
 
296
- df = pd.DataFrame(cursor.fetchall(), columns=cols)
297
- df.columns = [c.lower().strip() for c in df.columns]
298
 
299
  if display_setting:
300
  cols_to_show = [c.strip() for c in display_setting.split(',')]
301
- if anchor_col not in cols_to_show:
302
- cols_to_show.insert(0, anchor_col)
303
- df = df[cols_to_show]
 
 
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):