Update common.py
Browse files
common.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import traceback
|
| 4 |
|
| 5 |
# ============================================================
|
|
@@ -7,7 +8,6 @@ import traceback
|
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
def format_number(num):
|
| 10 |
-
"""Format numbers with commas, handle None, remove trailing zeros."""
|
| 11 |
if num is None:
|
| 12 |
return "-"
|
| 13 |
try:
|
|
@@ -15,9 +15,7 @@ def format_number(num):
|
|
| 15 |
except:
|
| 16 |
return str(num)
|
| 17 |
|
| 18 |
-
|
| 19 |
def format_large_number(num):
|
| 20 |
-
"""Format large numbers into K, Lakh, Crore."""
|
| 21 |
if num is None:
|
| 22 |
return "-"
|
| 23 |
try:
|
|
@@ -38,7 +36,6 @@ def format_large_number(num):
|
|
| 38 |
# ============================================================
|
| 39 |
|
| 40 |
def html_card(title, content):
|
| 41 |
-
"""Beautiful card-style container."""
|
| 42 |
return f"""
|
| 43 |
<div style="
|
| 44 |
background:#fff;
|
|
@@ -53,9 +50,7 @@ def html_card(title, content):
|
|
| 53 |
</div>
|
| 54 |
"""
|
| 55 |
|
| 56 |
-
|
| 57 |
def html_section(title, content):
|
| 58 |
-
"""Simple titled section."""
|
| 59 |
return f"""
|
| 60 |
<div style="margin:20px 0;">
|
| 61 |
<h3 style="color:#444;margin-bottom:8px;">{title}</h3>
|
|
@@ -63,9 +58,7 @@ def html_section(title, content):
|
|
| 63 |
</div>
|
| 64 |
"""
|
| 65 |
|
| 66 |
-
|
| 67 |
def html_error(msg):
|
| 68 |
-
"""Red error block with message."""
|
| 69 |
return f"""
|
| 70 |
<div style="
|
| 71 |
padding:15px;
|
|
@@ -84,7 +77,6 @@ def html_error(msg):
|
|
| 84 |
# ============================================================
|
| 85 |
|
| 86 |
def clean_df(df):
|
| 87 |
-
"""Standard cleanup for all results."""
|
| 88 |
if isinstance(df.index, pd.DatetimeIndex):
|
| 89 |
df.index = df.index.strftime("%Y-%m-%d")
|
| 90 |
df.replace([np.inf, -np.inf], np.nan, inplace=True)
|
|
@@ -96,12 +88,10 @@ def clean_df(df):
|
|
| 96 |
# ============================================================
|
| 97 |
|
| 98 |
def make_table(df):
|
| 99 |
-
"""Convert DataFrame to pretty HTML table."""
|
| 100 |
try:
|
| 101 |
df = df.copy()
|
| 102 |
df = clean_df(df)
|
| 103 |
html = df.to_html(classes="styled-table", escape=False, border=0)
|
| 104 |
-
|
| 105 |
return f"""
|
| 106 |
<style>
|
| 107 |
.styled-table {{
|
|
@@ -136,7 +126,6 @@ def make_table(df):
|
|
| 136 |
# ============================================================
|
| 137 |
|
| 138 |
def wrap_plotly_html(html_chart, table_html=None):
|
| 139 |
-
"""Wrap chart + optional table into same styled page."""
|
| 140 |
extra = f"<div style='margin-top:20px'>{table_html}</div>" if table_html else ""
|
| 141 |
return f"""
|
| 142 |
<div style="width:98%;margin:auto;">
|
|
@@ -150,16 +139,48 @@ def wrap_plotly_html(html_chart, table_html=None):
|
|
| 150 |
# ============================================================
|
| 151 |
|
| 152 |
def safe_get(df, key, default_val="-"):
|
| 153 |
-
"""Sometimes JSON keys are missing; avoid crash."""
|
| 154 |
try:
|
| 155 |
return df.get(key, default_val)
|
| 156 |
except:
|
| 157 |
return default_val
|
|
|
|
| 158 |
def format_timestamp_to_date(timestamp):
|
| 159 |
if not isinstance(timestamp, (int, float)) or timestamp <= 0:
|
| 160 |
return "N/A"
|
| 161 |
try:
|
| 162 |
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d')
|
| 163 |
-
except
|
| 164 |
return "Invalid Date"
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
+
import datetime
|
| 4 |
import traceback
|
| 5 |
|
| 6 |
# ============================================================
|
|
|
|
| 8 |
# ============================================================
|
| 9 |
|
| 10 |
def format_number(num):
|
|
|
|
| 11 |
if num is None:
|
| 12 |
return "-"
|
| 13 |
try:
|
|
|
|
| 15 |
except:
|
| 16 |
return str(num)
|
| 17 |
|
|
|
|
| 18 |
def format_large_number(num):
|
|
|
|
| 19 |
if num is None:
|
| 20 |
return "-"
|
| 21 |
try:
|
|
|
|
| 36 |
# ============================================================
|
| 37 |
|
| 38 |
def html_card(title, content):
|
|
|
|
| 39 |
return f"""
|
| 40 |
<div style="
|
| 41 |
background:#fff;
|
|
|
|
| 50 |
</div>
|
| 51 |
"""
|
| 52 |
|
|
|
|
| 53 |
def html_section(title, content):
|
|
|
|
| 54 |
return f"""
|
| 55 |
<div style="margin:20px 0;">
|
| 56 |
<h3 style="color:#444;margin-bottom:8px;">{title}</h3>
|
|
|
|
| 58 |
</div>
|
| 59 |
"""
|
| 60 |
|
|
|
|
| 61 |
def html_error(msg):
|
|
|
|
| 62 |
return f"""
|
| 63 |
<div style="
|
| 64 |
padding:15px;
|
|
|
|
| 77 |
# ============================================================
|
| 78 |
|
| 79 |
def clean_df(df):
|
|
|
|
| 80 |
if isinstance(df.index, pd.DatetimeIndex):
|
| 81 |
df.index = df.index.strftime("%Y-%m-%d")
|
| 82 |
df.replace([np.inf, -np.inf], np.nan, inplace=True)
|
|
|
|
| 88 |
# ============================================================
|
| 89 |
|
| 90 |
def make_table(df):
|
|
|
|
| 91 |
try:
|
| 92 |
df = df.copy()
|
| 93 |
df = clean_df(df)
|
| 94 |
html = df.to_html(classes="styled-table", escape=False, border=0)
|
|
|
|
| 95 |
return f"""
|
| 96 |
<style>
|
| 97 |
.styled-table {{
|
|
|
|
| 126 |
# ============================================================
|
| 127 |
|
| 128 |
def wrap_plotly_html(html_chart, table_html=None):
|
|
|
|
| 129 |
extra = f"<div style='margin-top:20px'>{table_html}</div>" if table_html else ""
|
| 130 |
return f"""
|
| 131 |
<div style="width:98%;margin:auto;">
|
|
|
|
| 139 |
# ============================================================
|
| 140 |
|
| 141 |
def safe_get(df, key, default_val="-"):
|
|
|
|
| 142 |
try:
|
| 143 |
return df.get(key, default_val)
|
| 144 |
except:
|
| 145 |
return default_val
|
| 146 |
+
|
| 147 |
def format_timestamp_to_date(timestamp):
|
| 148 |
if not isinstance(timestamp, (int, float)) or timestamp <= 0:
|
| 149 |
return "N/A"
|
| 150 |
try:
|
| 151 |
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d')
|
| 152 |
+
except:
|
| 153 |
return "Invalid Date"
|
| 154 |
|
| 155 |
+
# ============================================================
|
| 156 |
+
# HTML WRAPPER
|
| 157 |
+
# ============================================================
|
| 158 |
+
|
| 159 |
+
STYLE_BLOCK = """
|
| 160 |
+
<style>
|
| 161 |
+
.styled-table {border-collapse: collapse; margin: 10px 0; font-size: 0.9em; font-family: sans-serif; width: 100%; box-shadow: 0 0 10px rgba(0,0,0,0.1);}
|
| 162 |
+
.styled-table th, .styled-table td {padding: 8px 10px; border: 1px solid #ddd;}
|
| 163 |
+
.styled-table tbody tr:nth-child(even) {background-color: #f9f9f9;}
|
| 164 |
+
.card {display: block; width: 95%; margin: 10px auto; padding: 15px; border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); background: #fafafa;}
|
| 165 |
+
.card-category-title {font-size: 1.1em; color: #222; margin: 0 0 8px; border-bottom: 1px solid #eee; padding-bottom: 5px;}
|
| 166 |
+
.card-content-grid {display: flex; flex-wrap: wrap; gap: 15px;}
|
| 167 |
+
.key-value-pair {flex: 1 1 calc(20% - 15px); box-sizing: border-box; min-width: 150px; background: #fff; padding: 10px; border: 1px solid #e0e0e0; border-radius: 5px; box-shadow: 0 1px 3px rgba(0,0,0,0.05);}
|
| 168 |
+
.key-value-pair h3 {font-size: 0.95em; color: #444; margin: 0 0 5px 0;}
|
| 169 |
+
.key-value-pair p {font-size: 0.9em; color: #555; margin: 0; font-weight: bold;}
|
| 170 |
+
.big-box {width:95%; margin:20px auto; padding:20px; border:1px solid #ccc; border-radius:8px; background:#fff; box-shadow:0 2px 8px rgba(0,0,0,0.1); font-size:0.95em; line-height:1.4em; max-height:400px; overflow-y:auto;}
|
| 171 |
+
</style>
|
| 172 |
+
"""
|
| 173 |
+
|
| 174 |
+
def wrap_html(content, title="Stock Data"):
|
| 175 |
+
return f"""
|
| 176 |
+
<!DOCTYPE html>
|
| 177 |
+
<html>
|
| 178 |
+
<head>
|
| 179 |
+
<title>{title}</title>
|
| 180 |
+
{STYLE_BLOCK}
|
| 181 |
+
</head>
|
| 182 |
+
<body>
|
| 183 |
+
{content}
|
| 184 |
+
</body>
|
| 185 |
+
</html>
|
| 186 |
+
"""
|