eshan6704 commited on
Commit
989249d
·
verified ·
1 Parent(s): a23ca0e

Create eq_html.py

Browse files
Files changed (1) hide show
  1. eq_html.py +156 -0
eq_html.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from nsepython import *
2
+ def build_eq_html(symbol):
3
+ """Build full HTML page for eq(symbol) output, similar to build_indices_html."""
4
+
5
+ import json
6
+ import pandas as pd
7
+
8
+ # -------------------------------------------------------
9
+ # CALL eq() function internally
10
+ # -------------------------------------------------------
11
+ out = eq(symbol) # <-- your existing eq(symbol)
12
+ if not isinstance(out, dict):
13
+ return "<h3>Error: EQ data not available</h3>"
14
+
15
+ # -------------------------------------------------------
16
+ # Helper to convert DF → HTML table
17
+ # -------------------------------------------------------
18
+ def df_to_table(df):
19
+ if df is None or len(df) == 0:
20
+ return '<div class="empty">No data</div>'
21
+ return df.to_html(index=False, escape=False, border=0, classes="tbl")
22
+
23
+ # -------------------------------------------------------
24
+ # ORDER — metadata FIRST (date table)
25
+ # -------------------------------------------------------
26
+ section_order = [
27
+ "metadata",
28
+ "securityInfo",
29
+ "priceInfo",
30
+ "industryInfo",
31
+ "pdSectorIndAll",
32
+ "info",
33
+ "preOpen",
34
+ "preOpenMarket"
35
+ ]
36
+
37
+ # Normalize all values into DataFrame
38
+ normalized = {}
39
+ for sec in section_order:
40
+ val = out.get(sec, None)
41
+ if isinstance(val, pd.DataFrame):
42
+ normalized[sec] = val
43
+ elif isinstance(val, list):
44
+ normalized[sec] = pd.DataFrame(val)
45
+ elif isinstance(val, dict):
46
+ normalized[sec] = pd.DataFrame([val])
47
+ else:
48
+ normalized[sec] = pd.DataFrame()
49
+
50
+ # -------------------------------------------------------
51
+ # Build sections HTML
52
+ # -------------------------------------------------------
53
+ section_html = ""
54
+ for sec in section_order:
55
+ df = normalized[sec]
56
+ section_html += f"""
57
+ <div class="section">
58
+ <div class="section-header">
59
+ <div class="section-title">{sec}</div>
60
+ <button class="toggle-btn" onclick="toggleSection('{sec}')">View / Hide</button>
61
+ </div>
62
+ <div id="{sec}" class="section-body">
63
+ {df_to_table(df)}
64
+ </div>
65
+ </div>
66
+ """
67
+
68
+ # -------------------------------------------------------
69
+ # FINAL HTML PAGE
70
+ # -------------------------------------------------------
71
+ html = f"""
72
+ <html>
73
+ <head>
74
+ <style>
75
+ body {{
76
+ font-family: Arial, sans-serif;
77
+ background: #fafafa;
78
+ margin: 0;
79
+ padding: 12px;
80
+ }}
81
+ h2 {{
82
+ color: #0366d6;
83
+ margin-bottom: 10px;
84
+ }}
85
+ .section {{
86
+ background: #fff;
87
+ border: 1px solid #ddd;
88
+ margin-bottom: 14px;
89
+ padding: 12px;
90
+ border-radius: 6px;
91
+ }}
92
+ .section-header {{
93
+ display: flex;
94
+ justify-content: space-between;
95
+ margin-bottom: 8px;
96
+ }}
97
+ .section-title {{
98
+ font-weight: bold;
99
+ color: #0b69a3;
100
+ font-size: 15px;
101
+ }}
102
+ .toggle-btn {{
103
+ background: #0b69a3;
104
+ color: #fff;
105
+ border: none;
106
+ padding: 6px 10px;
107
+ font-size: 12px;
108
+ border-radius: 5px;
109
+ cursor: pointer;
110
+ }}
111
+ .section-body {{
112
+ margin-top: 6px;
113
+ }}
114
+ .tbl {{
115
+ border-collapse: collapse;
116
+ width: 100%;
117
+ font-size: 13px;
118
+ }}
119
+ .tbl th {{
120
+ background: #0b69a3;
121
+ color: #fff;
122
+ padding: 6px;
123
+ border: 1px solid #ccc;
124
+ }}
125
+ .tbl td {{
126
+ padding: 6px;
127
+ border: 1px solid #ccc;
128
+ }}
129
+ .empty {{
130
+ padding: 10px;
131
+ color: #777;
132
+ }}
133
+ </style>
134
+
135
+ <script>
136
+ function toggleSection(id) {{
137
+ let e = document.getElementById(id);
138
+ if (e.style.display === "none") {{
139
+ e.style.display = "block";
140
+ }} else {{
141
+ e.style.display = "none";
142
+ }}
143
+ }}
144
+ </script>
145
+ </head>
146
+
147
+ <body>
148
+ <h2>Equity Report — {symbol}</h2>
149
+
150
+ {section_html}
151
+
152
+ </body>
153
+ </html>
154
+ """
155
+
156
+ return html