eshan6704 commited on
Commit
3f24dea
·
verified ·
1 Parent(s): 618c884

Update yahooinfo.py

Browse files
Files changed (1) hide show
  1. yahooinfo.py +41 -16
yahooinfo.py CHANGED
@@ -37,14 +37,14 @@ MAIN_ICONS = {
37
  }
38
 
39
  # ==============================
40
- # Column layout wrapper
41
  # ==============================
42
- def column_layout(html, min_width=260):
43
  return f"""
44
  <div style="
45
  display:grid;
46
  grid-template-columns:repeat(auto-fit,minmax({min_width}px,1fr));
47
- gap:8px;
48
  align-items:start;
49
  ">
50
  {html}
@@ -89,7 +89,7 @@ def html_card(title, body, mini=False, shade=0):
89
  """
90
 
91
  # ==============================
92
- # Number formatting
93
  # ==============================
94
  def format_number(x):
95
  try:
@@ -103,18 +103,15 @@ def format_number(x):
103
  return str(x)
104
 
105
  # ==============================
106
- # Compact inline table (Field : Value)
107
  # ==============================
108
  def make_table(df):
109
  rows = ""
110
  for _, r in df.iterrows():
111
- val = r[1]
112
  color = "#0d1f3c"
113
-
114
- # Highlight change values
115
  if any(x in r[0].lower() for x in ["chg", "%"]):
116
  try:
117
- color = "#0a7d32" if float(val) >= 0 else "#b00020"
118
  except:
119
  pass
120
 
@@ -137,11 +134,10 @@ def make_table(df):
137
  border-radius:4px;
138
  white-space:nowrap;
139
  ">
140
- {val}
141
  </span>
142
  </div>
143
  """
144
-
145
  return f"<div>{rows}</div>"
146
 
147
  # ==============================
@@ -158,7 +154,7 @@ def is_noise(k):
158
  return k in NOISE_KEYS
159
 
160
  # ==============================
161
- # Duplicate resolution
162
  # ==============================
163
  DUPLICATE_PRIORITY = {
164
  "price": ["regularMarketPrice","currentPrice"],
@@ -237,6 +233,25 @@ def build_grouped_info(info):
237
  groups[classify_key(k,v)][k] = v
238
  return groups
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  # ==============================
241
  # DataFrame builder
242
  # ==============================
@@ -288,17 +303,27 @@ def fetch_info(symbol):
288
 
289
  # -------- FUNDAMENTALS --------
290
  if groups["fundamental"]:
 
 
 
 
 
291
  html += html_card(
292
  f"{MAIN_ICONS['Fundamentals']} Fundamentals",
293
- make_table(build_df_from_dict(groups["fundamental"])),
294
  shade=1
295
  )
296
 
297
- # -------- PROFILE --------
298
  if groups["profile"]:
 
 
 
 
 
299
  html += html_card(
300
  f"{MAIN_ICONS['Company Profile']} Company Profile",
301
- make_table(build_df_from_dict(groups["profile"])),
302
  shade=2
303
  )
304
 
@@ -308,5 +333,5 @@ def fetch_info(symbol):
308
 
309
  return html
310
 
311
- except Exception as e:
312
  return f"<pre>{traceback.format_exc()}</pre>"
 
37
  }
38
 
39
  # ==============================
40
+ # Responsive column layout
41
  # ==============================
42
+ def column_layout(html, min_width=320):
43
  return f"""
44
  <div style="
45
  display:grid;
46
  grid-template-columns:repeat(auto-fit,minmax({min_width}px,1fr));
47
+ gap:10px;
48
  align-items:start;
49
  ">
50
  {html}
 
89
  """
90
 
91
  # ==============================
92
+ # Formatting
93
  # ==============================
94
  def format_number(x):
95
  try:
 
103
  return str(x)
104
 
105
  # ==============================
106
+ # Compact inline key:value view
107
  # ==============================
108
  def make_table(df):
109
  rows = ""
110
  for _, r in df.iterrows():
 
111
  color = "#0d1f3c"
 
 
112
  if any(x in r[0].lower() for x in ["chg", "%"]):
113
  try:
114
+ color = "#0a7d32" if float(r[1]) >= 0 else "#b00020"
115
  except:
116
  pass
117
 
 
134
  border-radius:4px;
135
  white-space:nowrap;
136
  ">
137
+ {r[1]}
138
  </span>
139
  </div>
140
  """
 
141
  return f"<div>{rows}</div>"
142
 
143
  # ==============================
 
154
  return k in NOISE_KEYS
155
 
156
  # ==============================
157
+ # Duplicate resolver
158
  # ==============================
159
  DUPLICATE_PRIORITY = {
160
  "price": ["regularMarketPrice","currentPrice"],
 
233
  groups[classify_key(k,v)][k] = v
234
  return groups
235
 
236
+ # ==============================
237
+ # Balanced column splitter (UTILISATION FIRST)
238
+ # ==============================
239
+ def split_df_evenly(df):
240
+ if df is None or df.empty:
241
+ return []
242
+
243
+ n = len(df)
244
+
245
+ if n <= 6:
246
+ cols = 1
247
+ elif n <= 14:
248
+ cols = 2
249
+ else:
250
+ cols = 3
251
+
252
+ chunk = (n + cols - 1) // cols
253
+ return [df.iloc[i:i+chunk] for i in range(0, n, chunk)]
254
+
255
  # ==============================
256
  # DataFrame builder
257
  # ==============================
 
303
 
304
  # -------- FUNDAMENTALS --------
305
  if groups["fundamental"]:
306
+ chunks = split_df_evenly(build_df_from_dict(groups["fundamental"]))
307
+ cols = "".join(
308
+ html_card("📊 Fundamentals", make_table(c), mini=True, shade=i)
309
+ for i,c in enumerate(chunks)
310
+ )
311
  html += html_card(
312
  f"{MAIN_ICONS['Fundamentals']} Fundamentals",
313
+ column_layout(cols),
314
  shade=1
315
  )
316
 
317
+ # -------- COMPANY PROFILE --------
318
  if groups["profile"]:
319
+ chunks = split_df_evenly(build_df_from_dict(groups["profile"]))
320
+ cols = "".join(
321
+ html_card("🏢 Profile", make_table(c), mini=True, shade=i)
322
+ for i,c in enumerate(chunks)
323
+ )
324
  html += html_card(
325
  f"{MAIN_ICONS['Company Profile']} Company Profile",
326
+ column_layout(cols),
327
  shade=2
328
  )
329
 
 
333
 
334
  return html
335
 
336
+ except Exception:
337
  return f"<pre>{traceback.format_exc()}</pre>"