eshan6704 commited on
Commit
cac815d
·
verified ·
1 Parent(s): 62d151c

Update eq_html.py

Browse files
Files changed (1) hide show
  1. eq_html.py +141 -159
eq_html.py CHANGED
@@ -1,87 +1,72 @@
1
  from nsepython import *
2
- import os
3
- import pickle
4
  import pandas as pd
5
  from datetime import datetime
 
6
 
7
- # =====================================================
8
- # CACHE CONFIG (DAILY VALIDITY)
9
- # =====================================================
10
- CACHE_DIR = "./cache/eq"
11
  os.makedirs(CACHE_DIR, exist_ok=True)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- def _today_key():
15
- return datetime.now().strftime("%Y%m%d")
16
-
17
-
18
- def _path(name):
19
- return os.path.join(CACHE_DIR, name)
20
-
21
-
22
- def cache_load(name):
23
- try:
24
- with open(_path(name), "rb") as f:
25
- return pickle.load(f)
26
- except Exception:
27
- return None
28
-
29
 
30
- def cache_save(name, obj):
31
- with open(_path(name), "wb") as f:
32
- pickle.dump(obj, f)
 
33
 
 
 
 
 
34
 
35
- # =====================================================
36
- # MAIN FUNCTION (DO NOT RENAME)
37
- # =====================================================
38
- def build_eq_html(symbol):
39
- """
40
- Build full HTML page for eq(symbol)
41
- Cache rules:
42
- - HTML cached per (symbol + day)
43
- - EQ raw output cached per (symbol + day)
44
- """
45
 
46
- day = _today_key()
47
- symbol = symbol.upper()
48
-
49
- html_key = f"eq_html_{symbol}_{day}.pkl"
50
- raw_key = f"eq_raw_{symbol}_{day}.pkl"
51
-
52
- # --------------------------------------------------
53
- # 1️⃣ HTML CACHE FIRST
54
- # --------------------------------------------------
55
- html = cache_load(html_key)
56
- if html:
57
- return html
58
-
59
- # --------------------------------------------------
60
- # 2️⃣ RAW EQ CACHE
61
- # --------------------------------------------------
62
- out = cache_load(raw_key)
63
- if not out:
64
- out = eq(symbol) # 🔥 existing nsepython function
65
- if not isinstance(out, dict):
66
- return "<h3>Error: EQ data not available</h3>"
67
- cache_save(raw_key, out)
68
-
69
- # --------------------------------------------------
70
- # 3️⃣ DF → HTML TABLE
71
- # --------------------------------------------------
72
  def df_to_table(df):
73
  if df is None or df.empty:
74
  return '<div class="empty">No data</div>'
75
- return df.to_html(
76
- index=False,
77
- escape=False,
78
- border=0,
79
- classes="tbl"
80
- )
81
-
82
- # --------------------------------------------------
83
- # 4️⃣ SECTION ORDER (METADATA FIRST)
84
- # --------------------------------------------------
85
  section_order = [
86
  "metadata",
87
  "securityInfo",
@@ -93,6 +78,7 @@ def build_eq_html(symbol):
93
  "preOpenMarket"
94
  ]
95
 
 
96
  normalized = {}
97
  for sec in section_order:
98
  val = out.get(sec)
@@ -105,18 +91,16 @@ def build_eq_html(symbol):
105
  else:
106
  normalized[sec] = pd.DataFrame()
107
 
108
- # --------------------------------------------------
109
- # 5️⃣ BUILD SECTION HTML
110
- # --------------------------------------------------
111
  section_html = ""
112
  for sec in section_order:
113
  section_html += f"""
114
  <div class="section">
115
  <div class="section-header">
116
  <div class="section-title">{sec}</div>
117
- <button class="toggle-btn" onclick="toggleSection('{sec}')">
118
- View / Hide
119
- </button>
120
  </div>
121
  <div id="{sec}" class="section-body">
122
  {df_to_table(normalized[sec])}
@@ -124,88 +108,86 @@ def build_eq_html(symbol):
124
  </div>
125
  """
126
 
127
- # --------------------------------------------------
128
- # 6️⃣ FINAL HTML
129
- # --------------------------------------------------
130
  html = f"""
131
- <html>
132
- <head>
133
- <style>
134
- body {{
135
- font-family: Arial, sans-serif;
136
- background: #fafafa;
137
- margin: 0;
138
- padding: 12px;
139
- }}
140
- h2 {{
141
- color: #0366d6;
142
- margin-bottom: 10px;
143
- }}
144
- .section {{
145
- background: #fff;
146
- border: 1px solid #ddd;
147
- margin-bottom: 14px;
148
- padding: 12px;
149
- border-radius: 6px;
150
- }}
151
- .section-header {{
152
- display: flex;
153
- justify-content: space-between;
154
- margin-bottom: 8px;
155
- }}
156
- .section-title {{
157
- font-weight: bold;
158
- color: #0b69a3;
159
- font-size: 15px;
160
- }}
161
- .toggle-btn {{
162
- background: #0b69a3;
163
- color: #fff;
164
- border: none;
165
- padding: 6px 10px;
166
- font-size: 12px;
167
- border-radius: 5px;
168
- cursor: pointer;
169
- }}
170
- .tbl {{
171
- border-collapse: collapse;
172
- width: 100%;
173
- font-size: 13px;
174
- }}
175
- .tbl th {{
176
- background: #0b69a3;
177
- color: #fff;
178
- padding: 6px;
179
- border: 1px solid #ccc;
180
- }}
181
- .tbl td {{
182
- padding: 6px;
183
- border: 1px solid #ccc;
184
- }}
185
- .empty {{
186
- padding: 10px;
187
- color: #777;
188
- }}
189
- </style>
190
-
191
- <script>
192
- function toggleSection(id) {{
193
- const e = document.getElementById(id);
194
- e.style.display = (e.style.display === "none") ? "block" : "none";
195
- }}
196
- </script>
197
- </head>
198
-
199
- <body>
200
- <h2>Equity Report — {symbol}</h2>
201
- {section_html}
202
- </body>
203
- </html>
204
- """
205
-
206
- # --------------------------------------------------
207
- # 7️⃣ SAVE HTML CACHE
208
- # --------------------------------------------------
209
- cache_save(html_key, html)
210
-
211
  return html
 
1
  from nsepython import *
 
 
2
  import pandas as pd
3
  from datetime import datetime
4
+ import os
5
 
6
+ # ==============================
7
+ # Simple daily cache helpers
8
+ # ==============================
9
+ CACHE_DIR = "./cache_eq"
10
  os.makedirs(CACHE_DIR, exist_ok=True)
11
 
12
+ def _cache_path(key):
13
+ return os.path.join(CACHE_DIR, f"{key}.html")
14
+
15
+ def _is_today(path):
16
+ if not os.path.exists(path):
17
+ return False
18
+ mtime = datetime.fromtimestamp(os.path.getmtime(path)).date()
19
+ return mtime == datetime.now().date()
20
+
21
+ def cache_load(key):
22
+ path = _cache_path(key)
23
+ if _is_today(path):
24
+ try:
25
+ with open(path, "r", encoding="utf-8") as f:
26
+ return f.read()
27
+ except:
28
+ return False
29
+ return False
30
+
31
+ def cache_save(key, html):
32
+ with open(_cache_path(key), "w", encoding="utf-8") as f:
33
+ f.write(html)
34
+
35
+ # ==============================
36
+ # MAIN FUNCTION (CACHED)
37
+ # ==============================
38
+ def build_eq_html(symbol):
39
+ """
40
+ Build full HTML page for eq(symbol) output
41
+ DAILY CACHED
42
+ """
43
 
44
+ cache_key = f"eq_{symbol.upper()}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ # ---------- CACHE HIT ----------
47
+ cached = cache_load(cache_key)
48
+ if cached:
49
+ return cached
50
 
51
+ # -------------------------------------------------------
52
+ # CALL eq() function
53
+ # -------------------------------------------------------
54
+ out = eq(symbol)
55
 
56
+ if not isinstance(out, dict):
57
+ return "<h3>Error: EQ data not available</h3>"
 
 
 
 
 
 
 
 
58
 
59
+ # -------------------------------------------------------
60
+ # Helper: DataFrame → HTML table
61
+ # -------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def df_to_table(df):
63
  if df is None or df.empty:
64
  return '<div class="empty">No data</div>'
65
+ return df.to_html(index=False, escape=False, border=0, classes="tbl")
66
+
67
+ # -------------------------------------------------------
68
+ # ORDER
69
+ # -------------------------------------------------------
 
 
 
 
 
70
  section_order = [
71
  "metadata",
72
  "securityInfo",
 
78
  "preOpenMarket"
79
  ]
80
 
81
+ # Normalize to DataFrames
82
  normalized = {}
83
  for sec in section_order:
84
  val = out.get(sec)
 
91
  else:
92
  normalized[sec] = pd.DataFrame()
93
 
94
+ # -------------------------------------------------------
95
+ # Sections HTML
96
+ # -------------------------------------------------------
97
  section_html = ""
98
  for sec in section_order:
99
  section_html += f"""
100
  <div class="section">
101
  <div class="section-header">
102
  <div class="section-title">{sec}</div>
103
+ <button class="toggle-btn" onclick="toggleSection('{sec}')">View / Hide</button>
 
 
104
  </div>
105
  <div id="{sec}" class="section-body">
106
  {df_to_table(normalized[sec])}
 
108
  </div>
109
  """
110
 
111
+ # -------------------------------------------------------
112
+ # FINAL HTML
113
+ # -------------------------------------------------------
114
  html = f"""
115
+ <!DOCTYPE html>
116
+ <html>
117
+ <head>
118
+ <meta charset="UTF-8">
119
+ <title>Equity Report - {symbol}</title>
120
+ <style>
121
+ body {{
122
+ font-family: Arial, sans-serif;
123
+ background: #fafafa;
124
+ margin: 0;
125
+ padding: 12px;
126
+ }}
127
+ h2 {{
128
+ color: #0366d6;
129
+ }}
130
+ .section {{
131
+ background: #fff;
132
+ border: 1px solid #ddd;
133
+ margin-bottom: 14px;
134
+ padding: 12px;
135
+ border-radius: 6px;
136
+ }}
137
+ .section-header {{
138
+ display: flex;
139
+ justify-content: space-between;
140
+ margin-bottom: 8px;
141
+ }}
142
+ .section-title {{
143
+ font-weight: bold;
144
+ color: #0b69a3;
145
+ }}
146
+ .toggle-btn {{
147
+ background: #0b69a3;
148
+ color: #fff;
149
+ border: none;
150
+ padding: 6px 10px;
151
+ font-size: 12px;
152
+ border-radius: 5px;
153
+ cursor: pointer;
154
+ }}
155
+ .tbl {{
156
+ border-collapse: collapse;
157
+ width: 100%;
158
+ font-size: 13px;
159
+ }}
160
+ .tbl th {{
161
+ background: #0b69a3;
162
+ color: #fff;
163
+ padding: 6px;
164
+ border: 1px solid #ccc;
165
+ }}
166
+ .tbl td {{
167
+ padding: 6px;
168
+ border: 1px solid #ccc;
169
+ }}
170
+ .empty {{
171
+ padding: 10px;
172
+ color: #777;
173
+ }}
174
+ </style>
175
+
176
+ <script>
177
+ function toggleSection(id) {{
178
+ let e = document.getElementById(id);
179
+ e.style.display = (e.style.display === "none") ? "block" : "none";
180
+ }}
181
+ </script>
182
+ </head>
183
+
184
+ <body>
185
+ <h2>Equity Report — {symbol}</h2>
186
+ {section_html}
187
+ </body>
188
+ </html>
189
+ """
190
+
191
+ # ---------- SAVE CACHE ----------
192
+ cache_save(cache_key, html)
 
 
193
  return html