eshan6704 commited on
Commit
2194f48
·
verified ·
1 Parent(s): a106796

Create indices_html.py

Browse files
Files changed (1) hide show
  1. indices_html.py +226 -0
indices_html.py ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pandas as pd
3
+ from nsepython import *
4
+ def build_indices_html():
5
+
6
+ p=indices()
7
+
8
+ data_df = p["data"]
9
+ dates_df = p["dates"]
10
+
11
+ # Convert to JSON for JS
12
+ data_json = json.dumps(data_df.to_dict(orient="records"), ensure_ascii=False)
13
+ dates_json = json.dumps(dates_df.to_dict(orient="records"), ensure_ascii=False)
14
+
15
+ DEFAULT_KEY = "INDICES ELIGIBLE IN DERIVATIVES"
16
+ DEFAULT_SYMBOL = "NIFTY 50"
17
+
18
+ html = f"""
19
+ <!DOCTYPE html>
20
+ <html>
21
+ <head>
22
+ <meta charset="UTF-8">
23
+ <title>NSE Indices Dashboard</title>
24
+
25
+ <style>
26
+ body {{
27
+ font-family: Arial, sans-serif;
28
+ padding: 20px;
29
+ }}
30
+ .scroll-table {{
31
+ width: 100%;
32
+ overflow: auto;
33
+ border: 1px solid #ccc;
34
+ max-height: 450px;
35
+ margin-bottom: 20px;
36
+ }}
37
+ table {{
38
+ border-collapse: collapse;
39
+ width: max-content;
40
+ min-width: 100%;
41
+ }}
42
+ th, td {{
43
+ border: 1px solid #ddd;
44
+ padding: 8px;
45
+ white-space: nowrap;
46
+ }}
47
+ th {{
48
+ background-color: #007bff;
49
+ color: white;
50
+ position: sticky;
51
+ top: 0;
52
+ z-index: 5;
53
+ }}
54
+ #datesTable {{
55
+ margin-bottom: 25px;
56
+ width: max-content;
57
+ border: 1px solid #ccc;
58
+ }}
59
+
60
+ .chart-grid {{
61
+ display: grid;
62
+ grid-template-columns: 1fr 1fr;
63
+ grid-template-rows: 200px 200px;
64
+ gap: 20px;
65
+ width: 100%;
66
+ }}
67
+
68
+ .chart-box {{
69
+ width: 100%;
70
+ height: 100%;
71
+ border: 1px solid #ccc;
72
+ }}
73
+
74
+ #chart365 {{
75
+ grid-column: 1 / 3;
76
+ }}
77
+
78
+ select {{
79
+ padding: 6px;
80
+ margin: 8px 0;
81
+ }}
82
+ </style>
83
+
84
+ </head>
85
+ <body>
86
+
87
+ <h2>NSE Indices Dashboard</h2>
88
+
89
+ <!-- DATES TABLE -->
90
+ <h3>Reference Dates</h3>
91
+ <table id="datesTable"></table>
92
+
93
+ <hr>
94
+
95
+ <!-- Dropdown for Key -->
96
+ <label><b>Select Index Category:</b></label>
97
+ <select id="keyDropdown"></select>
98
+
99
+ <div id="altTableSection" class="scroll-table">
100
+ <table id="altTable"></table>
101
+ </div>
102
+
103
+ <hr>
104
+
105
+ <!-- Dropdown for Charts -->
106
+ <label><b>Select Index for Charts:</b></label>
107
+ <select id="chartDropdown"></select>
108
+
109
+ <div class="chart-grid">
110
+ <iframe id="chartToday" class="chart-box"></iframe>
111
+ <iframe id="chart30" class="chart-box"></iframe>
112
+ <iframe id="chart365" class="chart-box"></iframe>
113
+ </div>
114
+
115
+ <script>
116
+ const records = {data_json};
117
+ const dates = {dates_json};
118
+ const DEFAULT_KEY = "{DEFAULT_KEY}";
119
+ const DEFAULT_SYMBOL = "{DEFAULT_SYMBOL}";
120
+
121
+ const keyDropdown = document.getElementById("keyDropdown");
122
+ const chartDropdown = document.getElementById("chartDropdown");
123
+
124
+ // ------------------- Build Dates Table -------------------
125
+ function buildDatesTable() {{
126
+ const table = document.getElementById("datesTable");
127
+ const row = dates[0];
128
+ let header = "<tr>";
129
+ Object.keys(row).forEach(k => header += `<th>${{k}}</th>`);
130
+ header += "</tr>";
131
+ let body = "<tr>";
132
+ Object.values(row).forEach(v => body += `<td>${{v}}</td>`);
133
+ body += "</tr>";
134
+ table.innerHTML = header + body;
135
+ }}
136
+ buildDatesTable();
137
+
138
+ // ------------------- Populate Key Dropdown -------------------
139
+ const keyList = [...new Set(records.map(r => r.key))];
140
+ keyList.forEach(k => {{
141
+ const opt = document.createElement("option");
142
+ opt.value = k;
143
+ opt.textContent = k;
144
+ if (k === DEFAULT_KEY) opt.selected = true;
145
+ keyDropdown.appendChild(opt);
146
+ }});
147
+
148
+ // ------------------- Populate Chart Dropdown -------------------
149
+ function populateChartDropdown(keyVal) {{
150
+ chartDropdown.innerHTML = "";
151
+ records.filter(r => r.key === keyVal).forEach(r => {{
152
+ const opt = document.createElement("option");
153
+ opt.value = r.indexSymbol;
154
+ opt.textContent = r.index;
155
+ if (r.index === DEFAULT_SYMBOL) opt.selected = true;
156
+ chartDropdown.appendChild(opt);
157
+ }});
158
+ }}
159
+
160
+ // ------------------- Build Filtered Table -------------------
161
+ function buildAltTable(keyName) {{
162
+ const table = document.getElementById("altTable");
163
+ const div = document.getElementById("altTableSection");
164
+
165
+ const filtered = records.filter(r => r.key === keyName);
166
+ if (!filtered.length) {{
167
+ table.innerHTML = "<tr><td>No Data</td></tr>";
168
+ div.style.display = "none";
169
+ return;
170
+ }}
171
+
172
+ const hiddenCols = [
173
+ "key", "chartTodayPath", "chart30dPath", "chart365dPath",
174
+ "date365dAgo","date30dAgo","previousDay","oneWeekAgo","oneMonthAgoVal",
175
+ "oneWeekAgoVal","oneYearAgoVal","index","indicativeClose"
176
+ ];
177
+
178
+ const columns = Object.keys(filtered[0]).filter(c => !hiddenCols.includes(c));
179
+
180
+ let header = "<tr>";
181
+ columns.forEach(c => header += `<th>${{c}}</th>`);
182
+ header += "</tr>";
183
+
184
+ let rows = "";
185
+ filtered.forEach(obj => {{
186
+ rows += "<tr>";
187
+ columns.forEach(c => rows += `<td>${{obj[c]}}</td>`);
188
+ rows += "</tr>";
189
+ }});
190
+
191
+ table.innerHTML = header + rows;
192
+ div.style.display = "block";
193
+ }}
194
+
195
+ // ------------------- Load Charts -------------------
196
+ function loadCharts(symbol) {{
197
+ const row = records.find(r => r.indexSymbol === symbol);
198
+ if (!row) return;
199
+ document.getElementById("chartToday").src = row.chartTodayPath;
200
+ document.getElementById("chart30").src = row.chart30dPath;
201
+ document.getElementById("chart365").src = row.chart365dPath;
202
+ }}
203
+
204
+ // Event listeners
205
+ keyDropdown.addEventListener("change", () => {{
206
+ const keyVal = keyDropdown.value;
207
+ buildAltTable(keyVal);
208
+ populateChartDropdown(keyVal);
209
+ loadCharts(chartDropdown.value);
210
+ }});
211
+
212
+ chartDropdown.addEventListener("change", () => {{
213
+ loadCharts(chartDropdown.value);
214
+ }});
215
+
216
+ // ------------------- Initial Load -------------------
217
+ buildAltTable(DEFAULT_KEY);
218
+ populateChartDropdown(DEFAULT_KEY);
219
+ loadCharts(records.find(r => r.index === DEFAULT_SYMBOL).indexSymbol);
220
+
221
+ </script>
222
+
223
+ </body>
224
+ </html>
225
+ """
226
+ return html