Spaces:
Running
Running
Update app/index_live_html.py
Browse files- app/index_live_html.py +33 -24
app/index_live_html.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from . import nsepythonmodified as ns
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime as dt
|
|
|
|
| 4 |
|
| 5 |
# persist helpers (HF only)
|
| 6 |
from .persist import exists, load, save
|
|
@@ -69,27 +70,37 @@ def build_index_live_html(index_name ="NIFTY 50"):
|
|
| 69 |
const_df = const_df.sort_values("pChange", ascending=False)
|
| 70 |
|
| 71 |
# ================= HTML HELPERS =================
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
-
Format numeric
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
"""
|
|
|
|
|
|
|
| 80 |
if val == 0:
|
| 81 |
return "0"
|
| 82 |
-
|
| 83 |
if abs(val) < 0.001:
|
| 84 |
-
|
| 85 |
-
return f"{val_display:.3f}"
|
| 86 |
|
| 87 |
-
# Crore formatting for large values
|
| 88 |
if abs(val) >= 1e7:
|
| 89 |
-
|
| 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):
|
|
@@ -105,18 +116,18 @@ def build_index_live_html(index_name ="NIFTY 50"):
|
|
| 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 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
cls = "numeric-positive"
|
| 116 |
-
elif
|
| 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"
|
|
@@ -125,7 +136,7 @@ def build_index_live_html(index_name ="NIFTY 50"):
|
|
| 125 |
|
| 126 |
df_html.at[idx, col] = f'<span class="{cls.strip()}">{val_str}</span>'
|
| 127 |
else:
|
| 128 |
-
#
|
| 129 |
df_html.at[idx, col] = str(val)
|
| 130 |
|
| 131 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
|
@@ -240,7 +251,5 @@ th, td {{ border: 1px solid #bbb; padding: 5px 8px; }}
|
|
| 240 |
</html>
|
| 241 |
"""
|
| 242 |
|
| 243 |
-
# ================= SAVE (HTML ONLY) =================
|
| 244 |
save(cache_name, html_out, "html")
|
| 245 |
-
|
| 246 |
return html_out
|
|
|
|
| 1 |
from . import nsepythonmodified as ns
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime as dt
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
# persist helpers (HF only)
|
| 7 |
from .persist import exists, load, save
|
|
|
|
| 70 |
const_df = const_df.sort_values("pChange", ascending=False)
|
| 71 |
|
| 72 |
# ================= HTML HELPERS =================
|
| 73 |
+
|
| 74 |
+
def is_pure_number(s):
|
| 75 |
+
"""
|
| 76 |
+
Returns True if s is a pure numeric string (int or decimal),
|
| 77 |
+
False if it contains letters or non-numeric characters.
|
| 78 |
+
"""
|
| 79 |
+
try:
|
| 80 |
+
float(s)
|
| 81 |
+
except (ValueError, TypeError):
|
| 82 |
+
return False
|
| 83 |
+
return bool(re.fullmatch(r'-?\d+(\.\d+)?', str(s)))
|
| 84 |
+
|
| 85 |
+
def format_number_string(val_str):
|
| 86 |
"""
|
| 87 |
+
Format a pure numeric string according to:
|
| 88 |
+
0 => "0"
|
| 89 |
+
|val| < 0.001 => "±0.001"
|
| 90 |
+
large (>=1e7) => Crore
|
| 91 |
+
else => up to 3 decimals
|
| 92 |
"""
|
| 93 |
+
val = float(val_str)
|
| 94 |
+
|
| 95 |
if val == 0:
|
| 96 |
return "0"
|
| 97 |
+
|
| 98 |
if abs(val) < 0.001:
|
| 99 |
+
val = 0.001 if val > 0 else -0.001
|
|
|
|
| 100 |
|
|
|
|
| 101 |
if abs(val) >= 1e7:
|
| 102 |
+
return f"{val/1e7:.2f} Cr"
|
|
|
|
| 103 |
|
|
|
|
| 104 |
return f"{val:.3f}"
|
| 105 |
|
| 106 |
def df_to_html_color(df, metric_col=None):
|
|
|
|
| 116 |
for col in df_html.columns:
|
| 117 |
val = row[col]
|
| 118 |
cls = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
# Only format pure numeric content
|
| 121 |
+
if is_pure_number(val):
|
| 122 |
+
val_str = format_number_string(val)
|
| 123 |
+
|
| 124 |
+
# assign classes
|
| 125 |
+
num_val = float(val)
|
| 126 |
+
if num_val > 0:
|
| 127 |
cls = "numeric-positive"
|
| 128 |
+
elif num_val < 0:
|
| 129 |
cls = "numeric-negative"
|
| 130 |
|
|
|
|
| 131 |
if metric_col and col == metric_col:
|
| 132 |
if idx in top_up:
|
| 133 |
cls += " top-up"
|
|
|
|
| 136 |
|
| 137 |
df_html.at[idx, col] = f'<span class="{cls.strip()}">{val_str}</span>'
|
| 138 |
else:
|
| 139 |
+
# text stays exactly as-is
|
| 140 |
df_html.at[idx, col] = str(val)
|
| 141 |
|
| 142 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
|
|
|
| 251 |
</html>
|
| 252 |
"""
|
| 253 |
|
|
|
|
| 254 |
save(cache_name, html_out, "html")
|
|
|
|
| 255 |
return html_out
|