Update indices_html.py
Browse files- indices_html.py +158 -119
indices_html.py
CHANGED
|
@@ -2,188 +2,227 @@ import json
|
|
| 2 |
import pandas as pd
|
| 3 |
from nsepython import *
|
| 4 |
import html
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
def build_indices_html():
|
| 8 |
"""
|
| 9 |
-
Generate
|
| 10 |
-
- main
|
| 11 |
-
- dates table
|
| 12 |
-
-
|
| 13 |
-
-
|
| 14 |
-
|
| 15 |
-
Requires: indices() function available in scope which returns dict with keys:
|
| 16 |
-
- "data": DataFrame of records (each row a dict with fields including at least 'key', 'index', 'indexSymbol')
|
| 17 |
-
- "dates": DataFrame (optional)
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
| 20 |
data_df = p.get("data", pd.DataFrame())
|
| 21 |
dates_df = p.get("dates", pd.DataFrame())
|
| 22 |
|
| 23 |
-
# Convert records to list of dicts for iteration
|
| 24 |
records = data_df.to_dict(orient="records") if not data_df.empty else []
|
| 25 |
|
| 26 |
-
#
|
| 27 |
hidden_cols = {
|
| 28 |
"key","chartTodayPath","chart30dPath","chart30Path","chart365dPath",
|
| 29 |
"date365dAgo","date30dAgo","previousDay","oneWeekAgo","oneMonthAgoVal",
|
| 30 |
"oneWeekAgoVal","oneYearAgoVal","index","indicativeClose"
|
| 31 |
}
|
| 32 |
|
|
|
|
| 33 |
def build_table_from_records(recs, cols=None):
|
| 34 |
-
"""Return HTML <table> string for recs (list of dicts). If cols provided, use that order."""
|
| 35 |
if not recs:
|
| 36 |
return "<p>No data available.</p>"
|
| 37 |
|
| 38 |
-
# Determine columns
|
| 39 |
if cols is None:
|
| 40 |
-
# union of keys preserving insertion order from first record
|
| 41 |
cols = []
|
| 42 |
for r in recs:
|
| 43 |
for k in r.keys():
|
| 44 |
if k not in cols:
|
| 45 |
cols.append(k)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
for r in recs:
|
| 51 |
-
|
| 52 |
for c in cols:
|
| 53 |
v = r.get(c, "")
|
| 54 |
-
# Convert lists/dicts to string
|
| 55 |
if isinstance(v, (list, dict)):
|
| 56 |
-
|
| 57 |
-
else
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
def build_chart_grid_for_record(r):
|
| 66 |
"""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
If a path is missing, show a placeholder box with text.
|
| 70 |
"""
|
| 71 |
-
|
|
|
|
| 72 |
if src and isinstance(src, str) and src.strip():
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
f' {iframe_or_placeholder(month30_src, "30d Chart")}\n'
|
| 90 |
-
' </div>\n'
|
| 91 |
-
' <div class="chart-row">\n'
|
| 92 |
-
f' {iframe_or_placeholder(year365_src, "365d Chart")}\n'
|
| 93 |
-
' </div>\n'
|
| 94 |
-
'</div>\n'
|
| 95 |
)
|
| 96 |
-
return grid
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
main_table_html = build_table_from_records(records)
|
| 100 |
|
| 101 |
-
#
|
| 102 |
dates_table_html = ""
|
| 103 |
if not dates_df.empty:
|
| 104 |
dates_records = dates_df.to_dict(orient="records")
|
| 105 |
dates_table_html = build_table_from_records(dates_records)
|
| 106 |
|
| 107 |
-
#
|
| 108 |
from collections import defaultdict
|
| 109 |
groups = defaultdict(list)
|
| 110 |
for r in records:
|
| 111 |
-
|
| 112 |
-
groups[k].append(r)
|
| 113 |
|
| 114 |
-
# Build per-key tables and per-record chart grids
|
| 115 |
per_key_sections = []
|
|
|
|
|
|
|
| 116 |
for key_name, recs in groups.items():
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
cols = [c for c in first.keys() if c not in hidden_cols]
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 126 |
for c in cols:
|
| 127 |
-
if c not in
|
| 128 |
-
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
<section class="key-section">
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
</section>
|
| 140 |
-
"""
|
| 141 |
-
per_key_sections.append(section_html)
|
| 142 |
|
| 143 |
-
# CSS
|
| 144 |
css = """
|
| 145 |
<style>
|
| 146 |
-
body { font-family: Arial
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
th
|
| 150 |
-
|
| 151 |
-
.scroll { max-height: 420px; overflow: auto;
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
.
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
</style>
|
| 162 |
"""
|
| 163 |
|
| 164 |
-
# HTML
|
| 165 |
-
html_parts = [
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
html_parts.append(main_table_html)
|
| 176 |
-
html_parts.append("</div>")
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
html_parts.append("</div>")
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
|
| 187 |
-
|
|
|
|
| 188 |
|
| 189 |
-
return "\n".join(html_parts)
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from nsepython import *
|
| 4 |
import html
|
| 5 |
+
import html
|
| 6 |
+
import pandas as pd
|
| 7 |
|
| 8 |
+
def build_indices_html_nojs():
|
|
|
|
| 9 |
"""
|
| 10 |
+
Generate HTML:
|
| 11 |
+
- main table
|
| 12 |
+
- dates table
|
| 13 |
+
- tables for all categories
|
| 14 |
+
- charts ONLY for key == "INDICES ELIGIBLE IN DERIVATIVES"
|
| 15 |
+
- flexible chart layout (no grid, auto-fit)
|
|
|
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
+
|
| 18 |
+
p = indices() # your existing function
|
| 19 |
data_df = p.get("data", pd.DataFrame())
|
| 20 |
dates_df = p.get("dates", pd.DataFrame())
|
| 21 |
|
|
|
|
| 22 |
records = data_df.to_dict(orient="records") if not data_df.empty else []
|
| 23 |
|
| 24 |
+
# Columns to hide in category tables
|
| 25 |
hidden_cols = {
|
| 26 |
"key","chartTodayPath","chart30dPath","chart30Path","chart365dPath",
|
| 27 |
"date365dAgo","date30dAgo","previousDay","oneWeekAgo","oneMonthAgoVal",
|
| 28 |
"oneWeekAgoVal","oneYearAgoVal","index","indicativeClose"
|
| 29 |
}
|
| 30 |
|
| 31 |
+
# ----------- BASIC TABLE BUILDER -----------
|
| 32 |
def build_table_from_records(recs, cols=None):
|
|
|
|
| 33 |
if not recs:
|
| 34 |
return "<p>No data available.</p>"
|
| 35 |
|
|
|
|
| 36 |
if cols is None:
|
|
|
|
| 37 |
cols = []
|
| 38 |
for r in recs:
|
| 39 |
for k in r.keys():
|
| 40 |
if k not in cols:
|
| 41 |
cols.append(k)
|
| 42 |
|
| 43 |
+
header = "".join(f"<th>{html.escape(str(c))}</th>" for c in cols)
|
| 44 |
+
|
| 45 |
+
body_rows = []
|
| 46 |
for r in recs:
|
| 47 |
+
tds = []
|
| 48 |
for c in cols:
|
| 49 |
v = r.get(c, "")
|
|
|
|
| 50 |
if isinstance(v, (list, dict)):
|
| 51 |
+
v = str(v)
|
| 52 |
+
tds.append(f"<td>{html.escape('' if v is None else str(v))}</td>")
|
| 53 |
+
body_rows.append("<tr>" + "".join(tds) + "</tr>")
|
| 54 |
+
|
| 55 |
+
return f"""
|
| 56 |
+
<table>
|
| 57 |
+
<thead><tr>{header}</tr></thead>
|
| 58 |
+
<tbody>{''.join(body_rows)}</tbody>
|
| 59 |
+
</table>
|
| 60 |
+
"""
|
| 61 |
|
| 62 |
+
# ----------- FLEXIBLE CHART BLOCK -----------
|
| 63 |
def build_chart_grid_for_record(r):
|
| 64 |
"""
|
| 65 |
+
Flexible chart layout: auto-fit, no fixed grid.
|
| 66 |
+
ONLY for INDICES ELIGIBLE IN DERIVATIVES category.
|
|
|
|
| 67 |
"""
|
| 68 |
+
|
| 69 |
+
def iframe_if_exists(src, label):
|
| 70 |
if src and isinstance(src, str) and src.strip():
|
| 71 |
+
return f"""
|
| 72 |
+
<div class="chart-flex-item">
|
| 73 |
+
<iframe src="{html.escape(src)}" loading="lazy"
|
| 74 |
+
frameborder="0" title="{html.escape(label)}"></iframe>
|
| 75 |
+
</div>
|
| 76 |
+
"""
|
| 77 |
+
return ""
|
| 78 |
+
|
| 79 |
+
today_src = r.get("chartTodayPath") or r.get("chartToday") or ""
|
| 80 |
+
month30_src = r.get("chart30dPath") or r.get("chart30Path") or ""
|
| 81 |
+
year365_src = r.get("chart365dPath") or r.get("chart365") or ""
|
| 82 |
+
|
| 83 |
+
block = (
|
| 84 |
+
iframe_if_exists(today_src, "Today Chart") +
|
| 85 |
+
iframe_if_exists(month30_src, "30d Chart") +
|
| 86 |
+
iframe_if_exists(year365_src, "365d Chart")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
)
|
|
|
|
| 88 |
|
| 89 |
+
if not block.strip():
|
| 90 |
+
return ""
|
| 91 |
+
|
| 92 |
+
title = r.get("index") or r.get("indexSymbol") or r.get("symbol") or ""
|
| 93 |
+
|
| 94 |
+
return f"""
|
| 95 |
+
<div class="chart-flex-block">
|
| 96 |
+
<div class="chart-title"><strong>{html.escape(str(title))}</strong></div>
|
| 97 |
+
<div class="chart-flex-container">
|
| 98 |
+
{block}
|
| 99 |
+
</div>
|
| 100 |
+
</div>
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
# ----------- MAIN TABLE -----------
|
| 104 |
main_table_html = build_table_from_records(records)
|
| 105 |
|
| 106 |
+
# ----------- DATES TABLE -----------
|
| 107 |
dates_table_html = ""
|
| 108 |
if not dates_df.empty:
|
| 109 |
dates_records = dates_df.to_dict(orient="records")
|
| 110 |
dates_table_html = build_table_from_records(dates_records)
|
| 111 |
|
| 112 |
+
# ----------- GROUP BY KEY ----------
|
| 113 |
from collections import defaultdict
|
| 114 |
groups = defaultdict(list)
|
| 115 |
for r in records:
|
| 116 |
+
groups[r.get("key") or "UNCLASSIFIED"].append(r)
|
|
|
|
| 117 |
|
|
|
|
| 118 |
per_key_sections = []
|
| 119 |
+
|
| 120 |
+
# ----------- PER CATEGORY SECTIONS ----------
|
| 121 |
for key_name, recs in groups.items():
|
| 122 |
+
|
| 123 |
+
# Determine visible columns
|
| 124 |
+
first = recs[0]
|
| 125 |
cols = [c for c in first.keys() if c not in hidden_cols]
|
| 126 |
+
|
| 127 |
+
# Preferred order
|
| 128 |
+
preferred = ["indexSymbol", "index", "symbol", "name"]
|
| 129 |
+
ordered = []
|
| 130 |
+
for pkey in preferred:
|
| 131 |
+
if pkey in cols:
|
| 132 |
+
ordered.append(pkey)
|
| 133 |
for c in cols:
|
| 134 |
+
if c not in ordered:
|
| 135 |
+
ordered.append(c)
|
| 136 |
+
|
| 137 |
+
table_html = build_table_from_records(recs, ordered)
|
| 138 |
|
| 139 |
+
# CHARTS ONLY FOR INDICES ELIGIBLE IN DERIVATIVES
|
| 140 |
+
if str(key_name).strip().upper() == "INDICES ELIGIBLE IN DERIVATIVES":
|
| 141 |
+
charts_html = "\n".join(build_chart_grid_for_record(r) for r in recs)
|
| 142 |
+
else:
|
| 143 |
+
charts_html = "" # no charts for other categories
|
| 144 |
|
| 145 |
+
per_key_sections.append(f"""
|
| 146 |
<section class="key-section">
|
| 147 |
+
<h3>Category: {html.escape(str(key_name))} (Total: {len(recs)})</h3>
|
| 148 |
+
<div class="key-table">{table_html}</div>
|
| 149 |
+
{charts_html}
|
| 150 |
</section>
|
| 151 |
+
""")
|
|
|
|
| 152 |
|
| 153 |
+
# ----------- CSS -----------
|
| 154 |
css = """
|
| 155 |
<style>
|
| 156 |
+
body { font-family: Arial; padding: 16px; background: #fff; color: #111; }
|
| 157 |
+
table { border-collapse: collapse; width: 100%; margin-bottom: 14px; }
|
| 158 |
+
th, td { border: 1px solid #ccc; padding: 6px 8px; font-size: 13px; }
|
| 159 |
+
th { background: #007bff; color: white; position: sticky; top: 0; }
|
| 160 |
+
|
| 161 |
+
.scroll { max-height: 420px; overflow: auto; padding: 6px; background: #fafafa;
|
| 162 |
+
margin-bottom: 16px; border: 1px solid #ddd; }
|
| 163 |
+
|
| 164 |
+
.key-section {
|
| 165 |
+
border: 1px solid #e6eef6;
|
| 166 |
+
background: #fbfeff;
|
| 167 |
+
border-radius: 6px;
|
| 168 |
+
padding: 10px;
|
| 169 |
+
margin-bottom: 30px;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
/* FLEXIBLE CHART LAYOUT */
|
| 173 |
+
.chart-flex-block {
|
| 174 |
+
border: 1px solid #ddd;
|
| 175 |
+
background: #fff;
|
| 176 |
+
padding: 8px;
|
| 177 |
+
border-radius: 6px;
|
| 178 |
+
margin-bottom: 14px;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
.chart-title { margin-bottom: 6px; font-size: 14px; }
|
| 182 |
+
|
| 183 |
+
.chart-flex-container {
|
| 184 |
+
display: flex;
|
| 185 |
+
flex-wrap: wrap;
|
| 186 |
+
gap: 10px;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
.chart-flex-item {
|
| 190 |
+
flex: 1 1 300px;
|
| 191 |
+
min-height: 180px;
|
| 192 |
+
border: 1px solid #ccc;
|
| 193 |
+
border-radius: 6px;
|
| 194 |
+
overflow: hidden;
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
.chart-flex-item iframe {
|
| 198 |
+
width: 100%;
|
| 199 |
+
height: 100%;
|
| 200 |
+
border: 0;
|
| 201 |
+
}
|
| 202 |
</style>
|
| 203 |
"""
|
| 204 |
|
| 205 |
+
# ----------- FINAL HTML ASSEMBLY -----------
|
| 206 |
+
html_parts = [
|
| 207 |
+
"<!DOCTYPE html>",
|
| 208 |
+
"<html><head><meta charset='utf-8'><title>NSE Indices</title>",
|
| 209 |
+
css,
|
| 210 |
+
"</head><body>",
|
| 211 |
+
"<h1>NSE Indices — Full Static Render</h1>",
|
| 212 |
+
f"<div class='meta'>Generated: {html.escape(pd.Timestamp.now().strftime('%Y-%m-%d %H:%M:%S'))}</div>",
|
| 213 |
|
| 214 |
+
"<h2>Main Indices Table</h2>",
|
| 215 |
+
"<div class='scroll'>", main_table_html, "</div>",
|
|
|
|
|
|
|
| 216 |
|
| 217 |
+
"<h2>Dates / Meta</h2>" if dates_table_html else "",
|
| 218 |
+
"<div class='scroll'>" if dates_table_html else "",
|
| 219 |
+
dates_table_html,
|
| 220 |
+
"</div>" if dates_table_html else "",
|
|
|
|
| 221 |
|
| 222 |
+
"<h2>Categories</h2>",
|
| 223 |
+
*per_key_sections,
|
| 224 |
|
| 225 |
+
"</body></html>"
|
| 226 |
+
]
|
| 227 |
|
| 228 |
+
return "\n".join(str(x) for x in html_parts)
|