Update index_live_html.py
Browse files- index_live_html.py +47 -35
index_live_html.py
CHANGED
|
@@ -2,19 +2,25 @@ from nsepython import *
|
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime as dt
|
| 4 |
|
| 5 |
-
# persist helpers (
|
| 6 |
from persist import exists, load, save
|
| 7 |
|
| 8 |
|
| 9 |
def build_index_live_html():
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# ================= LIVE FETCH =================
|
| 20 |
index_name = "NIFTY 50"
|
|
@@ -33,7 +39,7 @@ def build_index_live_html():
|
|
| 33 |
if not const_df.empty:
|
| 34 |
const_df = const_df.iloc[:, 1:]
|
| 35 |
|
| 36 |
-
move_to_info = [c for c in [
|
| 37 |
if move_to_info:
|
| 38 |
rem_df = pd.concat([rem_df, const_df[move_to_info].iloc[[0]]], axis=1)
|
| 39 |
const_df = const_df.drop(columns=move_to_info)
|
|
@@ -65,33 +71,38 @@ def build_index_live_html():
|
|
| 65 |
# ================= HTML HELPERS =================
|
| 66 |
def df_to_html_color(df, metric_col=None):
|
| 67 |
df_html = df.copy()
|
| 68 |
-
|
| 69 |
|
| 70 |
if metric_col and metric_col in df_html.columns:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
for idx, row in df_html.iterrows():
|
| 76 |
for col in df_html.columns:
|
| 77 |
val = row[col]
|
| 78 |
-
|
| 79 |
if isinstance(val, (int, float)):
|
| 80 |
val_fmt = f"{val:.2f}"
|
| 81 |
-
if val > 0:
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
if metric_col and col == metric_col:
|
| 84 |
-
if idx in
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
else:
|
| 88 |
df_html.at[idx, col] = str(val)
|
| 89 |
|
| 90 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
| 91 |
|
| 92 |
-
def
|
| 93 |
combined = pd.concat([rem_df, main_df], axis=1)
|
| 94 |
combined = combined.loc[:, ~combined.columns.duplicated()]
|
|
|
|
| 95 |
html = '<div class="mini-card-container">'
|
| 96 |
for col in combined.columns:
|
| 97 |
val = combined.at[0, col] if not combined.empty else ""
|
|
@@ -104,27 +115,31 @@ def build_index_live_html():
|
|
| 104 |
html += "</div>"
|
| 105 |
return html
|
| 106 |
|
| 107 |
-
info_cards_html =
|
| 108 |
cons_html = df_to_html_color(const_df)
|
| 109 |
|
| 110 |
-
metric_cols = [
|
| 111 |
-
|
|
|
|
|
|
|
| 112 |
|
|
|
|
| 113 |
for col in metric_cols:
|
| 114 |
if col not in const_df.columns:
|
| 115 |
continue
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
metric_tables += f"""
|
| 120 |
<div class="small-table">
|
| 121 |
<div class="st-title">{col}</div>
|
| 122 |
-
<div class="st-body">{df_to_html_color(
|
| 123 |
</div>
|
| 124 |
"""
|
| 125 |
|
| 126 |
# ================= FINAL HTML =================
|
| 127 |
-
|
| 128 |
<!DOCTYPE html>
|
| 129 |
<html>
|
| 130 |
<head>
|
|
@@ -149,7 +164,7 @@ th, td {{ border: 1px solid #bbb; padding: 5px 8px; }}
|
|
| 149 |
|
| 150 |
<h2>Live Index Data: NIFTY 50</h2>
|
| 151 |
|
| 152 |
-
<h3>Index Info
|
| 153 |
{info_cards_html}
|
| 154 |
|
| 155 |
<h3>Constituents</h3>
|
|
@@ -164,10 +179,7 @@ th, td {{ border: 1px solid #bbb; padding: 5px 8px; }}
|
|
| 164 |
</html>
|
| 165 |
"""
|
| 166 |
|
| 167 |
-
# ================= SAVE
|
| 168 |
-
save(
|
| 169 |
-
"date": today,
|
| 170 |
-
"html": html
|
| 171 |
-
})
|
| 172 |
|
| 173 |
-
return
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime as dt
|
| 4 |
|
| 5 |
+
# persist helpers (HF only)
|
| 6 |
from persist import exists, load, save
|
| 7 |
|
| 8 |
|
| 9 |
def build_index_live_html():
|
| 10 |
+
"""
|
| 11 |
+
Live HTML for NIFTY 50
|
| 12 |
+
- Intraday TTL (15 minutes)
|
| 13 |
+
- HTML only cache
|
| 14 |
+
- persist.py controls validity
|
| 15 |
+
"""
|
| 16 |
|
| 17 |
+
# ================= CACHE (TTL via persist) =================
|
| 18 |
+
cache_name = "INTRADAY_INDEX_LIVE_NIFTY50"
|
| 19 |
+
|
| 20 |
+
if exists(cache_name, "html"):
|
| 21 |
+
cached_html = load(cache_name, "html")
|
| 22 |
+
if isinstance(cached_html, str):
|
| 23 |
+
return cached_html
|
| 24 |
|
| 25 |
# ================= LIVE FETCH =================
|
| 26 |
index_name = "NIFTY 50"
|
|
|
|
| 39 |
if not const_df.empty:
|
| 40 |
const_df = const_df.iloc[:, 1:]
|
| 41 |
|
| 42 |
+
move_to_info = [c for c in ["segment", "equityTime", "preOpenTime"] if c in const_df.columns]
|
| 43 |
if move_to_info:
|
| 44 |
rem_df = pd.concat([rem_df, const_df[move_to_info].iloc[[0]]], axis=1)
|
| 45 |
const_df = const_df.drop(columns=move_to_info)
|
|
|
|
| 71 |
# ================= HTML HELPERS =================
|
| 72 |
def df_to_html_color(df, metric_col=None):
|
| 73 |
df_html = df.copy()
|
| 74 |
+
top_up, top_down = [], []
|
| 75 |
|
| 76 |
if metric_col and metric_col in df_html.columns:
|
| 77 |
+
col_num = pd.to_numeric(df_html[metric_col], errors="coerce").dropna()
|
| 78 |
+
top_up = col_num.nlargest(3).index.tolist()
|
| 79 |
+
top_down = col_num.nsmallest(3).index.tolist()
|
| 80 |
|
| 81 |
for idx, row in df_html.iterrows():
|
| 82 |
for col in df_html.columns:
|
| 83 |
val = row[col]
|
| 84 |
+
cls = ""
|
| 85 |
if isinstance(val, (int, float)):
|
| 86 |
val_fmt = f"{val:.2f}"
|
| 87 |
+
if val > 0:
|
| 88 |
+
cls = "numeric-positive"
|
| 89 |
+
elif val < 0:
|
| 90 |
+
cls = "numeric-negative"
|
| 91 |
if metric_col and col == metric_col:
|
| 92 |
+
if idx in top_up:
|
| 93 |
+
cls += " top-up"
|
| 94 |
+
elif idx in top_down:
|
| 95 |
+
cls += " top-down"
|
| 96 |
+
df_html.at[idx, col] = f'<span class="{cls.strip()}">{val_fmt}</span>'
|
| 97 |
else:
|
| 98 |
df_html.at[idx, col] = str(val)
|
| 99 |
|
| 100 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
| 101 |
|
| 102 |
+
def build_info_cards(rem_df, main_df):
|
| 103 |
combined = pd.concat([rem_df, main_df], axis=1)
|
| 104 |
combined = combined.loc[:, ~combined.columns.duplicated()]
|
| 105 |
+
|
| 106 |
html = '<div class="mini-card-container">'
|
| 107 |
for col in combined.columns:
|
| 108 |
val = combined.at[0, col] if not combined.empty else ""
|
|
|
|
| 115 |
html += "</div>"
|
| 116 |
return html
|
| 117 |
|
| 118 |
+
info_cards_html = build_info_cards(rem_df, main_df)
|
| 119 |
cons_html = df_to_html_color(const_df)
|
| 120 |
|
| 121 |
+
metric_cols = [
|
| 122 |
+
"pChange","totalTradedValue","nearWKH",
|
| 123 |
+
"nearWKL","perChange365d","perChange30d"
|
| 124 |
+
]
|
| 125 |
|
| 126 |
+
metric_tables = ""
|
| 127 |
for col in metric_cols:
|
| 128 |
if col not in const_df.columns:
|
| 129 |
continue
|
| 130 |
+
df_m = const_df[["symbol", col]].copy()
|
| 131 |
+
df_m[col] = pd.to_numeric(df_m[col], errors="coerce")
|
| 132 |
+
df_m = df_m.sort_values(col, ascending=False)
|
| 133 |
+
|
| 134 |
metric_tables += f"""
|
| 135 |
<div class="small-table">
|
| 136 |
<div class="st-title">{col}</div>
|
| 137 |
+
<div class="st-body">{df_to_html_color(df_m, col)}</div>
|
| 138 |
</div>
|
| 139 |
"""
|
| 140 |
|
| 141 |
# ================= FINAL HTML =================
|
| 142 |
+
html_out = f"""
|
| 143 |
<!DOCTYPE html>
|
| 144 |
<html>
|
| 145 |
<head>
|
|
|
|
| 164 |
|
| 165 |
<h2>Live Index Data: NIFTY 50</h2>
|
| 166 |
|
| 167 |
+
<h3>Index Info</h3>
|
| 168 |
{info_cards_html}
|
| 169 |
|
| 170 |
<h3>Constituents</h3>
|
|
|
|
| 179 |
</html>
|
| 180 |
"""
|
| 181 |
|
| 182 |
+
# ================= SAVE (HTML ONLY) =================
|
| 183 |
+
save(cache_name, html_out, "html")
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
return html_out
|