eshan6704 commited on
Commit
bcb038d
·
verified ·
1 Parent(s): 03533f6

Update indices_html.py

Browse files
Files changed (1) hide show
  1. indices_html.py +35 -31
indices_html.py CHANGED
@@ -2,8 +2,12 @@ import json
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():
9
  """
@@ -13,8 +17,19 @@ def build_indices_html():
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())
@@ -61,10 +76,6 @@ def build_indices_html():
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():
@@ -78,7 +89,7 @@ def build_indices_html():
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") +
@@ -106,11 +117,11 @@ def build_indices_html():
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)
@@ -120,27 +131,19 @@ def build_indices_html():
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">
@@ -169,7 +172,6 @@ def build_indices_html():
169
  margin-bottom: 30px;
170
  }
171
 
172
- /* FLEXIBLE CHART LAYOUT */
173
  .chart-flex-block {
174
  border: 1px solid #ddd;
175
  background: #fff;
@@ -202,27 +204,29 @@ def build_indices_html():
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)
 
2
  import pandas as pd
3
  from nsepython import *
4
  import html
5
+ from datetime import datetime as dt
6
+ from collections import defaultdict
7
+
8
+ # persist helpers (already exist in your project)
9
+ from persist import exists, load, save
10
+
11
 
12
  def build_indices_html():
13
  """
 
17
  - tables for all categories
18
  - charts ONLY for key == "INDICES ELIGIBLE IN DERIVATIVES"
19
  - flexible chart layout (no grid, auto-fit)
20
+ - DAILY CACHE ENABLED
21
  """
22
 
23
+ # ================= CACHE =================
24
+ today = dt.now().strftime("%Y-%m-%d")
25
+ cache_key = "indices_html"
26
+
27
+ if exists(cache_key):
28
+ cached = load(cache_key)
29
+ if isinstance(cached, dict) and cached.get("date") == today:
30
+ return cached.get("html")
31
+
32
+ # ================= FETCH DATA =================
33
  p = indices() # your existing function
34
  data_df = p.get("data", pd.DataFrame())
35
  dates_df = p.get("dates", pd.DataFrame())
 
76
 
77
  # ----------- FLEXIBLE CHART BLOCK -----------
78
  def build_chart_grid_for_record(r):
 
 
 
 
79
 
80
  def iframe_if_exists(src, label):
81
  if src and isinstance(src, str) and src.strip():
 
89
 
90
  today_src = r.get("chartTodayPath") or r.get("chartToday") or ""
91
  month30_src = r.get("chart30dPath") or r.get("chart30Path") or ""
92
+ year365_src = r.get("chart365dPath") or r.get("chart365") or ""
93
 
94
  block = (
95
  iframe_if_exists(today_src, "Today Chart") +
 
117
  # ----------- DATES TABLE -----------
118
  dates_table_html = ""
119
  if not dates_df.empty:
120
+ dates_table_html = build_table_from_records(
121
+ dates_df.to_dict(orient="records")
122
+ )
123
 
124
  # ----------- GROUP BY KEY ----------
 
125
  groups = defaultdict(list)
126
  for r in records:
127
  groups[r.get("key") or "UNCLASSIFIED"].append(r)
 
131
  # ----------- PER CATEGORY SECTIONS ----------
132
  for key_name, recs in groups.items():
133
 
 
134
  first = recs[0]
135
  cols = [c for c in first.keys() if c not in hidden_cols]
136
 
 
137
  preferred = ["indexSymbol", "index", "symbol", "name"]
138
+ ordered = [c for c in preferred if c in cols] + [c for c in cols if c not in preferred]
 
 
 
 
 
 
139
 
140
  table_html = build_table_from_records(recs, ordered)
141
 
142
+ # Charts only for derivative-eligible indices
143
  if str(key_name).strip().upper() == "INDICES ELIGIBLE IN DERIVATIVES":
144
  charts_html = "\n".join(build_chart_grid_for_record(r) for r in recs)
145
  else:
146
+ charts_html = ""
147
 
148
  per_key_sections.append(f"""
149
  <section class="key-section">
 
172
  margin-bottom: 30px;
173
  }
174
 
 
175
  .chart-flex-block {
176
  border: 1px solid #ddd;
177
  background: #fff;
 
204
  </style>
205
  """
206
 
207
+ # ----------- FINAL HTML -----------
208
+ html_out = "\n".join([
209
  "<!DOCTYPE html>",
210
  "<html><head><meta charset='utf-8'><title>NSE Indices</title>",
211
  css,
212
  "</head><body>",
213
  "<h1>NSE Indices — Full Static Render</h1>",
214
+ f"<div class='meta'>Generated: {html.escape(dt.now().strftime('%Y-%m-%d %H:%M:%S'))}</div>",
 
215
  "<h2>Main Indices Table</h2>",
216
  "<div class='scroll'>", main_table_html, "</div>",
 
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
  "<h2>Categories</h2>",
222
  *per_key_sections,
 
223
  "</body></html>"
224
+ ])
225
+
226
+ # ================= SAVE CACHE =================
227
+ save(cache_key, {
228
+ "date": today,
229
+ "html": html_out
230
+ })
231
 
232
+ return html_out