import pandas as pd from nsepython import * import html from datetime import datetime as dt from collections import defaultdict # persist helpers (HF only) from persist import exists, load, save def build_indices_html(): """ Generates full static HTML for NSE Indices - Tables for all indices - Category-wise tables - Charts ONLY for 'INDICES ELIGIBLE IN DERIVATIVES' - DAILY cache via persist.py (HTML only) """ # ================= CACHE (TTL handled by persist) ================= cache_name = "DAILY_INDICES_HTML" if exists(cache_name, "html"): cached_html = load(cache_name, "html") if isinstance(cached_html, str): return cached_html # ================= FETCH DATA ================= p = indices() data_df = p.get("data", pd.DataFrame()) dates_df = p.get("dates", pd.DataFrame()) records = data_df.to_dict(orient="records") if not data_df.empty else [] hidden_cols = { "key", "chartTodayPath", "chart30dPath", "chart30Path", "chart365dPath", "date365dAgo", "date30dAgo", "previousDay", "oneWeekAgo", "oneMonthAgoVal", "oneWeekAgoVal", "oneYearAgoVal", "index", "indicativeClose" } # ================= TABLE BUILDER ================= def build_table(recs, cols=None): if not recs: return "

No data available.

" if cols is None: cols = [] for r in recs: for k in r: if k not in cols: cols.append(k) header = "".join(f"{html.escape(str(c))}" for c in cols) body = [] for r in recs: tds = [] for c in cols: v = r.get(c, "") if isinstance(v, (dict, list)): v = str(v) tds.append(f"{html.escape(str(v) if v is not None else '')}") body.append("" + "".join(tds) + "") return f""" {header}{''.join(body)}
""" # ================= CHART BUILDER ================= def build_chart_block(r): def iframe(src, label): if src and isinstance(src, str): return f"""
""" return "" charts = ( iframe(r.get("chartTodayPath"), "Today") + iframe(r.get("chart30dPath"), "30 Days") + iframe(r.get("chart365dPath"), "1 Year") ) if not charts.strip(): return "" title = r.get("indexSymbol") or r.get("index") or r.get("symbol") or "" return f"""
{html.escape(title)}
{charts}
""" # ================= MAIN TABLE ================= main_table_html = build_table(records) # ================= DATES TABLE ================= dates_table_html = "" if not dates_df.empty: dates_table_html = build_table( dates_df.to_dict(orient="records") ) # ================= GROUP BY CATEGORY ================= groups = defaultdict(list) for r in records: groups[r.get("key") or "UNCLASSIFIED"].append(r) sections = [] for key_name, recs in groups.items(): first = recs[0] cols = [c for c in first if c not in hidden_cols] preferred = ["indexSymbol", "index", "symbol", "name"] ordered_cols = ( [c for c in preferred if c in cols] + [c for c in cols if c not in preferred] ) table_html = build_table(recs, ordered_cols) charts_html = "" if str(key_name).strip().upper() == "INDICES ELIGIBLE IN DERIVATIVES": charts_html = "\n".join(build_chart_block(r) for r in recs) sections.append(f"""

{html.escape(str(key_name))} (Total: {len(recs)})

{table_html} {charts_html}
""") # ================= CSS ================= css = """ """ # ================= FINAL HTML ================= html_out = "\n".join([ "", "", "", "", "NSE Indices", css, "", "", "

NSE Indices

", f"
Generated: {dt.now().strftime('%Y-%m-%d %H:%M:%S')}
", "

Main Indices

", "
", main_table_html, "
", "

Dates

" if dates_table_html else "", "
" if dates_table_html else "", dates_table_html, "
" if dates_table_html else "", "

Categories

", *sections, "" ]) # ================= SAVE HTML (HF only) ================= save(cache_name, html_out, "html") return html_out