eshan6704 commited on
Commit
044060d
·
verified ·
1 Parent(s): 4fad0b3

Update build_index_live_html.py

Browse files
Files changed (1) hide show
  1. build_index_live_html.py +216 -0
build_index_live_html.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from nsepython import *
2
  import pandas as pd
3
 
@@ -186,3 +187,218 @@ h2, h3 {{
186
  """
187
 
188
  return html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
  from nsepython import *
3
  import pandas as pd
4
 
 
187
  """
188
 
189
  return html
190
+ '''
191
+ from nsepython import *
192
+ import pandas as pd
193
+
194
+ def build_index_live_html(name=""):
195
+ p = nse_index_live(name)
196
+
197
+ full_df = p.get("data", pd.DataFrame())
198
+ rem_df = p.get("rem", pd.DataFrame())
199
+
200
+ if full_df.empty:
201
+ main_df = pd.DataFrame()
202
+ const_df = pd.DataFrame()
203
+ else:
204
+ main_df = full_df.iloc[[0]]
205
+ const_df = full_df.iloc[1:] # Constituents
206
+ if not const_df.empty:
207
+ const_df = const_df.iloc[:, 1:] # Remove first column
208
+ # Default sort constituents by pChange descending if exists
209
+ if 'pChange' in const_df.columns:
210
+ const_df['pChange'] = pd.to_numeric(const_df['pChange'], errors='coerce')
211
+ const_df = const_df.sort_values('pChange', ascending=False)
212
+
213
+ # ================= HELPER FUNCTION: COLOR-CODE NUMERIC =================
214
+ def df_to_html_color(df):
215
+ df_html = df.copy()
216
+ for col in df_html.columns:
217
+ if pd.api.types.is_numeric_dtype(df_html[col]):
218
+ df_html[col] = df_html[col].apply(
219
+ lambda x: f'<span class="numeric-positive">{x}</span>' if x > 0 else
220
+ f'<span class="numeric-negative">{x}</span>' if x < 0 else str(x)
221
+ )
222
+ return df_html.to_html(index=False, escape=False, classes="compact-table")
223
+
224
+ rem_html = df_to_html_color(rem_df)
225
+ main_html = df_to_html_color(main_df)
226
+ cons_html = df_to_html_color(const_df)
227
+
228
+ # ================= METRIC TABLES =================
229
+ metric_cols = [
230
+ "pChange", "totalTradedValue", "nearWKH", "nearWKL",
231
+ "perChange365d", "perChange30d", "listingDate"
232
+ ]
233
+
234
+ metric_tables = ""
235
+ for col in metric_cols:
236
+ if col not in const_df.columns:
237
+ continue
238
+
239
+ df_const = const_df.copy()
240
+ df_const[col] = pd.to_numeric(df_const[col], errors="ignore")
241
+
242
+ # Top 20 Upside
243
+ df_up = df_const[["symbol", col]].dropna().sort_values(col, ascending=False).head(20)
244
+ df_up_html = df_to_html_color(df_up)
245
+
246
+ # Top 20 Downside
247
+ df_down = df_const[["symbol", col]].dropna().sort_values(col, ascending=True).head(20)
248
+ df_down_html = df_to_html_color(df_down)
249
+
250
+ metric_tables += f"""
251
+ <div class="small-table">
252
+ <div class="st-title">{col}</div>
253
+ <div class="sub-title up">Top 20 Upside</div>
254
+ <div class="st-body">{df_up_html}</div>
255
+ <div class="sub-title down">Top 20 Downside</div>
256
+ <div class="st-body">{df_down_html}</div>
257
+ </div>
258
+ """
259
+
260
+ # ================= FINAL HTML =================
261
+ html = f"""
262
+ <!DOCTYPE html>
263
+ <html>
264
+ <head>
265
+ <meta charset="UTF-8">
266
+
267
+ <style>
268
+ body {{
269
+ font-family: Arial;
270
+ margin: 8px;
271
+ background: #f5f5f5;
272
+ color: #222;
273
+ }}
274
+
275
+ h2, h3 {{
276
+ margin: 8px 0 4px 0;
277
+ font-weight: 600;
278
+ }}
279
+
280
+ table {{
281
+ border-collapse: collapse;
282
+ width: 100%;
283
+ }}
284
+
285
+ th, td {{
286
+ border: 1px solid #bbb;
287
+ padding: 3px 5px;
288
+ text-align: left;
289
+ font-size: 11px;
290
+ }}
291
+
292
+ th {{
293
+ background: #333;
294
+ color: white;
295
+ font-weight: 600;
296
+ }}
297
+
298
+ .compact-table td.numeric-positive {{
299
+ color: green;
300
+ font-weight: bold;
301
+ }}
302
+ .compact-table td.numeric-negative {{
303
+ color: red;
304
+ font-weight: bold;
305
+ }}
306
+
307
+ .grid {{
308
+ display: grid;
309
+ grid-template-columns: repeat(5, 1fr);
310
+ gap: 12px;
311
+ margin-top: 12px;
312
+ }}
313
+
314
+ .small-table {{
315
+ background: white;
316
+ border-radius: 6px;
317
+ padding: 6px;
318
+ box-shadow: 0px 1px 4px rgba(0,0,0,0.15);
319
+ border: 1px solid #ddd;
320
+ overflow: auto;
321
+ }}
322
+
323
+ .st-title {{
324
+ font-size: 13px;
325
+ text-align: center;
326
+ margin-bottom: 4px;
327
+ font-weight: bold;
328
+ background: #222;
329
+ color: white;
330
+ padding: 3px 0;
331
+ border-radius: 4px;
332
+ }}
333
+
334
+ .sub-title {{
335
+ margin-top: 4px;
336
+ font-weight: bold;
337
+ text-align: center;
338
+ padding: 2px;
339
+ border-radius: 4px;
340
+ font-size: 11px;
341
+ color: white;
342
+ }}
343
+
344
+ .sub-title.up {{
345
+ background: #0a4;
346
+ }}
347
+
348
+ .sub-title.down {{
349
+ background: #c22;
350
+ }}
351
+
352
+ .st-body {{
353
+ max-height: 200px;
354
+ overflow: auto;
355
+ }}
356
+
357
+ .compact-container {{
358
+ display: flex;
359
+ gap: 8px;
360
+ flex-wrap: wrap;
361
+ }}
362
+
363
+ .compact-section {{
364
+ background: white;
365
+ padding: 5px;
366
+ border-radius: 6px;
367
+ box-shadow: 0 1px 4px rgba(0,0,0,0.12);
368
+ border: 1px solid #ddd;
369
+ flex: 1;
370
+ min-width: 220px;
371
+ max-width: 48%;
372
+ margin-bottom: 8px;
373
+ overflow-x: auto;
374
+ }}
375
+ </style>
376
+ </head>
377
+ <body>
378
+
379
+ <h2>Live Index Data: {name or 'Default Index'}</h2>
380
+
381
+ <div class="compact-container">
382
+ <div class="compact-section">
383
+ <h3>Index Info</h3>
384
+ {rem_html}
385
+ </div>
386
+ <div class="compact-section">
387
+ <h3>Main Data</h3>
388
+ {main_html}
389
+ </div>
390
+ <div class="compact-section" style="flex-basis:100%;">
391
+ <h3>Constituents</h3>
392
+ {cons_html}
393
+ </div>
394
+ </div>
395
+
396
+ <h3>Metric Tables (Top 20 Upside / Downside)</h3>
397
+ <div class="grid">
398
+ {metric_tables}
399
+ </div>
400
+
401
+ </body>
402
+ </html>
403
+ """
404
+ return html