aboutsummaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rw-r--r--components/macro.py22
-rw-r--r--components/persistence.py62
2 files changed, 73 insertions, 11 deletions
diff --git a/components/macro.py b/components/macro.py
index a166855..c8e8b0c 100644
--- a/components/macro.py
+++ b/components/macro.py
@@ -15,12 +15,11 @@ _INDICES = {
"VIX": "^VIX",
}
-# (symbol, divisor) — yfinance encodes yields as rate×divisor
_YIELDS = {
- "3M": ("^IRX", 100),
- "5Y": ("^FVX", 10),
- "10Y": ("^TNX", 10),
- "30Y": ("^TYX", 10),
+ "3M": "^IRX",
+ "5Y": "^FVX",
+ "10Y": "^TNX",
+ "30Y": "^TYX",
}
_SECTORS = {
@@ -74,15 +73,15 @@ def _get_macro_data() -> dict:
indices[name] = None
yields = {}
- for label, (sym, div) in _YIELDS.items():
+ for label, sym in _YIELDS.items():
try:
hist = yf.Ticker(sym).history(period="5d")
if hist.empty:
yields[label] = None
continue
closes = hist["Close"].dropna()
- curr = float(closes.iloc[-1]) / div
- prev = float(closes.iloc[-2]) / div if len(closes) >= 2 else None
+ curr = float(closes.iloc[-1])
+ prev = float(closes.iloc[-2]) if len(closes) >= 2 else None
yields[label] = {"rate": curr, "change_1d": (curr - prev) if prev is not None else None}
except Exception:
yields[label] = None
@@ -176,8 +175,8 @@ def _render_index_table(indices: dict) -> None:
def _render_yield_curve(yields: dict) -> None:
labels = list(yields.keys())
- rates = [yields[l]["rate"] if yields[l] else None for l in labels]
- valid = [(l, r) for l, r in zip(labels, rates) if r is not None]
+ rates = [yields[lbl]["rate"] if yields[lbl] else None for lbl in labels]
+ valid = [(lbl, r) for lbl, r in zip(labels, rates) if r is not None]
fig = go.Figure()
if valid:
@@ -283,7 +282,8 @@ def _render_sector_heatmap(sectors: dict) -> None:
# ── Public entry point ────────────────────────────────────────────────────────
def render_macro() -> None:
- data = _get_macro_data()
+ with st.spinner("Loading macro data…"):
+ data = _get_macro_data()
st.markdown(
"<h5 style='font-family:\"IBM Plex Sans\",sans-serif;font-size:10px;font-weight:600;"
diff --git a/components/persistence.py b/components/persistence.py
new file mode 100644
index 0000000..552347d
--- /dev/null
+++ b/components/persistence.py
@@ -0,0 +1,62 @@
+import json
+import streamlit as st
+import streamlit.components.v1 as components
+from utils.security import escape_html
+
+
+def render_persistence_bridge() -> None:
+ """Sync watchlist + ticker to/from localStorage."""
+ restored_wl = st.session_state.get("_persist_wl", "")
+ restored_tk = st.session_state.get("_persist_tk", "")
+ if restored_wl and not st.session_state.get("_persist_loaded"):
+ try:
+ st.session_state["watchlist"] = json.loads(restored_wl)
+ except Exception:
+ pass
+ if restored_tk:
+ st.session_state["ticker"] = restored_tk
+ st.session_state["_persist_loaded"] = True
+ # No st.rerun() — the bridge runs at the top of the render cycle so the
+ # restored values propagate naturally to components below. Calling
+ # st.rerun() here would abort the render before click-receiver inputs
+ # (qt_click_receiver, wl_click_receiver) are registered, causing
+ # Streamlit to clear their pending values and swallow the click.
+
+ st.text_input("persist_wl", key="_persist_wl", label_visibility="collapsed")
+ st.text_input("persist_tk", key="_persist_tk", label_visibility="collapsed")
+
+ loaded = "1" if st.session_state.get("_persist_loaded") else "0"
+ wl_json = escape_html(json.dumps(st.session_state.get("watchlist", [])))
+ tk_val = escape_html(st.session_state.get("ticker") or "")
+
+ html = (
+ "<div id='d' data-loaded='" + loaded + "' data-wl='" + wl_json + "' data-tk='" + tk_val + "'></div>"
+ "<script>"
+ "(function() {"
+ " var d = document.getElementById('d');"
+ " function setInput(label, val) {"
+ " var inputs = window.parent.document.querySelectorAll('input[type=text]');"
+ " for (var i = 0; i < inputs.length; i++) {"
+ " if (inputs[i].getAttribute('aria-label') === label) {"
+ " var setter = Object.getOwnPropertyDescriptor(window.parent.HTMLInputElement.prototype, 'value').set;"
+ " setter.call(inputs[i], val);"
+ " inputs[i].dispatchEvent(new window.parent.Event('input', { bubbles: true }));"
+ " return;"
+ " }"
+ " }"
+ " }"
+ " if (d.dataset.loaded === '1') {"
+ " localStorage.setItem('prism_watchlist', d.dataset.wl);"
+ " if (d.dataset.tk) localStorage.setItem('prism_ticker', d.dataset.tk);"
+ " else localStorage.removeItem('prism_ticker');"
+ " } else {"
+ " var stored_wl = localStorage.getItem('prism_watchlist');"
+ " var stored_tk = localStorage.getItem('prism_ticker') || '';"
+ " if (!stored_wl) return;"
+ " setInput('persist_wl', stored_wl);"
+ " if (stored_tk) setInput('persist_tk', stored_tk);"
+ " }"
+ "})();"
+ "</script>"
+ )
+ components.html(html, height=0, scrolling=False)