# -*- coding: utf-8 -*-
"""前端追加「量化推荐」区(筛选按钮 + 事件渲染 + 表格 + 权重展示)"""
import io
p = r'src\web\index.html'
s = io.open(p, encoding='utf-8').read()
# 1) 筛选按钮
old1 = ' '
new1 = (old1 + '\n \n'
' ')
assert old1 in s, 'filters not found'
s = s.replace(old1, new1)
# 2) 主区追加量化推荐表格
old2 = '
\n'
new2 = (' \n\n'
'\n'
' 量化模型 · 自适应推荐'
'(多因子 + IC 微调)
\n'
' '
'权重:加载中…
\n'
' \n'
'
\n'
' \n'
' | 代码 | 名称 | \n'
' 现价 | 模型分 | \n'
' 购入区间 | '
'预计收益(回测口径) | \n'
' 推荐指数 | '
'简易原因 | \n'
'
\n'
'
\n'
'
\n'
' '
'⚠ 购入区间与预计收益为模型回测/推测口径,不构成投资建议。
\n'
'')
assert old2 in s, 'feed ul not found'
s = s.replace(old2, new2)
# 3) WS 事件渲染分支(量化推荐)
old3 = (" } else {\n"
" const txt = typeof d === 'object' ? JSON.stringify(d) : String(d)\n"
" li.innerHTML = `${e.ts}${e.kind}"
"${esc(txt)}`\n }")
new3 = (" } else if (e.kind === '量化推荐') {\n"
" renderQuant(e.data)\n"
" const txt = '自适应模型推送 ' + ((d.list || []).length) + ' 只推荐'\n"
" li.innerHTML = `${e.ts}${e.kind}"
"${esc(txt)}`\n"
" } else {\n"
" const txt = typeof d === 'object' ? JSON.stringify(d) : String(d)\n"
" li.innerHTML = `${e.ts}${e.kind}"
"${esc(txt)}`\n }")
assert old3 in s, 'render branch not found'
s = s.replace(old3, new3)
# 4) 渲染函数与加载
old4 = 'connect()'
new4 = (
"function renderQuant(d) {\n"
" const tb = document.querySelector('#qtable tbody')\n"
" if (!tb) return\n"
" tb.innerHTML = (d.list || []).map(r =>\n"
" '| ' + esc(r.code) + ' | ' +\n"
" '' + esc(r.name) + ' | ' +\n"
" '' + r.price + ' | ' +\n"
" '' + r.score + ' | ' +\n"
" '' + r.buy_low + ' ~ ' + r.buy_high + ' | ' +\n"
" '' + (r.expected_return_pct == null ? '—' : r.expected_return_pct + '%') + ' | ' +\n"
" '' + '★'.repeat(r.stars) + ' | ' +\n"
" '' + esc(r.reason) + ' |
').join('')\n"
"}\n"
"function loadQuant() {\n"
" fetch('/api/quant/recommendations').then(r => r.json()).then(renderQuant).catch(() => {})\n"
" fetch('/api/quant/weights').then(r => r.json()).then(w => {\n"
" document.getElementById('qw').textContent =\n"
" Object.entries(w || {}).map(([k, v]) => k + '=' + Number(v).toFixed(2)).join(' ') || '—'\n"
" }).catch(() => {})\n"
"}\n"
"loadQuant()\n"
"setInterval(loadQuant, 60000)\n"
"connect()")
assert old4 in s
s = s.replace(old4, new4, 1)
io.open(p, 'w', encoding='utf-8').write(s)
print('index.html 量化推荐区 OK')