eshan6704 commited on
Commit
f88975f
·
verified ·
1 Parent(s): 82460cd

Update nsepython.py

Browse files
Files changed (1) hide show
  1. nsepython.py +273 -0
nsepython.py CHANGED
@@ -216,3 +216,276 @@ def stock_hist(f,t,symbol,series="ALL"):
216
  def nse_index_live(name="NIFTY 50"):
217
  p=nsefetch(f"https://www.nseindia.com/api/equity-stockIndices?index={name.replace(' ','%20')}")
218
  return {"data":df_from_data(p.pop("data")) if "data" in p else pd.DataFrame(), "rem":df_from_data([p])}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  def nse_index_live(name="NIFTY 50"):
217
  p=nsefetch(f"https://www.nseindia.com/api/equity-stockIndices?index={name.replace(' ','%20')}")
218
  return {"data":df_from_data(p.pop("data")) if "data" in p else pd.DataFrame(), "rem":df_from_data([p])}
219
+ import json
220
+ import pandas as pd
221
+ from nsepython import *
222
+
223
+ def build_indices_html():
224
+
225
+ p = indices()
226
+
227
+ data_df = p["data"]
228
+ dates_df = p["dates"]
229
+
230
+ data_json = json.dumps(data_df.to_dict(orient="records"), ensure_ascii=False)
231
+ dates_json = json.dumps(dates_df.to_dict(orient="records"), ensure_ascii=False)
232
+
233
+ DEFAULT_KEY = "INDICES ELIGIBLE IN DERIVATIVES"
234
+ DEFAULT_SYMBOL = "NIFTY 50"
235
+
236
+ html = f"""
237
+ <!DOCTYPE html>
238
+ <html>
239
+ <head>
240
+ <meta charset="UTF-8">
241
+ <title>NSE Indices Dashboard</title>
242
+
243
+ <style>
244
+ body {{
245
+ font-family: Arial, sans-serif;
246
+ padding: 20px;
247
+ }}
248
+
249
+ button {{
250
+ padding: 7px 14px;
251
+ margin-bottom: 10px;
252
+ cursor: pointer;
253
+ }}
254
+
255
+ .scroll-table {{
256
+ width: 100%;
257
+ overflow: auto;
258
+ border: 1px solid #ccc;
259
+ max-height: 450px;
260
+ margin-bottom: 20px;
261
+ }}
262
+
263
+ table {{
264
+ border-collapse: collapse;
265
+ width: max-content;
266
+ min-width: 100%;
267
+ }}
268
+
269
+ th, td {{
270
+ border: 1px solid #ddd;
271
+ padding: 8px;
272
+ white-space: nowrap;
273
+ }}
274
+
275
+ th {{
276
+ background-color: #007bff;
277
+ color: white;
278
+ position: sticky;
279
+ top: 0;
280
+ z-index: 5;
281
+ }}
282
+
283
+ .chart-grid {{
284
+ display: grid;
285
+ grid-template-columns: 1fr 1fr;
286
+ grid-template-rows: 200px 200px;
287
+ gap: 20px;
288
+ }}
289
+
290
+ .chart-box {{
291
+ width: 100%;
292
+ height: 100%;
293
+ border: 1px solid #ccc;
294
+ }}
295
+
296
+ #chart365 {{
297
+ grid-column: 1 / 3;
298
+ }}
299
+
300
+ select {{
301
+ padding: 6px;
302
+ margin: 8px 0;
303
+ }}
304
+ </style>
305
+
306
+ </head>
307
+ <body>
308
+
309
+ <h2>NSE Indices Dashboard</h2>
310
+
311
+ <script>
312
+ const records = {data_json};
313
+ const dates = {dates_json};
314
+ const DEFAULT_KEY = "{DEFAULT_KEY}";
315
+ const DEFAULT_SYMBOL = "{DEFAULT_SYMBOL}";
316
+ </script>
317
+
318
+ <!-- MAIN TABLE -->
319
+ <h3>Main Full Indices Table</h3>
320
+ <button onclick="toggleMainTable()">Show / Hide Main Table</button>
321
+
322
+ <div id="mainTableSection" class="scroll-table" style="display:none;">
323
+ <table id="mainTable"></table>
324
+ </div>
325
+
326
+ <hr>
327
+
328
+ <!-- FILTERED TABLE -->
329
+ <h3>Filter Table by Category</h3>
330
+
331
+ <label><b>Select Index Category:</b></label>
332
+ <select id="keyDropdown"></select>
333
+
334
+ <div id="altTableSection" class="scroll-table">
335
+ <table id="altTable"></table>
336
+ </div>
337
+
338
+ <hr>
339
+
340
+ <!-- CHARTS -->
341
+ <h3>Charts Based on Index</h3>
342
+
343
+ <label><b>Select Index:</b></label>
344
+ <select id="chartDropdown"></select>
345
+
346
+ <div class="chart-grid">
347
+ <iframe id="chartToday" class="chart-box"></iframe>
348
+ <iframe id="chart30" class="chart-box"></iframe>
349
+ <iframe id="chart365" class="chart-box"></iframe>
350
+ </div>
351
+
352
+ <script>
353
+
354
+ // ================= MAIN TABLE =================
355
+
356
+ function buildMainTable() {{
357
+ const table = document.getElementById("mainTable");
358
+ const cols = Object.keys(records[0]);
359
+
360
+ let header = "<tr>";
361
+ cols.forEach(c => header += `<th>${{c}}</th>`);
362
+ header += "</tr>";
363
+
364
+ let rows = "";
365
+ records.forEach(r => {{
366
+ rows += "<tr>";
367
+ cols.forEach(c => rows += `<td>${{r[c]}}</td>`);
368
+ rows += "</tr>";
369
+ }});
370
+
371
+ table.innerHTML = header + rows;
372
+ }}
373
+
374
+ function toggleMainTable() {{
375
+ const sec = document.getElementById("mainTableSection");
376
+ sec.style.display = sec.style.display === "none" ? "block" : "none";
377
+ }}
378
+
379
+ buildMainTable();
380
+
381
+
382
+ // ================= FILTERED TABLE =================
383
+
384
+ const keyDropdown = document.getElementById("keyDropdown");
385
+ const chartDropdown = document.getElementById("chartDropdown");
386
+
387
+ const keyList = [...new Set(records.map(r => r.key))];
388
+ keyList.forEach(k => {{
389
+ const opt = document.createElement("option");
390
+ opt.value = k;
391
+ opt.textContent = k;
392
+ if (k === DEFAULT_KEY) opt.selected = true;
393
+ keyDropdown.appendChild(opt);
394
+ }});
395
+
396
+ function buildAltTable(keyName) {{
397
+ const table = document.getElementById("altTable");
398
+
399
+ const filtered = records.filter(r => r.key === keyName);
400
+
401
+ if (!filtered.length) {{
402
+ table.innerHTML = "<tr><td>No Data</td></tr>";
403
+ return;
404
+ }}
405
+
406
+ const hiddenCols = [
407
+ "key","chartTodayPath","chart30dPath","chart30Path","chart365dPath",
408
+ "date365dAgo","date30dAgo","previousDay","oneWeekAgo","oneMonthAgoVal",
409
+ "oneWeekAgoVal","oneYearAgoVal","index","indicativeClose"
410
+ ];
411
+
412
+ const cols = Object.keys(filtered[0]).filter(c => !hiddenCols.includes(c));
413
+
414
+ let header = "<tr>";
415
+ cols.forEach(c => header += `<th>${{c}}</th>`);
416
+ header += "</tr>";
417
+
418
+ let rows = "";
419
+ filtered.forEach(obj => {{
420
+ rows += "<tr>";
421
+ cols.forEach(c => rows += `<td>${{obj[c]}}</td>`);
422
+ rows += "</tr>";
423
+ }});
424
+
425
+ table.innerHTML = header + rows;
426
+ }}
427
+
428
+
429
+ // ================= CHARTS =================
430
+
431
+ function populateChartDropdown(keyVal) {{
432
+ chartDropdown.innerHTML = "";
433
+
434
+ records.filter(r => r.key === keyVal).forEach(r => {{
435
+ const opt = document.createElement("option");
436
+ opt.value = r.indexSymbol;
437
+ opt.textContent = r.index;
438
+ chartDropdown.appendChild(opt);
439
+ }});
440
+
441
+ // auto select default
442
+ [...chartDropdown.options].forEach(opt => {{
443
+ if (opt.textContent.toUpperCase().includes(DEFAULT_SYMBOL.toUpperCase()))
444
+ opt.selected = true;
445
+ }});
446
+ }}
447
+
448
+ function loadCharts(symbol) {{
449
+ const row = records.find(r => r.indexSymbol === symbol);
450
+ if (!row) return;
451
+
452
+ document.getElementById("chartToday").src = row.chartTodayPath;
453
+ document.getElementById("chart30").src = row.chart30dPath || row.chart30Path;
454
+ document.getElementById("chart365").src = row.chart365dPath;
455
+ }}
456
+
457
+
458
+ // ================= EVENT HANDLERS =================
459
+
460
+ keyDropdown.addEventListener("change", () => {{
461
+ const keyVal = keyDropdown.value;
462
+ buildAltTable(keyVal);
463
+ populateChartDropdown(keyVal);
464
+ loadCharts(chartDropdown.value);
465
+ }});
466
+
467
+ chartDropdown.addEventListener("change", () => {{
468
+ loadCharts(chartDropdown.value);
469
+ }});
470
+
471
+
472
+ // ================= INITIAL LOAD =================
473
+
474
+ buildAltTable(DEFAULT_KEY);
475
+ populateChartDropdown(DEFAULT_KEY);
476
+
477
+ let initial = records.find(
478
+ r => r.index.toUpperCase().includes(DEFAULT_SYMBOL.toUpperCase())
479
+ );
480
+
481
+ if (!initial) initial = records[0];
482
+
483
+ loadCharts(initial.indexSymbol);
484
+
485
+ </script>
486
+
487
+ </body>
488
+ </html>
489
+ """
490
+ return html
491
+