Update build_index_live_html.py
Browse files- build_index_live_html.py +0 -247
build_index_live_html.py
CHANGED
|
@@ -1,251 +1,4 @@
|
|
| 1 |
-
'''
|
| 2 |
-
from nsepython import *
|
| 3 |
-
import pandas as pd
|
| 4 |
-
|
| 5 |
-
def build_index_live_html(name=""):
|
| 6 |
-
p = nse_index_live(name)
|
| 7 |
-
|
| 8 |
-
full_df = p.get("data", pd.DataFrame())
|
| 9 |
-
rem_df = p.get("rem", pd.DataFrame())
|
| 10 |
-
|
| 11 |
-
if full_df.empty:
|
| 12 |
-
main_df = pd.DataFrame()
|
| 13 |
-
const_df = pd.DataFrame()
|
| 14 |
-
else:
|
| 15 |
-
main_df = full_df.iloc[[0]]
|
| 16 |
-
const_df = full_df.iloc[1:] # Constituents
|
| 17 |
-
if not const_df.empty:
|
| 18 |
-
const_df = const_df.iloc[:, 1:] # Remove first column
|
| 19 |
-
|
| 20 |
-
# Columns to move from constituents to info
|
| 21 |
-
move_to_info = [c for c in ['segment', 'equityTime', 'preOpenTime'] if c in const_df.columns]
|
| 22 |
-
if move_to_info:
|
| 23 |
-
rem_df = pd.concat([rem_df, const_df[move_to_info].iloc[[0]]], axis=1)
|
| 24 |
-
const_df = const_df.drop(columns=move_to_info)
|
| 25 |
-
|
| 26 |
-
# Drop unnecessary columns from Constituents
|
| 27 |
-
drop_cols_const = [
|
| 28 |
-
"identifier", "ffmc", "stockIndClosePrice", "lastUpdateTime",
|
| 29 |
-
"chartTodayPath", "chart30dPath", "chart365dPath", "series",
|
| 30 |
-
"symbol_meta", "activeSeries", "debtSeries", "isFNOSec",
|
| 31 |
-
"isCASec", "isSLBSec", "isDebtSec", "isSuspended",
|
| 32 |
-
"tempSuspendedSeries", "isETFSec", "isDelisted",
|
| 33 |
-
"slb_isin", "isMunicipalBond", "isHybridSymbol", "QuotePreOpenFlag"
|
| 34 |
-
]
|
| 35 |
-
const_df = const_df.drop(columns=[c for c in drop_cols_const if c in const_df.columns])
|
| 36 |
-
|
| 37 |
-
# Drop unnecessary columns from Main Data
|
| 38 |
-
drop_cols_main = [
|
| 39 |
-
"series", "symbol_meta", "companyName", "industry", "activeSeries", "debtSeries",
|
| 40 |
-
"isFNOSec", "isCASec", "isSLBSec", "isDebtSec", "isSuspended", "tempSuspendedSeries",
|
| 41 |
-
"isETFSec", "isDelisted", "isin", "slb_isin", "listingDate", "isMunicipalBond",
|
| 42 |
-
"isHybridSymbol", "segment", "equityTime", "preOpenTime", "QuotePreOpenFlag"
|
| 43 |
-
]
|
| 44 |
-
main_df = main_df.drop(columns=[c for c in drop_cols_main if c in main_df.columns])
|
| 45 |
-
|
| 46 |
-
# Ensure pChange is numeric and sort
|
| 47 |
-
if 'pChange' in const_df.columns:
|
| 48 |
-
const_df['pChange'] = pd.to_numeric(const_df['pChange'], errors='coerce')
|
| 49 |
-
const_df = const_df.sort_values('pChange', ascending=False)
|
| 50 |
-
|
| 51 |
-
# ================= HELPER FUNCTION: COLOR-CODE AND FORMAT NUMERIC =================
|
| 52 |
-
def df_to_html_color(df, metric_col=None):
|
| 53 |
-
df_html = df.copy()
|
| 54 |
-
top3_up = []
|
| 55 |
-
top3_down = []
|
| 56 |
-
if metric_col and metric_col in df_html.columns and pd.api.types.is_numeric_dtype(df_html[metric_col]):
|
| 57 |
-
col_numeric = df_html[metric_col].dropna()
|
| 58 |
-
top3_up = col_numeric.nlargest(3).index.tolist()
|
| 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 |
-
style = ""
|
| 65 |
-
if pd.api.types.is_numeric_dtype(type(val)) or isinstance(val, (int, float)):
|
| 66 |
-
val_fmt = f"{val:.2f}"
|
| 67 |
-
if val > 0:
|
| 68 |
-
style = "numeric-positive"
|
| 69 |
-
elif val < 0:
|
| 70 |
-
style = "numeric-negative"
|
| 71 |
-
if metric_col and col == metric_col:
|
| 72 |
-
if idx in top3_up:
|
| 73 |
-
style += " top-up"
|
| 74 |
-
elif idx in top3_down:
|
| 75 |
-
style += " top-down"
|
| 76 |
-
df_html.at[idx, col] = f'<span class="{style.strip()}">{val_fmt}</span>'
|
| 77 |
-
else:
|
| 78 |
-
df_html.at[idx, col] = str(val)
|
| 79 |
-
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
| 80 |
-
|
| 81 |
-
rem_html = df_to_html_color(rem_df)
|
| 82 |
-
main_html = df_to_html_color(main_df)
|
| 83 |
-
cons_html = df_to_html_color(const_df)
|
| 84 |
-
|
| 85 |
-
# ================= METRIC TABLES =================
|
| 86 |
-
metric_cols = [
|
| 87 |
-
"pChange", "totalTradedValue", "nearWKH", "nearWKL",
|
| 88 |
-
"perChange365d", "perChange30d"
|
| 89 |
-
]
|
| 90 |
-
|
| 91 |
-
metric_tables = ""
|
| 92 |
-
for col in metric_cols:
|
| 93 |
-
if col not in const_df.columns:
|
| 94 |
-
continue
|
| 95 |
-
|
| 96 |
-
df_const = const_df.copy()
|
| 97 |
-
df_const[col] = pd.to_numeric(df_const[col], errors="ignore")
|
| 98 |
-
df_const = df_const.sort_values(col, ascending=False)
|
| 99 |
-
df_html = df_to_html_color(df_const[['symbol', col]], metric_col=col)
|
| 100 |
-
|
| 101 |
-
metric_tables += f"""
|
| 102 |
-
<div class="small-table">
|
| 103 |
-
<div class="st-title">{col}</div>
|
| 104 |
-
<div class="st-body">{df_html}</div>
|
| 105 |
-
</div>
|
| 106 |
-
"""
|
| 107 |
-
|
| 108 |
-
# ================= FINAL HTML =================
|
| 109 |
-
html = f"""
|
| 110 |
-
<!DOCTYPE html>
|
| 111 |
-
<html>
|
| 112 |
-
<head>
|
| 113 |
-
<meta charset="UTF-8">
|
| 114 |
|
| 115 |
-
<style>
|
| 116 |
-
body {{
|
| 117 |
-
font-family: Arial;
|
| 118 |
-
margin: 12px;
|
| 119 |
-
background: #f5f5f5;
|
| 120 |
-
color: #222;
|
| 121 |
-
font-size: 14px;
|
| 122 |
-
}}
|
| 123 |
-
|
| 124 |
-
h2, h3 {{
|
| 125 |
-
margin: 12px 0 6px 0;
|
| 126 |
-
font-weight: 600;
|
| 127 |
-
}}
|
| 128 |
-
|
| 129 |
-
table {{
|
| 130 |
-
border-collapse: collapse;
|
| 131 |
-
width: 100%;
|
| 132 |
-
table-layout: auto;
|
| 133 |
-
}}
|
| 134 |
-
|
| 135 |
-
th, td {{
|
| 136 |
-
border: 1px solid #bbb;
|
| 137 |
-
padding: 5px 8px;
|
| 138 |
-
text-align: left;
|
| 139 |
-
font-size: 13px;
|
| 140 |
-
}}
|
| 141 |
-
|
| 142 |
-
th {{
|
| 143 |
-
background: #333;
|
| 144 |
-
color: white;
|
| 145 |
-
font-weight: 600;
|
| 146 |
-
}}
|
| 147 |
-
|
| 148 |
-
.compact-table td.numeric-positive {{
|
| 149 |
-
color: green;
|
| 150 |
-
font-weight: bold;
|
| 151 |
-
}}
|
| 152 |
-
.compact-table td.numeric-negative {{
|
| 153 |
-
color: red;
|
| 154 |
-
font-weight: bold;
|
| 155 |
-
}}
|
| 156 |
-
|
| 157 |
-
/* Highlight top 3 gainers / losers */
|
| 158 |
-
.compact-table td.top-up {{
|
| 159 |
-
background: #a8f0a5; /* light green */
|
| 160 |
-
}}
|
| 161 |
-
.compact-table td.top-down {{
|
| 162 |
-
background: #f0a8a8; /* light red */
|
| 163 |
-
}}
|
| 164 |
-
|
| 165 |
-
/* Fixed row height & clipping for Constituent Table */
|
| 166 |
-
#constituents-table tr, #constituents-table td {{
|
| 167 |
-
max-height: 25px;
|
| 168 |
-
height: 25px;
|
| 169 |
-
overflow: hidden;
|
| 170 |
-
white-space: nowrap;
|
| 171 |
-
text-overflow: ellipsis;
|
| 172 |
-
}}
|
| 173 |
-
|
| 174 |
-
.small-table {{
|
| 175 |
-
background: white;
|
| 176 |
-
border-radius: 6px;
|
| 177 |
-
padding: 8px;
|
| 178 |
-
box-shadow: 0px 1px 4px rgba(0,0,0,0.15);
|
| 179 |
-
border: 1px solid #ddd;
|
| 180 |
-
overflow-y: auto;
|
| 181 |
-
}}
|
| 182 |
-
|
| 183 |
-
.st-title {{
|
| 184 |
-
font-size: 14px;
|
| 185 |
-
text-align: center;
|
| 186 |
-
margin-bottom: 6px;
|
| 187 |
-
font-weight: bold;
|
| 188 |
-
background: #222;
|
| 189 |
-
color: white;
|
| 190 |
-
padding: 5px 0;
|
| 191 |
-
border-radius: 4px;
|
| 192 |
-
}}
|
| 193 |
-
|
| 194 |
-
.st-body {{
|
| 195 |
-
max-height: 300px; /* vertical scroll for metric tables */
|
| 196 |
-
overflow-y: auto;
|
| 197 |
-
font-size: 12px;
|
| 198 |
-
}}
|
| 199 |
-
|
| 200 |
-
.compact-section {{
|
| 201 |
-
background: white;
|
| 202 |
-
padding: 8px;
|
| 203 |
-
border-radius: 6px;
|
| 204 |
-
box-shadow: 0 1px 4px rgba(0,0,0,0.12);
|
| 205 |
-
border: 1px solid #ddd;
|
| 206 |
-
margin-bottom: 15px;
|
| 207 |
-
overflow-x: visible;
|
| 208 |
-
}}
|
| 209 |
-
|
| 210 |
-
.grid {{
|
| 211 |
-
display: grid;
|
| 212 |
-
grid-template-columns: repeat(5, 1fr);
|
| 213 |
-
gap: 12px;
|
| 214 |
-
margin-top: 12px;
|
| 215 |
-
}}
|
| 216 |
-
</style>
|
| 217 |
-
</head>
|
| 218 |
-
<body>
|
| 219 |
-
|
| 220 |
-
<h2>Live Index Data: {name or 'Default Index'}</h2>
|
| 221 |
-
|
| 222 |
-
<div class="compact-section">
|
| 223 |
-
<h3>Index Info</h3>
|
| 224 |
-
{rem_html}
|
| 225 |
-
</div>
|
| 226 |
-
|
| 227 |
-
<div class="compact-section">
|
| 228 |
-
<h3>Main Data</h3>
|
| 229 |
-
{main_html}
|
| 230 |
-
</div>
|
| 231 |
-
|
| 232 |
-
<div class="compact-section">
|
| 233 |
-
<h3>Constituents</h3>
|
| 234 |
-
<div id="constituents-table">
|
| 235 |
-
{cons_html}
|
| 236 |
-
</div>
|
| 237 |
-
</div>
|
| 238 |
-
|
| 239 |
-
<h3>Metric Tables (All Symbols)</h3>
|
| 240 |
-
<div class="grid">
|
| 241 |
-
{metric_tables}
|
| 242 |
-
</div>
|
| 243 |
-
|
| 244 |
-
</body>
|
| 245 |
-
</html>
|
| 246 |
-
"""
|
| 247 |
-
return html
|
| 248 |
-
'''
|
| 249 |
from nsepython import *
|
| 250 |
import pandas as pd
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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