1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
"""Top Movers component — day gainers, losers, most active."""
from html import escape
import streamlit as st
import yfinance as yf
DEFAULT_VISIBLE_MOVERS = 3
MAX_MOVERS = 8
def _toggle_mover_tab(state_key: str):
st.session_state[state_key] = not st.session_state.get(state_key, False)
def _inject_styles(compact: bool = False):
row_template = (
"minmax(48px, 0.9fr) minmax(56px, 1fr) minmax(60px, 1fr)"
if compact
else "minmax(72px, 0.8fr) minmax(0, 2.6fr) minmax(90px, 1fr) minmax(110px, 1.1fr)"
)
row_gap = "0.3rem" if compact else "0.85rem"
name_display = "none" if compact else "block"
row_padding = "0.12rem 0" if compact else "0.18rem 0"
symbol_size = "0.75rem" if compact else "0.875rem"
name_size = "0.75rem" if compact else "0.8125rem"
price_size = "0.75rem" if compact else "0.8125rem"
change_size = "0.75rem" if compact else "0.8125rem"
change_meta_size = "10px" if compact else "11px"
st.markdown(
"""
<style>
.prism-mover-list {{
display: grid;
gap: 0.12rem;
}}
.prism-mover-row {{
display: grid;
grid-template-columns: {row_template};
gap: {row_gap};
align-items: center;
padding: {row_padding};
}}
.prism-mover-symbol {{
font-family: 'IBM Plex Sans', sans-serif;
font-size: {symbol_size};
font-weight: 600;
color: #F2ECDC;
line-height: 1.1;
}}
.prism-mover-name {{
display: {name_display};
font-family: 'IBM Plex Sans', sans-serif;
color: #8E8676;
font-size: {name_size};
line-height: 1.15;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}}
.prism-mover-price {{
font-family: 'IBM Plex Mono', monospace;
font-variant-numeric: tabular-nums;
font-size: {price_size};
color: #C7C0AE;
line-height: 1.1;
}}
.prism-mover-change {{
font-family: 'IBM Plex Mono', monospace;
font-variant-numeric: tabular-nums;
font-size: {change_size};
font-weight: 500;
line-height: 1.1;
}}
.prism-mover-change-meta {{
font-family: 'IBM Plex Mono', monospace;
font-size: {change_meta_size};
color: #5E5849;
margin-left: 0.2rem;
}}
@media (max-width: 900px) {{
.prism-mover-row {{
grid-template-columns: minmax(68px, 0.9fr) minmax(0, 2.2fr) minmax(82px, 1fr) minmax(96px, 1fr);
gap: 0.55rem;
}}
}}
</style>
""".format(
row_template=row_template,
row_gap=row_gap,
row_padding=row_padding,
symbol_size=symbol_size,
name_size=name_size,
name_display=name_display,
price_size=price_size,
change_size=change_size,
change_meta_size=change_meta_size,
),
unsafe_allow_html=True,
)
@st.cache_data(ttl=180)
def _fetch_movers(screen: str, count: int = MAX_MOVERS) -> list[dict]:
try:
result = yf.screen(screen, count=count)
return result.get("quotes", [])
except Exception:
return []
def _fmt_price(val) -> str:
try:
return f"${float(val):,.2f}"
except Exception:
return "—"
def _mover_row_html(q: dict) -> str:
symbol = escape(str(q.get("symbol", "")))
name = escape(str(q.get("shortName") or q.get("longName") or symbol))
price = q.get("regularMarketPrice")
chg_pct = q.get("regularMarketChangePercent")
chg_abs = q.get("regularMarketChange")
try:
chg_f = float(chg_pct)
color = "#4F8C5E" if chg_f >= 0 else "#B5494B"
sign = "+" if chg_f >= 0 else ""
pct_str = f"{sign}{chg_f:.2f}%"
except Exception:
color = "#9aa0b0"
pct_str = "—"
try:
abs_str = f"({'+' if float(chg_abs) >= 0 else ''}{float(chg_abs):.2f})"
except Exception:
abs_str = ""
abs_str = escape(abs_str)
return (
"<div class='prism-mover-row'>"
f"<div class='prism-mover-symbol'>{symbol}</div>"
f"<div class='prism-mover-name'>{name}</div>"
f"<div class='prism-mover-price'>{_fmt_price(price)}</div>"
"<div>"
f"<span class='prism-mover-change' style='color:{color}'>{pct_str}</span>"
f"<span class='prism-mover-change-meta'>{abs_str}</span>"
"</div>"
"</div>"
)
def _render_mover_tab(screen: str, state_key: str):
quotes = _fetch_movers(screen)
if not quotes:
st.caption("No data available.")
return
expanded = st.session_state.get(state_key, False)
visible_count = len(quotes) if expanded else min(DEFAULT_VISIBLE_MOVERS, len(quotes))
rows_html = "".join(_mover_row_html(q) for q in quotes[:visible_count])
st.markdown(f"<div class='prism-mover-list'>{rows_html}</div>", unsafe_allow_html=True)
if len(quotes) > DEFAULT_VISIBLE_MOVERS:
button_label = "Show Less" if expanded else f"Show More ({len(quotes) - DEFAULT_VISIBLE_MOVERS} more)"
st.button(
button_label,
key=f"{state_key}_button",
width="stretch",
on_click=_toggle_mover_tab,
args=(state_key,),
)
@st.fragment
def render_top_movers(compact: bool = False):
_inject_styles(compact=compact)
st.markdown("""
<div style="
font-family:'IBM Plex Sans',sans-serif;
font-size:10px;font-weight:600;
text-transform:uppercase;letter-spacing:0.14em;
color:#5E5849;margin-bottom:8px;
">Top Movers</div>
""", unsafe_allow_html=True)
tab_gainers, tab_losers, tab_active = st.tabs(["Gainers", "Losers", "Most Active"])
screens = {
"gainers": "day_gainers",
"losers": "day_losers",
"active": "most_actives",
}
with tab_gainers:
if compact:
quotes = _fetch_movers(screens["gainers"])
if not quotes:
st.caption("No data available.")
else:
rows_html = "".join(_mover_row_html(q) for q in quotes[:DEFAULT_VISIBLE_MOVERS])
st.markdown(f"<div class='prism-mover-list'>{rows_html}</div>", unsafe_allow_html=True)
else:
_render_mover_tab(screens["gainers"], "top_movers_gainers_expanded")
with tab_losers:
if compact:
quotes = _fetch_movers(screens["losers"])
if not quotes:
st.caption("No data available.")
else:
rows_html = "".join(_mover_row_html(q) for q in quotes[:DEFAULT_VISIBLE_MOVERS])
st.markdown(f"<div class='prism-mover-list'>{rows_html}</div>", unsafe_allow_html=True)
else:
_render_mover_tab(screens["losers"], "top_movers_losers_expanded")
with tab_active:
if compact:
quotes = _fetch_movers(screens["active"])
if not quotes:
st.caption("No data available.")
else:
rows_html = "".join(_mover_row_html(q) for q in quotes[:DEFAULT_VISIBLE_MOVERS])
st.markdown(f"<div class='prism-mover-list'>{rows_html}</div>", unsafe_allow_html=True)
else:
_render_mover_tab(screens["active"], "top_movers_active_expanded")
|