eshan6704 commited on
Commit
2a1ebf2
·
verified ·
1 Parent(s): 4f453c6

Create eq_html.py

Browse files
Files changed (1) hide show
  1. app/eq_html.py +193 -0
app/eq_html.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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",
73
+ "priceInfo",
74
+ "industryInfo",
75
+ "pdSectorIndAll",
76
+ "info",
77
+ "preOpen",
78
+ "preOpenMarket"
79
+ ]
80
+
81
+ # Normalize to DataFrames
82
+ normalized = {}
83
+ for sec in section_order:
84
+ val = out.get(sec)
85
+ if isinstance(val, pd.DataFrame):
86
+ normalized[sec] = val
87
+ elif isinstance(val, list):
88
+ normalized[sec] = pd.DataFrame(val)
89
+ elif isinstance(val, dict):
90
+ normalized[sec] = pd.DataFrame([val])
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])}
107
+ </div>
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