QAway-to commited on
Commit
823e404
·
1 Parent(s): 8cab4b7

New style APP (Tab Changes v1.6)

Browse files
Files changed (1) hide show
  1. core/formatters.py +0 -95
core/formatters.py DELETED
@@ -1,95 +0,0 @@
1
- """
2
- 🇬🇧 Module: formatters.py
3
- Purpose: Cleanly format comparison output into a Bloomberg-style HTML table + narrative.
4
-
5
- 🇷🇺 Модуль: formatters.py
6
- Назначение: отображает сравнение портфелей в виде таблицы и аналитического блока в едином стиле.
7
- """
8
- import re
9
- from html import escape
10
-
11
-
12
- def build_comparison_table(metrics: list[dict]) -> str:
13
- """Builds a styled HTML table from list of metrics."""
14
- rows = []
15
- for m in metrics:
16
- name = escape(m["metric"])
17
- a = escape(m["a"])
18
- b = escape(m["b"])
19
- try:
20
- fa = float(a.replace("%", ""))
21
- fb = float(b.replace("%", ""))
22
- except Exception:
23
- fa, fb = None, None
24
-
25
- def color(v):
26
- if v is None:
27
- return "#e6edf3"
28
- return "#10b981" if v > 0 else "#ef4444" if v < 0 else "#e6edf3"
29
-
30
- def fmt(val, c):
31
- arrow = "▲" if c == "#10b981" else "▼" if c == "#ef4444" else "▪"
32
- return f"<span style='color:{c};font-weight:600'>{val} {arrow}</span>"
33
-
34
- ca, cb = color(fa), color(fb)
35
- rows.append(
36
- f"<tr>"
37
- f"<td style='padding:6px 8px;border-bottom:1px solid #30363d;text-align:left;color:#c9d1d9'>{name}</td>"
38
- f"<td style='padding:6px 8px;border-bottom:1px solid #30363d;text-align:center'>{fmt(a, ca)}</td>"
39
- f"<td style='padding:6px 8px;border-bottom:1px solid #30363d;text-align:center'>{fmt(b, cb)}</td>"
40
- f"</tr>"
41
- )
42
-
43
- html = (
44
- "<table style='width:100%;border-collapse:collapse;margin:12px 0;"
45
- "font-family:Inter,sans-serif;font-size:14px;'>"
46
- "<thead><tr>"
47
- "<th style='color:#9da5b4;text-align:left;padding:6px 8px;'>Metric</th>"
48
- "<th style='color:#9da5b4;text-align:center;'>Portfolio A</th>"
49
- "<th style='color:#9da5b4;text-align:center;'>Portfolio B</th>"
50
- "</tr></thead><tbody>"
51
- + "".join(rows)
52
- + "</tbody></table>"
53
- )
54
- return html
55
-
56
-
57
- def extract_metrics_from_text(raw: str) -> list[dict]:
58
- """Extracts metric rows like '| Annualized Return | 97.73% | -10.46% |'."""
59
- metrics = []
60
- for line in raw.splitlines():
61
- if "|" not in line:
62
- continue
63
- cols = [c.strip() for c in line.split("|") if c.strip()]
64
- if len(cols) == 3 and re.search(r"\d", cols[1]) and re.search(r"\d", cols[2]):
65
- metrics.append({"metric": cols[0], "a": cols[1], "b": cols[2]})
66
- return metrics
67
-
68
-
69
- def format_comparison_output(raw: str) -> str:
70
- """Transforms LLM comparison text into styled HTML."""
71
- if not raw:
72
- return "<i>No comparison data available.</i>"
73
-
74
- # Extract metric rows
75
- metrics = extract_metrics_from_text(raw)
76
-
77
- # Remove the original table to avoid duplication
78
- cleaned = re.sub(r"(\|.*\|\n)+", "", raw).strip()
79
-
80
- # Section styling
81
- cleaned = re.sub(r"###\s*1️⃣.*", "<h3 style='color:#93c5fd;'>1. Summary Table</h3>", cleaned)
82
- cleaned = re.sub(r"###\s*2️⃣.*", "<h3 style='color:#a78bfa;'>2. Comparative Analysis</h3>", cleaned)
83
- cleaned = re.sub(r"###\s*3️⃣.*", "<h3 style='color:#fbbf24;'>3. Recommendation</h3>", cleaned)
84
-
85
- # Build HTML
86
- html_table = build_comparison_table(metrics)
87
- cleaned = cleaned.replace("**", "").replace("\n", "<br>")
88
-
89
- return (
90
- "<div style='font-family:Inter,sans-serif;font-size:15px;line-height:1.6;color:#e6edf3;'>"
91
- "<h3 style='color:#93c5fd;margin-bottom:4px;'>1. Summary Table</h3>"
92
- f"{html_table}"
93
- f"<div style='margin-top:12px;'>{cleaned}</div>"
94
- "</div>"
95
- )