Spaces:
Running
Running
Update app/index_live_html.py
Browse files- app/index_live_html.py +33 -2
app/index_live_html.py
CHANGED
|
@@ -69,6 +69,29 @@ def build_index_live_html(index_name ="NIFTY 50"):
|
|
| 69 |
const_df = const_df.sort_values("pChange", ascending=False)
|
| 70 |
|
| 71 |
# ================= HTML HELPERS =================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def df_to_html_color(df, metric_col=None):
|
| 73 |
df_html = df.copy()
|
| 74 |
top_up, top_down = [], []
|
|
@@ -82,19 +105,27 @@ def build_index_live_html(index_name ="NIFTY 50"):
|
|
| 82 |
for col in df_html.columns:
|
| 83 |
val = row[col]
|
| 84 |
cls = ""
|
|
|
|
| 85 |
if isinstance(val, (int, float)):
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
if val > 0:
|
| 88 |
cls = "numeric-positive"
|
| 89 |
elif val < 0:
|
| 90 |
cls = "numeric-negative"
|
|
|
|
|
|
|
| 91 |
if metric_col and col == metric_col:
|
| 92 |
if idx in top_up:
|
| 93 |
cls += " top-up"
|
| 94 |
elif idx in top_down:
|
| 95 |
cls += " top-down"
|
| 96 |
-
|
|
|
|
| 97 |
else:
|
|
|
|
| 98 |
df_html.at[idx, col] = str(val)
|
| 99 |
|
| 100 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
|
|
|
| 69 |
const_df = const_df.sort_values("pChange", ascending=False)
|
| 70 |
|
| 71 |
# ================= HTML HELPERS =================
|
| 72 |
+
def format_number(val):
|
| 73 |
+
"""
|
| 74 |
+
Format numeric values:
|
| 75 |
+
- 0 => '0'
|
| 76 |
+
- |val| < 0.001 => ±0.001
|
| 77 |
+
- large numbers >= 1e7 => Crore format 'xx.xx Cr'
|
| 78 |
+
- other => up to 3 decimals
|
| 79 |
+
"""
|
| 80 |
+
if val == 0:
|
| 81 |
+
return "0"
|
| 82 |
+
# enforce minimum non-zero magnitude
|
| 83 |
+
if abs(val) < 0.001:
|
| 84 |
+
val_display = 0.001 if val > 0 else -0.001
|
| 85 |
+
return f"{val_display:.3f}"
|
| 86 |
+
|
| 87 |
+
# Crore formatting for large values
|
| 88 |
+
if abs(val) >= 1e7:
|
| 89 |
+
crores = val / 1e7
|
| 90 |
+
return f"{crores:.2f} Cr"
|
| 91 |
+
|
| 92 |
+
# normal numeric formatting with up to 3 decimals
|
| 93 |
+
return f"{val:.3f}"
|
| 94 |
+
|
| 95 |
def df_to_html_color(df, metric_col=None):
|
| 96 |
df_html = df.copy()
|
| 97 |
top_up, top_down = [], []
|
|
|
|
| 105 |
for col in df_html.columns:
|
| 106 |
val = row[col]
|
| 107 |
cls = ""
|
| 108 |
+
# only handle numeric types
|
| 109 |
if isinstance(val, (int, float)):
|
| 110 |
+
# format with the custom formatter
|
| 111 |
+
val_str = format_number(val)
|
| 112 |
+
|
| 113 |
+
# apply positive/negative classes
|
| 114 |
if val > 0:
|
| 115 |
cls = "numeric-positive"
|
| 116 |
elif val < 0:
|
| 117 |
cls = "numeric-negative"
|
| 118 |
+
|
| 119 |
+
# highlight top/bottom if metric
|
| 120 |
if metric_col and col == metric_col:
|
| 121 |
if idx in top_up:
|
| 122 |
cls += " top-up"
|
| 123 |
elif idx in top_down:
|
| 124 |
cls += " top-down"
|
| 125 |
+
|
| 126 |
+
df_html.at[idx, col] = f'<span class="{cls.strip()}">{val_str}</span>'
|
| 127 |
else:
|
| 128 |
+
# leave text values untouched
|
| 129 |
df_html.at[idx, col] = str(val)
|
| 130 |
|
| 131 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|