eshan6704 commited on
Commit
bcf470f
·
verified ·
1 Parent(s): 71a405b

Update nse.py

Browse files
Files changed (1) hide show
  1. nse.py +49 -0
nse.py CHANGED
@@ -106,6 +106,7 @@ def indices():
106
  # ---------------------------------------------------
107
  # Specific Index → DataFrames
108
  # ---------------------------------------------------
 
109
  def open(index_name="NIFTY 50"):
110
  url = f"https://www.nseindia.com/api/equity-stockIndices?index={index_name.replace(' ', '%20')}"
111
  data = fetch_data(url)
@@ -134,6 +135,54 @@ def open(index_name="NIFTY 50"):
134
 
135
  return full_html
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  # ---------------------------------------------------
139
  # Option Chain DF (Raw CE/PE)
 
106
  # ---------------------------------------------------
107
  # Specific Index → DataFrames
108
  # ---------------------------------------------------
109
+ '''
110
  def open(index_name="NIFTY 50"):
111
  url = f"https://www.nseindia.com/api/equity-stockIndices?index={index_name.replace(' ', '%20')}"
112
  data = fetch_data(url)
 
135
 
136
  return full_html
137
 
138
+ '''
139
+ def open(index_name="NIFTY 50"):
140
+ url = f"https://www.nseindia.com/api/equity-stockIndices?index={index_name.replace(' ', '%20')}"
141
+ data = fetch_data(url)
142
+
143
+ if data is None:
144
+ return "<h3>No Data</h3>"
145
+
146
+ # Extract base parts
147
+ df_market = pd.DataFrame([data["marketStatus"]])
148
+ df_adv = pd.DataFrame([data["advance"]])
149
+ df_data = pd.DataFrame(data["data"])
150
+
151
+ # ------------------------------
152
+ # Merge child meta keys into df_data
153
+ # ------------------------------
154
+ def flatten_meta(row):
155
+ meta = row.get("meta", {})
156
+ flat = {}
157
+ for k, v in meta.items():
158
+ if isinstance(v, dict): # flatten one level
159
+ for ck, cv in v.items():
160
+ flat[f"{k}_{ck}"] = cv
161
+ else:
162
+ flat[k] = v
163
+ return pd.Series(flat)
164
+
165
+ # Expand meta into columns
166
+ df_meta_expanded = df_data.apply(flatten_meta, axis=1)
167
+
168
+ # Merge expanded meta into df_data
169
+ df_data = pd.concat([df_data.drop(columns=["meta"], errors="ignore"),
170
+ df_meta_expanded], axis=1)
171
+
172
+ # ------------------------------
173
+ # Convert to HTML
174
+ # ------------------------------
175
+ html_market = df_market.to_html(index=False, border=1)
176
+ html_adv = df_adv.to_html(index=False, border=1)
177
+ html_data = df_data.to_html(index=False, border=1)
178
+
179
+ full_html = (
180
+ "<h3>Market Status</h3>" + html_market +
181
+ "<br><h3>Advance/Decline</h3>" + html_adv +
182
+ "<br><h3>Index Data (with META merged)</h3>" + html_data
183
+ )
184
+
185
+ return full_html
186
 
187
  # ---------------------------------------------------
188
  # Option Chain DF (Raw CE/PE)