Update preopen_html.py
Browse files- preopen_html.py +48 -51
preopen_html.py
CHANGED
|
@@ -3,25 +3,24 @@ import pandas as pd
|
|
| 3 |
import re
|
| 4 |
from datetime import datetime as dt
|
| 5 |
|
| 6 |
-
# persist helpers (
|
| 7 |
from persist import exists, load, save
|
| 8 |
|
| 9 |
|
| 10 |
def build_preopen_html(key="NIFTY"):
|
| 11 |
"""
|
| 12 |
-
Build full Pre-Open HTML
|
| 13 |
-
|
| 14 |
-
|
| 15 |
"""
|
| 16 |
|
| 17 |
-
# ================= CACHE =================
|
| 18 |
-
|
| 19 |
-
cache_key = f"preopen_html_{key}"
|
| 20 |
|
| 21 |
-
if exists(
|
| 22 |
-
|
| 23 |
-
if isinstance(
|
| 24 |
-
return
|
| 25 |
|
| 26 |
# ================= FETCH DATA =================
|
| 27 |
p = nsefetch(f"https://www.nseindia.com/api/market-data-pre-open?key={key}")
|
|
@@ -50,55 +49,51 @@ def build_preopen_html(key="NIFTY"):
|
|
| 50 |
return "<i>No data</i>"
|
| 51 |
|
| 52 |
df_html = df.copy()
|
| 53 |
-
|
| 54 |
|
| 55 |
if metric_col and metric_col in df_html.columns:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
top3_down = col_numeric.nsmallest(3).index.tolist()
|
| 60 |
|
| 61 |
for idx, row in df_html.iterrows():
|
| 62 |
for col in df_html.columns:
|
| 63 |
val = row[col]
|
| 64 |
-
|
| 65 |
-
|
| 66 |
if isinstance(val, (int, float)):
|
| 67 |
val_fmt = f"{val:.2f}"
|
| 68 |
if val > 0:
|
| 69 |
-
|
| 70 |
elif val < 0:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
df_html.at[idx, col] = f'<span class="{style.strip()}">{val_fmt}</span>'
|
| 80 |
else:
|
| 81 |
df_html.at[idx, col] = str(val)
|
| 82 |
|
| 83 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
| 84 |
|
| 85 |
-
# ================= MINI CARDS =================
|
| 86 |
def build_info_cards(rem_df, main_df):
|
| 87 |
combined = pd.concat([rem_df, main_df], axis=1)
|
| 88 |
combined = combined.loc[:, ~combined.columns.duplicated()]
|
| 89 |
combined = remove_pattern_cols(combined)
|
| 90 |
|
| 91 |
-
|
| 92 |
for col in combined.columns:
|
| 93 |
val = combined.at[0, col] if not combined.empty else ""
|
| 94 |
-
|
| 95 |
<div class="mini-card">
|
| 96 |
<div class="card-key">{col}</div>
|
| 97 |
<div class="card-val">{val}</div>
|
| 98 |
</div>
|
| 99 |
"""
|
| 100 |
-
|
| 101 |
-
return
|
| 102 |
|
| 103 |
info_cards_html = build_info_cards(rem_df, main_df)
|
| 104 |
|
|
@@ -115,23 +110,23 @@ def build_preopen_html(key="NIFTY"):
|
|
| 115 |
|
| 116 |
metric_tables = ""
|
| 117 |
for col in metric_cols_allowed:
|
| 118 |
-
if col in const_df.columns
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
-
show_cols = ["symbol", col] if "symbol" in
|
| 124 |
metric_tables += f"""
|
| 125 |
<div class="small-table">
|
| 126 |
<div class="st-title">{col}</div>
|
| 127 |
<div class="st-body">
|
| 128 |
-
{df_to_html_color(
|
| 129 |
</div>
|
| 130 |
</div>
|
| 131 |
"""
|
| 132 |
|
| 133 |
# ================= FINAL HTML =================
|
| 134 |
-
|
| 135 |
<!DOCTYPE html>
|
| 136 |
<html>
|
| 137 |
<head>
|
|
@@ -142,13 +137,13 @@ h2, h3 {{ margin: 10px 0; }}
|
|
| 142 |
table {{ border-collapse: collapse; width: 100%; }}
|
| 143 |
th, td {{ border: 1px solid #bbb; padding: 6px; font-size: 13px; }}
|
| 144 |
th {{ background: #333; color: #fff; }}
|
| 145 |
-
.
|
| 146 |
-
.
|
| 147 |
-
.
|
| 148 |
-
.
|
| 149 |
.grid {{ display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }}
|
| 150 |
.small-table {{ background: #fff; padding: 8px; border-radius: 6px; border: 1px solid #ddd; }}
|
| 151 |
-
.st-title {{ text-align: center; font-weight: bold; background: #222; color: #fff; padding: 6px;
|
| 152 |
.st-body {{ max-height: 300px; overflow-y: auto; }}
|
| 153 |
.mini-card-container {{ display: flex; flex-wrap: wrap; gap: 10px; }}
|
| 154 |
.mini-card {{ background: #fff; padding: 8px 10px; border-radius: 6px; border: 1px solid #ddd; min-width: 120px; }}
|
|
@@ -156,23 +151,25 @@ th {{ background: #333; color: #fff; }}
|
|
| 156 |
</style>
|
| 157 |
</head>
|
| 158 |
<body>
|
|
|
|
| 159 |
<h2>Pre-Open Market β {key}</h2>
|
|
|
|
| 160 |
<h3>Info</h3>
|
| 161 |
{info_cards_html}
|
|
|
|
| 162 |
<h3>Constituents</h3>
|
| 163 |
{cons_html}
|
|
|
|
| 164 |
<h3>Key Metrics</h3>
|
| 165 |
<div class="grid">
|
| 166 |
{metric_tables}
|
| 167 |
</div>
|
|
|
|
| 168 |
</body>
|
| 169 |
</html>
|
| 170 |
"""
|
| 171 |
|
| 172 |
-
# ================= SAVE
|
| 173 |
-
save(
|
| 174 |
-
"date": today,
|
| 175 |
-
"html": html
|
| 176 |
-
})
|
| 177 |
|
| 178 |
-
return
|
|
|
|
| 3 |
import re
|
| 4 |
from datetime import datetime as dt
|
| 5 |
|
| 6 |
+
# persist helpers (HF only)
|
| 7 |
from persist import exists, load, save
|
| 8 |
|
| 9 |
|
| 10 |
def build_preopen_html(key="NIFTY"):
|
| 11 |
"""
|
| 12 |
+
Build full Pre-Open HTML
|
| 13 |
+
- Daily TTL (via persist.py)
|
| 14 |
+
- HTML only cache
|
| 15 |
"""
|
| 16 |
|
| 17 |
+
# ================= CACHE (TTL via persist) =================
|
| 18 |
+
cache_name = f"DAILY_PREOPEN_{key.upper()}"
|
|
|
|
| 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 |
# ================= FETCH DATA =================
|
| 26 |
p = nsefetch(f"https://www.nseindia.com/api/market-data-pre-open?key={key}")
|
|
|
|
| 49 |
return "<i>No data</i>"
|
| 50 |
|
| 51 |
df_html = df.copy()
|
| 52 |
+
top_up, top_down = [], []
|
| 53 |
|
| 54 |
if metric_col and metric_col in df_html.columns:
|
| 55 |
+
col_num = pd.to_numeric(df_html[metric_col], errors="coerce").dropna()
|
| 56 |
+
top_up = col_num.nlargest(3).index.tolist()
|
| 57 |
+
top_down = col_num.nsmallest(3).index.tolist()
|
|
|
|
| 58 |
|
| 59 |
for idx, row in df_html.iterrows():
|
| 60 |
for col in df_html.columns:
|
| 61 |
val = row[col]
|
| 62 |
+
cls = ""
|
|
|
|
| 63 |
if isinstance(val, (int, float)):
|
| 64 |
val_fmt = f"{val:.2f}"
|
| 65 |
if val > 0:
|
| 66 |
+
cls = "numeric-positive"
|
| 67 |
elif val < 0:
|
| 68 |
+
cls = "numeric-negative"
|
| 69 |
+
if metric_col and col == metric_col:
|
| 70 |
+
if idx in top_up:
|
| 71 |
+
cls += " top-up"
|
| 72 |
+
elif idx in top_down:
|
| 73 |
+
cls += " top-down"
|
| 74 |
+
df_html.at[idx, col] = f'<span class="{cls.strip()}">{val_fmt}</span>'
|
|
|
|
|
|
|
| 75 |
else:
|
| 76 |
df_html.at[idx, col] = str(val)
|
| 77 |
|
| 78 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
| 79 |
|
| 80 |
+
# ================= MINI INFO CARDS =================
|
| 81 |
def build_info_cards(rem_df, main_df):
|
| 82 |
combined = pd.concat([rem_df, main_df], axis=1)
|
| 83 |
combined = combined.loc[:, ~combined.columns.duplicated()]
|
| 84 |
combined = remove_pattern_cols(combined)
|
| 85 |
|
| 86 |
+
html = '<div class="mini-card-container">'
|
| 87 |
for col in combined.columns:
|
| 88 |
val = combined.at[0, col] if not combined.empty else ""
|
| 89 |
+
html += f"""
|
| 90 |
<div class="mini-card">
|
| 91 |
<div class="card-key">{col}</div>
|
| 92 |
<div class="card-val">{val}</div>
|
| 93 |
</div>
|
| 94 |
"""
|
| 95 |
+
html += '</div>'
|
| 96 |
+
return html
|
| 97 |
|
| 98 |
info_cards_html = build_info_cards(rem_df, main_df)
|
| 99 |
|
|
|
|
| 110 |
|
| 111 |
metric_tables = ""
|
| 112 |
for col in metric_cols_allowed:
|
| 113 |
+
if col in const_df.columns:
|
| 114 |
+
df_m = const_df.copy()
|
| 115 |
+
df_m[col] = pd.to_numeric(df_m[col], errors="coerce")
|
| 116 |
+
df_m = df_m.sort_values(col, ascending=False)
|
| 117 |
|
| 118 |
+
show_cols = ["symbol", col] if "symbol" in df_m.columns else [col]
|
| 119 |
metric_tables += f"""
|
| 120 |
<div class="small-table">
|
| 121 |
<div class="st-title">{col}</div>
|
| 122 |
<div class="st-body">
|
| 123 |
+
{df_to_html_color(df_m[show_cols], metric_col=col)}
|
| 124 |
</div>
|
| 125 |
</div>
|
| 126 |
"""
|
| 127 |
|
| 128 |
# ================= FINAL HTML =================
|
| 129 |
+
html_out = f"""
|
| 130 |
<!DOCTYPE html>
|
| 131 |
<html>
|
| 132 |
<head>
|
|
|
|
| 137 |
table {{ border-collapse: collapse; width: 100%; }}
|
| 138 |
th, td {{ border: 1px solid #bbb; padding: 6px; font-size: 13px; }}
|
| 139 |
th {{ background: #333; color: #fff; }}
|
| 140 |
+
.numeric-positive {{ color: green; font-weight: bold; }}
|
| 141 |
+
.numeric-negative {{ color: red; font-weight: bold; }}
|
| 142 |
+
.top-up {{ background: #b6f2b6; }}
|
| 143 |
+
.top-down {{ background: #f2b6b6; }}
|
| 144 |
.grid {{ display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }}
|
| 145 |
.small-table {{ background: #fff; padding: 8px; border-radius: 6px; border: 1px solid #ddd; }}
|
| 146 |
+
.st-title {{ text-align: center; font-weight: bold; background: #222; color: #fff; padding: 6px; }}
|
| 147 |
.st-body {{ max-height: 300px; overflow-y: auto; }}
|
| 148 |
.mini-card-container {{ display: flex; flex-wrap: wrap; gap: 10px; }}
|
| 149 |
.mini-card {{ background: #fff; padding: 8px 10px; border-radius: 6px; border: 1px solid #ddd; min-width: 120px; }}
|
|
|
|
| 151 |
</style>
|
| 152 |
</head>
|
| 153 |
<body>
|
| 154 |
+
|
| 155 |
<h2>Pre-Open Market β {key}</h2>
|
| 156 |
+
|
| 157 |
<h3>Info</h3>
|
| 158 |
{info_cards_html}
|
| 159 |
+
|
| 160 |
<h3>Constituents</h3>
|
| 161 |
{cons_html}
|
| 162 |
+
|
| 163 |
<h3>Key Metrics</h3>
|
| 164 |
<div class="grid">
|
| 165 |
{metric_tables}
|
| 166 |
</div>
|
| 167 |
+
|
| 168 |
</body>
|
| 169 |
</html>
|
| 170 |
"""
|
| 171 |
|
| 172 |
+
# ================= SAVE (HTML ONLY) =================
|
| 173 |
+
save(cache_name, html_out, "html")
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
return html_out
|