Spaces:
Runtime error

ditgun commited on
Commit
637f166
·
verified ·
1 Parent(s): e78c985

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +206 -276
src/app.py CHANGED
@@ -1217,16 +1217,14 @@ def _navigate_to_ticker(ticker, from_portfolio=True):
1217
 
1218
 
1219
  def render_grouped_alert_panel(prices, rate):
1220
- """
1221
- 우하단 고정 알람 패널.
1222
- - components.v1.html() → iframe 안에서 position:fixed + JS setInterval 카운트다운
1223
- - 탭/최소화/종목클릭 → window.parent.postMessage → Streamlit query_params 로 처리
1224
- """
1225
  TAB_CFG = {
1226
- "급변": {"icon": "⚡", "accent": "#FF6B35", "bg": "rgba(255,107,53,0.10)", "border": "rgba(255,107,53,0.30)"},
1227
- "매수": {"icon": "📈", "accent": "#00D4AA", "bg": "rgba(0,212,170,0.10)", "border": "rgba(0,212,170,0.30)"},
1228
- "익절": {"icon": "✅", "accent": "#4299E1", "bg": "rgba(66,153,225,0.10)", "border": "rgba(66,153,225,0.30)"},
1229
- "스톱로스": {"icon": "🛑", "accent": "#E53E3E", "bg": "rgba(229,62,62,0.10)", "border": "rgba(229,62,62,0.30)"},
1230
  }
1231
 
1232
  groups = _build_alert_groups(prices, rate)
@@ -1234,32 +1232,6 @@ def render_grouped_alert_panel(prices, rate):
1234
  if total_cnt == 0:
1235
  return
1236
 
1237
- # ── postMessage로 들어온 액션 처리 ──────────────────────
1238
- params = st.query_params
1239
- action = params.get("alert_action", "")
1240
- if action:
1241
- st.query_params.clear()
1242
- if action == "minimize":
1243
- st.session_state.alert_panel_minimized = True
1244
- st.rerun()
1245
- elif action == "open":
1246
- st.session_state.alert_panel_minimized = False
1247
- st.session_state.alert_panel_start_time = time.time()
1248
- st.rerun()
1249
- elif action.startswith("tab:"):
1250
- tab_name = action[4:]
1251
- if tab_name in TAB_CFG:
1252
- st.session_state.alert_active_tab = tab_name
1253
- st.rerun()
1254
- elif action.startswith("goto:"):
1255
- ticker = action[5:]
1256
- _navigate_to_ticker(ticker)
1257
- st.rerun()
1258
- elif action == "expired":
1259
- st.session_state.alert_panel_minimized = True
1260
- st.rerun()
1261
-
1262
- # ── 현재 상태 계산 ────────────────────────────────────────
1263
  elapsed = time.time() - st.session_state.alert_panel_start_time
1264
  time_left = max(0, 60 - int(elapsed))
1265
  if time_left == 0 and not st.session_state.alert_panel_minimized:
@@ -1268,263 +1240,221 @@ def render_grouped_alert_panel(prices, rate):
1268
  sl_cnt = len(groups["스톱로스"])
1269
  badge_color = "#E53E3E" if sl_cnt > 0 else "#FF6B35"
1270
  minimized = st.session_state.alert_panel_minimized
 
1271
 
1272
- # 활성 탭 결정
1273
  if st.session_state.alert_active_tab not in TAB_CFG:
1274
  for pt in ["스톱로스", "급변", "익절", "매수"]:
1275
- if groups[pt]:
1276
- st.session_state.alert_active_tab = pt
1277
- break
1278
- else:
1279
- st.session_state.alert_active_tab = "급변"
1280
  active_tab = st.session_state.alert_active_tab
1281
 
1282
- # ── 각 탭의 종목 HTML 미리 생성 ───────────────────────────
1283
- def make_items_html(tab_name):
1284
- tc = TAB_CFG[tab_name]
1285
- items = groups[tab_name]
1286
  if not items:
1287
- return '<div style="text-align:center;padding:24px 12px;color:rgba(255,255,255,0.2);font-size:13px;">알람 없음</div>'
1288
  rows = ""
1289
  for item in items:
1290
- pct = item["change_pct"]
1291
- pct_str = f"{'▲' if pct>=0 else '▼'} {abs(pct):.1f}%"
1292
- tick = item["ticker"]
1293
- nm = item["name"][:15] + ".." if len(item["name"]) > 15 else item["name"]
1294
- cur_p = f"{item['current_price']:,}"
1295
- rows += f"""
1296
- <div onclick="goTo('{tick}')" style="
1297
- display:flex;align-items:center;gap:10px;
1298
- padding:9px 11px;border-radius:9px;cursor:pointer;
1299
- border:1px solid transparent;margin-bottom:3px;
1300
- transition:background 0.15s;background:rgba(255,255,255,0.02);"
1301
- onmouseover="this.style.background='rgba(255,255,255,0.06)'"
1302
- onmouseout="this.style.background='rgba(255,255,255,0.02)'">
1303
- <div style="
1304
- width:38px;height:38px;border-radius:8px;flex-shrink:0;
1305
- background:{tc['bg']};border:1px solid {tc['border']};
1306
- display:flex;align-items:center;justify-content:center;
1307
- font-size:9px;font-weight:800;color:{tc['accent']};
1308
- font-family:'DM Mono',monospace;">{tick[:4]}</div>
1309
- <div style="flex:1;min-width:0;">
1310
- <div style="font-size:12px;font-weight:700;color:#eef2f7;">
1311
- {tick} <span style="font-size:10px;color:rgba(255,255,255,0.3);font-weight:400;">{nm}</span>
1312
- </div>
1313
- <div style="font-size:11px;color:rgba(255,255,255,0.3);">{cur_p}원</div>
1314
- </div>
1315
- <div style="text-align:right;flex-shrink:0;">
1316
- <div style="font-size:12px;font-weight:700;color:{tc['accent']};">{pct_str}</div>
1317
- <div style="font-size:10px;color:rgba(255,255,255,0.2);margin-top:1px;">→ 분석</div>
1318
- </div>
1319
- </div>"""
1320
  return rows
1321
 
1322
- all_items_html = {tab: make_items_html(tab) for tab in TAB_CFG}
1323
-
1324
- # ── 탭 버튼 HTML ─────────────────────────────────────────
1325
- def make_tabs_html():
1326
- html = ""
1327
- for tab_name, tc in TAB_CFG.items():
1328
- cnt = len(groups[tab_name])
1329
- is_act = tab_name == active_tab
1330
- bg = tc["bg"] if is_act else "transparent"
1331
- bd = tc["border"] if is_act else "rgba(255,255,255,0.07)"
1332
- lc = tc["accent"] if is_act else "rgba(255,255,255,0.35)"
1333
- bb = f"border-bottom:2px solid {tc['accent']};" if is_act else ""
1334
- bg_badge = tc["accent"] if cnt > 0 else "rgba(255,255,255,0.15)"
1335
- html += f"""
1336
- <div onclick="switchTab('{tab_name}')" style="
1337
- flex:1;background:{bg};border:1px solid {bd};{bb}
1338
- border-radius:8px 8px 0 0;padding:7px 3px;cursor:pointer;
1339
- display:flex;flex-direction:column;align-items:center;gap:2px;
1340
- transition:all 0.15s;"
1341
- onmouseover="if('{tab_name}'!==currentTab) this.style.background='rgba(255,255,255,0.05)'"
1342
- onmouseout="if('{tab_name}'!==currentTab) this.style.background='transparent'">
1343
- <span style="font-size:14px;">{tc['icon']}</span>
1344
- <span style="font-size:9px;font-weight:700;color:{lc};">{tab_name}</span>
1345
- <span style="background:{bg_badge};color:#fff;border-radius:8px;
1346
- min-width:16px;height:16px;display:flex;align-items:center;
1347
- justify-content:center;font-size:9px;font-weight:700;padding:0 4px;">{cnt}</span>
1348
- </div>"""
1349
- return html
1350
-
1351
- # ── 전체 HTML (iframe 안에서 position:fixed) ─────────────
1352
- items_json = json.dumps({k: all_items_html[k] for k in TAB_CFG}, ensure_ascii=False)
1353
-
1354
- html = f"""<!DOCTYPE html>
1355
- <html>
1356
- <head>
1357
- <meta charset="utf-8">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1358
  <style>
1359
- * {{ margin:0;padding:0;box-sizing:border-box;font-family:'Noto Sans KR',sans-serif; }}
1360
- body {{ background:transparent;overflow:hidden; }}
1361
-
1362
- @keyframes pulse {{ 0%,100%{{opacity:1}}50%{{opacity:0.3}} }}
1363
- @keyframes slideUp {{ from{{transform:translateY(16px);opacity:0}} to{{transform:translateY(0);opacity:1}} }}
1364
- @keyframes badgePop {{ from{{transform:scale(0.7);opacity:0}} to{{transform:scale(1);opacity:1}} }}
1365
-
1366
- #panel {{
1367
- position:fixed;bottom:72px;right:14px;
1368
- width:min(96vw,360px);
1369
- z-index:99999;
1370
- animation:slideUp 0.35s cubic-bezier(0.34,1.56,0.64,1);
1371
- }}
1372
- #badge {{
1373
- position:fixed;bottom:72px;right:14px;
1374
- display:flex;align-items:center;gap:8px;
1375
- background:linear-gradient(135deg,#1a2035,#131820);
1376
- border:1px solid {badge_color};border-radius:40px;
1377
- padding:9px 18px;cursor:pointer;z-index:99999;
1378
- box-shadow:0 6px 24px rgba(0,0,0,0.5);
1379
- animation:badgePop 0.35s cubic-bezier(0.34,1.56,0.64,1);
1380
- }}
1381
- #badge:hover {{ border-color:white; }}
1382
- .panel-header {{
1383
- background:linear-gradient(135deg,#1a2035 0%,#131820 100%);
1384
- border:1px solid rgba(255,255,255,0.09);
1385
- border-bottom:none;border-radius:14px 14px 0 0;
1386
- padding:12px 14px 0;
1387
- }}
1388
- .tabs-row {{ display:flex;gap:3px;margin-top:10px; }}
1389
- .body-wrap {{
1390
- background:linear-gradient(180deg,#161c2a 0%,#111620 100%);
1391
- border:1px solid rgba(255,255,255,0.09);
1392
- border-radius:0 0 14px 14px;
1393
- max-height:240px;overflow-y:auto;padding:6px;
1394
- }}
1395
- .body-wrap::-webkit-scrollbar{{ width:3px; }}
1396
- .body-wrap::-webkit-scrollbar-thumb{{ background:rgba(255,255,255,0.1);border-radius:3px; }}
1397
- .minimize-btn {{
1398
- width:100%;margin-top:5px;padding:5px;
1399
- background:rgba(255,255,255,0.04);border:1px solid rgba(255,255,255,0.08);
1400
- border-radius:8px;color:rgba(255,255,255,0.35);font-size:11px;
1401
- cursor:pointer;transition:all 0.15s;
1402
- }}
1403
- .minimize-btn:hover{{ background:rgba(255,255,255,0.08);color:rgba(255,255,255,0.7); }}
1404
  </style>
1405
- </head>
1406
- <body>
1407
-
1408
- <!-- ── 배지 모드 ── -->
1409
- <div id="badge" style="display:{'flex' if minimized else 'none'};" onclick="openPanel()">
1410
- <span style="font-size:18px;position:relative;">
1411
- 🔔
1412
- <span style="position:absolute;top:-5px;right:-8px;
1413
- background:{badge_color};color:#fff;border-radius:10px;
1414
- font-size:9px;font-weight:700;min-width:16px;height:16px;
1415
- display:flex;align-items:center;justify-content:center;padding:0 3px;
1416
- font-family:'DM Mono',monospace;">{total_cnt}</span>
1417
- </span>
1418
- <span style="font-size:12px;font-weight:700;color:#e8eaf0;">알람 {total_cnt}개</span>
1419
- <span style="width:6px;height:6px;border-radius:50%;background:{badge_color};
1420
- animation:pulse 1.5s infinite;display:inline-block;"></span>
1421
- </div>
1422
-
1423
- <!-- ── 패널 모드 ── -->
1424
- <div id="panel" style="display:{'block' if not minimized else 'none'};">
1425
- <div class="panel-header">
1426
- <!-- 헤더 행 -->
1427
- <div style="display:flex;align-items:center;justify-content:space-between;">
1428
- <div style="display:flex;align-items:center;gap:7px;">
1429
- <div style="width:7px;height:7px;border-radius:50%;background:#FF6B35;animation:pulse 1.5s infinite;"></div>
1430
- <span style="color:#eef2f7;font-size:12px;font-weight:700;letter-spacing:0.06em;">LIVE ALERTS</span>
1431
- <span style="background:rgba(229,62,62,0.15);border:1px solid rgba(229,62,62,0.3);
1432
- border-radius:5px;padding:1px 7px;font-size:10px;color:#E53E3E;font-weight:700;">{total_cnt}</span>
1433
- </div>
1434
- <!-- 타이머: JS가 직접 DOM 업데이트 -->
1435
- <div style="background:rgba(255,255,255,0.05);border-radius:7px;padding:3px 9px;
1436
- display:flex;align-items:center;gap:4px;">
1437
- <span style="font-size:10px;">⏱</span>
1438
- <span id="timer-text" style="font-size:11px;font-weight:700;
1439
- font-family:'DM Mono',monospace;color:#4A5568;">{time_left}s</span>
1440
- </div>
1441
- </div>
1442
- <!-- 탭 -->
1443
- <div class="tabs-row" id="tabs-row">
1444
- {make_tabs_html()}
1445
- </div>
1446
- </div>
1447
-
1448
- <!-- 종목 바디 -->
1449
- <div class="body-wrap" id="items-body">
1450
- {all_items_html[active_tab]}
1451
- </div>
1452
-
1453
- <button class="minimize-btn" onclick="minimize()">− 최소화</button>
1454
- </div>
1455
 
 
 
1456
  <script>
1457
- var timeLeft = {time_left};
1458
- var minimized = {'true' if minimized else 'false'};
1459
- var currentTab = '{active_tab}';
1460
- var allItems = {items_json};
1461
-
1462
- // ── 실시간 카운트다운 ──────────────────────────
1463
- var timerEl = document.getElementById('timer-text');
1464
- var timerInterval = setInterval(function() {{
1465
- if (minimized) return;
1466
- timeLeft--;
1467
- if (timeLeft <= 0) {{
1468
- clearInterval(timerInterval);
1469
- minimize();
1470
- sendAction('expired');
1471
- return;
1472
- }}
1473
- if (timerEl) {{
1474
- timerEl.textContent = timeLeft + 's';
1475
- if (timeLeft <= 15) timerEl.style.color = '#E53E3E';
1476
- else if (timeLeft <= 30) timerEl.style.color = '#EAB308';
1477
- else timerEl.style.color = '#4A5568';
1478
- }}
1479
- }}, 1000);
1480
-
1481
- // ── URL 파라미터로 Streamlit에 액션 전달 ──────
1482
- function sendAction(action) {{
1483
- var url = window.parent.location.href.split('?')[0];
1484
- window.parent.history.replaceState(null, '', url + '?alert_action=' + encodeURIComponent(action));
1485
- // Streamlit 리렌더 트리거 (input 이벤트)
1486
- var ev = new Event('streamlit:setQueryString', {{bubbles: true}});
1487
- window.parent.document.dispatchEvent(ev);
1488
- }}
1489
-
1490
- function minimize() {{
1491
- minimized = true;
1492
- document.getElementById('panel').style.display = 'none';
1493
- document.getElementById('badge').style.display = 'flex';
1494
- sendAction('minimize');
1495
- }}
1496
-
1497
- function openPanel() {{
1498
- minimized = false;
1499
- timeLeft = 60;
1500
- if (timerEl) timerEl.textContent = '60s';
1501
- document.getElementById('badge').style.display = 'none';
1502
- document.getElementById('panel').style.display = 'block';
1503
- sendAction('open');
1504
- }}
1505
-
1506
- function switchTab(tabName) {{
1507
- currentTab = tabName;
1508
- document.getElementById('items-body').innerHTML = allItems[tabName];
1509
- // 탭 재렌더 없이 Streamlit에만 알림
1510
- sendAction('tab:' + tabName);
1511
- }}
1512
-
1513
- function goTo(ticker) {{
1514
- sendAction('goto:' + ticker);
1515
- }}
1516
  </script>
1517
- </body>
1518
- </html>"""
1519
-
1520
- # iframe 높이: 최소화면 작게, 패널이면 종목 수에 따라
1521
- if minimized:
1522
- iframe_h = 60
1523
- else:
1524
- item_cnt = len(groups[active_tab])
1525
- iframe_h = 360 + max(0, item_cnt - 2) * 52
1526
 
1527
- components.html(html, height=iframe_h, scrolling=False)
1528
 
1529
 
1530
  # ============================================================
 
1217
 
1218
 
1219
  def render_grouped_alert_panel(prices, rate):
1220
+ """우하단 플로팅 알람 패널 — components.html iframe을 CSS fixed로 올림"""
1221
+ import streamlit.components.v1 as components
1222
+
 
 
1223
  TAB_CFG = {
1224
+ "급변": {"icon": "⚡", "accent": "#FF6B35", "bg": "rgba(255,107,53,0.12)", "border": "rgba(255,107,53,0.35)"},
1225
+ "매수": {"icon": "📈", "accent": "#00D4AA", "bg": "rgba(0,212,170,0.12)", "border": "rgba(0,212,170,0.35)"},
1226
+ "익절": {"icon": "✅", "accent": "#4299E1", "bg": "rgba(66,153,225,0.12)", "border": "rgba(66,153,225,0.35)"},
1227
+ "스톱로스": {"icon": "🛑", "accent": "#E53E3E", "bg": "rgba(229,62,62,0.12)", "border": "rgba(229,62,62,0.35)"},
1228
  }
1229
 
1230
  groups = _build_alert_groups(prices, rate)
 
1232
  if total_cnt == 0:
1233
  return
1234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1235
  elapsed = time.time() - st.session_state.alert_panel_start_time
1236
  time_left = max(0, 60 - int(elapsed))
1237
  if time_left == 0 and not st.session_state.alert_panel_minimized:
 
1240
  sl_cnt = len(groups["스톱로스"])
1241
  badge_color = "#E53E3E" if sl_cnt > 0 else "#FF6B35"
1242
  minimized = st.session_state.alert_panel_minimized
1243
+ timer_col = "#E53E3E" if time_left <= 15 else ("#EAB308" if time_left <= 30 else "#8B98B4")
1244
 
 
1245
  if st.session_state.alert_active_tab not in TAB_CFG:
1246
  for pt in ["스톱로스", "급변", "익절", "매수"]:
1247
+ if groups[pt]: st.session_state.alert_active_tab = pt; break
1248
+ else: st.session_state.alert_active_tab = "급변"
 
 
 
1249
  active_tab = st.session_state.alert_active_tab
1250
 
1251
+ def make_items(tab):
1252
+ tc = TAB_CFG[tab]
1253
+ items = groups[tab]
 
1254
  if not items:
1255
+ return '<p style="text-align:center;padding:20px;color:rgba(255,255,255,0.2);font-size:13px;margin:0;">알람 없음</p>'
1256
  rows = ""
1257
  for item in items:
1258
+ pct = item["change_pct"]
1259
+ sign = "&#9650;" if pct >= 0 else "&#9660;"
1260
+ ps = sign + " " + str(round(abs(pct), 1)) + "%"
1261
+ tick = item["ticker"]
1262
+ nm = (item["name"][:14] + "..") if len(item["name"]) > 14 else item["name"]
1263
+ cp = format(item["current_price"], ",") + "&#xC6D0;"
1264
+ rows += (
1265
+ '<div onclick="goTo(\'' + tick + '\')" '
1266
+ 'style="display:flex;align-items:center;gap:10px;padding:9px 11px;'
1267
+ 'border-radius:9px;cursor:pointer;margin-bottom:3px;background:rgba(255,255,255,0.02);" '
1268
+ 'onmouseover="this.style.background=\'rgba(255,255,255,0.07)\'" '
1269
+ 'onmouseout="this.style.background=\'rgba(255,255,255,0.02)\'">'
1270
+ '<div style="width:38px;height:38px;border-radius:8px;flex-shrink:0;'
1271
+ 'background:' + tc["bg"] + ';border:1px solid ' + tc["border"] + ';'
1272
+ 'display:flex;align-items:center;justify-content:center;'
1273
+ 'font-size:9px;font-weight:800;color:' + tc["accent"] + ';">' + tick[:4] + '</div>'
1274
+ '<div style="flex:1;min-width:0;">'
1275
+ '<div style="font-size:12px;font-weight:700;color:#eef2f7;">' + tick +
1276
+ ' <span style="font-size:10px;color:rgba(255,255,255,0.3);">' + nm + '</span></div>'
1277
+ '<div style="font-size:11px;color:rgba(255,255,255,0.3);">' + cp + '</div>'
1278
+ '</div>'
1279
+ '<div style="text-align:right;flex-shrink:0;">'
1280
+ '<div style="font-size:12px;font-weight:700;color:' + tc["accent"] + ';">' + ps + '</div>'
1281
+ '<div style="font-size:10px;color:rgba(255,255,255,0.2);">&#8594; &#xBD84;&#xC11D;</div>'
1282
+ '</div></div>'
1283
+ )
 
 
 
 
1284
  return rows
1285
 
1286
+ all_items = {tab: make_items(tab) for tab in TAB_CFG}
1287
+
1288
+ tabs_html = ""
1289
+ for tab, tc in TAB_CFG.items():
1290
+ cnt = len(groups[tab])
1291
+ is_act = tab == active_tab
1292
+ bg = tc["bg"] if is_act else "transparent"
1293
+ bd = tc["border"] if is_act else "rgba(255,255,255,0.07)"
1294
+ lc = tc["accent"] if is_act else "rgba(255,255,255,0.35)"
1295
+ bb = ("border-bottom:2px solid " + tc["accent"] + ";") if is_act else ""
1296
+ bg_b = tc["accent"] if cnt > 0 else "rgba(255,255,255,0.15)"
1297
+ tabs_html += (
1298
+ '<div onclick="switchTab(\'' + tab + '\')" '
1299
+ 'style="flex:1;background:' + bg + ';border:1px solid ' + bd + ';' + bb +
1300
+ 'border-radius:8px 8px 0 0;padding:7px 3px;cursor:pointer;'
1301
+ 'display:flex;flex-direction:column;align-items:center;gap:2px;">'
1302
+ '<span style="font-size:14px;">' + tc["icon"] + '</span>'
1303
+ '<span style="font-size:9px;font-weight:700;color:' + lc + ';">' + tab + '</span>'
1304
+ '<span style="background:' + bg_b + ';color:#fff;border-radius:8px;'
1305
+ 'min-width:16px;height:16px;display:flex;align-items:center;'
1306
+ 'justify-content:center;font-size:9px;font-weight:700;padding:0 4px;">' + str(cnt) + '</span>'
1307
+ '</div>'
1308
+ )
1309
+
1310
+ items_json = json.dumps(all_items, ensure_ascii=False)
1311
+ init_min = "true" if minimized else "false"
1312
+
1313
+ html = (
1314
+ "<!DOCTYPE html><html><head><meta charset='utf-8'>"
1315
+ "<style>"
1316
+ "* { margin:0; padding:0; box-sizing:border-box; font-family:'Noto Sans KR',sans-serif; }"
1317
+ "html, body { background:transparent; overflow:hidden; width:100%; height:100%; }"
1318
+ "@keyframes apP { 0%,100%{opacity:1} 50%{opacity:.3} }"
1319
+ "@keyframes apS { from{transform:translateY(14px);opacity:0} to{transform:none;opacity:1} }"
1320
+ "@keyframes apB { from{transform:scale(.7);opacity:0} to{transform:none;opacity:1} }"
1321
+ "#panel {"
1322
+ "position:fixed; bottom:4px; right:4px;"
1323
+ "width:356px;"
1324
+ "z-index:9999;"
1325
+ "animation:apS .35s cubic-bezier(.34,1.56,.64,1);"
1326
+ "}"
1327
+ "#badge {"
1328
+ "position:fixed; bottom:4px; right:4px;"
1329
+ "display:flex; align-items:center; gap:8px;"
1330
+ "background:linear-gradient(135deg,#1a2035,#131820);"
1331
+ "border:1px solid " + badge_color + "; border-radius:40px;"
1332
+ "padding:9px 18px; cursor:pointer; z-index:9999;"
1333
+ "box-shadow:0 6px 24px rgba(0,0,0,.6);"
1334
+ "animation:apB .35s cubic-bezier(.34,1.56,.64,1);"
1335
+ "}"
1336
+ "#badge:hover { border-color:#fff; }"
1337
+ ".body { max-height:240px; overflow-y:auto; padding:6px; }"
1338
+ ".body::-webkit-scrollbar { width:3px; }"
1339
+ ".body::-webkit-scrollbar-thumb { background:rgba(255,255,255,.15); border-radius:3px; }"
1340
+ "</style></head><body>"
1341
+
1342
+ # 배지
1343
+ "<div id='badge' style='display:" + ("flex" if minimized else "none") + ";'>"
1344
+ "<span style='font-size:18px;position:relative;line-height:1;'>&#x1F514;"
1345
+ "<span style='position:absolute;top:-5px;right:-8px;background:" + badge_color + ";color:#fff;"
1346
+ "border-radius:10px;font-size:9px;font-weight:700;min-width:16px;height:16px;"
1347
+ "display:flex;align-items:center;justify-content:center;padding:0 3px;'>" + str(total_cnt) + "</span>"
1348
+ "</span>"
1349
+ "<span style='font-size:12px;font-weight:700;color:#e8eaf0;'>&#xC54C;&#xB78C; " + str(total_cnt) + "&#xAC1C;</span>"
1350
+ "<span style='width:6px;height:6px;border-radius:50%;background:" + badge_color + ";"
1351
+ "animation:apP 1.5s infinite;display:inline-block;'></span>"
1352
+ "</div>"
1353
+
1354
+ # 패널
1355
+ "<div id='panel' style='display:" + ("none" if minimized else "block") + ";'>"
1356
+ "<div style='background:linear-gradient(135deg,#1a2035,#131820);"
1357
+ "border:1px solid rgba(255,255,255,.09);border-bottom:none;border-radius:14px 14px 0 0;padding:12px 14px 0;'>"
1358
+ "<div style='display:flex;align-items:center;justify-content:space-between;margin-bottom:10px;'>"
1359
+ "<div style='display:flex;align-items:center;gap:7px;'>"
1360
+ "<div style='width:7px;height:7px;border-radius:50%;background:#FF6B35;animation:apP 1.5s infinite;'></div>"
1361
+ "<span style='color:#eef2f7;font-size:12px;font-weight:700;letter-spacing:.06em;'>LIVE ALERTS</span>"
1362
+ "<span style='background:rgba(229,62,62,.15);border:1px solid rgba(229,62,62,.3);"
1363
+ "border-radius:5px;padding:1px 7px;font-size:10px;color:#E53E3E;font-weight:700;'>" + str(total_cnt) + "</span>"
1364
+ "</div>"
1365
+ "<div style='background:rgba(255,255,255,.05);border-radius:7px;padding:3px 9px;"
1366
+ "display:flex;align-items:center;gap:4px;'>"
1367
+ "<span>&#x23F1;</span>"
1368
+ "<span id='timer' style='font-size:11px;font-weight:700;font-family:DM Mono,monospace;color:" + timer_col + ";'>" + str(time_left) + "s</span>"
1369
+ "</div>"
1370
+ "</div>"
1371
+ "<div style='display:flex;gap:3px;'>" + tabs_html + "</div>"
1372
+ "</div>"
1373
+ "<div class='body' style='background:linear-gradient(180deg,#161c2a,#111620);"
1374
+ "border:1px solid rgba(255,255,255,.09);border-radius:0 0 14px 14px;' id='body'>"
1375
+ + all_items[active_tab] +
1376
+ "</div>"
1377
+ "<button onclick='minimize()' style='width:100%;margin-top:4px;padding:6px;"
1378
+ "background:rgba(255,255,255,.04);border:1px solid rgba(255,255,255,.08);"
1379
+ "border-radius:8px;color:rgba(255,255,255,.4);font-size:11px;cursor:pointer;'>&#x2212; &#xCD5C;&#xC18C;&#xD654;</button>"
1380
+ "</div>"
1381
+
1382
+ "<script>"
1383
+ "var M=" + init_min + ", T=" + str(time_left) + ", TAB=" + json.dumps(active_tab) + ";"
1384
+ "var DATA=" + items_json + ";"
1385
+ "var te=document.getElementById('timer');"
1386
+
1387
+ "var iv=setInterval(function(){"
1388
+ "if(M)return; T--;"
1389
+ "if(T<=0){clearInterval(iv);minimize();send('expired');return;}"
1390
+ "if(te){te.textContent=T+'s';"
1391
+ "te.style.color=T<=15?'#E53E3E':T<=30?'#EAB308':'#8B98B4';}"
1392
+ "},1000);"
1393
+
1394
+ "function send(a){"
1395
+ "window.parent.postMessage({type:'alert_action',action:a},'*');"
1396
+ "}"
1397
+ "function minimize(){"
1398
+ "M=true;"
1399
+ "document.getElementById('panel').style.display='none';"
1400
+ "document.getElementById('badge').style.display='flex';"
1401
+ "send('minimize');"
1402
+ "}"
1403
+ "function openPanel(){"
1404
+ "M=false; T=" + str(time_left) + ";"
1405
+ "if(te)te.textContent=T+'s';"
1406
+ "document.getElementById('badge').style.display='none';"
1407
+ "document.getElementById('panel').style.display='block';"
1408
+ "send('open');"
1409
+ "}"
1410
+ "function switchTab(t){"
1411
+ "TAB=t;"
1412
+ "document.getElementById('body').innerHTML=DATA[t];"
1413
+ "send('tab:'+t);"
1414
+ "}"
1415
+ "function goTo(tk){ send('goto:'+tk); }"
1416
+ "document.getElementById('badge').onclick=openPanel;"
1417
+ "</script>"
1418
+ "</body></html>"
1419
+ )
1420
+
1421
+ # CSS로 components.html iframe을 fixed 오버레이로 변환
1422
+ st.markdown("""
1423
  <style>
1424
+ /* components.html iframe → 우하단 고정 오버레이 */
1425
+ [data-testid="stCustomComponentV1"] iframe,
1426
+ iframe[title="components.v1.html"] {
1427
+ position: fixed !important;
1428
+ bottom: 72px !important;
1429
+ right: 0 !important;
1430
+ width: 380px !important;
1431
+ height: 500px !important;
1432
+ border: none !important;
1433
+ z-index: 2147483640 !important;
1434
+ background: transparent !important;
1435
+ pointer-events: auto !important;
1436
+ }
1437
+ /* iframe이 페이지 흐름에서 차지하는 공간 제거 */
1438
+ [data-testid="stCustomComponentV1"] {
1439
+ height: 0 !important;
1440
+ overflow: visible !important;
1441
+ display: block !important;
1442
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1443
  </style>
1444
+ """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1445
 
1446
+ # postMessage 수신 → query_params로 Streamlit 상태 전달
1447
+ st.markdown("""
1448
  <script>
1449
+ window.addEventListener('message', function(e) {
1450
+ if (!e.data || e.data.type !== 'alert_action') return;
1451
+ var base = window.location.href.split('?')[0];
1452
+ window.location.href = base + '?alert_action=' + encodeURIComponent(e.data.action);
1453
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1454
  </script>
1455
+ """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
1456
 
1457
+ components.html(html, height=0, scrolling=False)
1458
 
1459
 
1460
  # ============================================================