Update preopen_html.py
Browse files- preopen_html.py +29 -79
preopen_html.py
CHANGED
|
@@ -3,19 +3,25 @@ import pandas as pd
|
|
| 3 |
|
| 4 |
def build_preopen_html(key="NIFTY"):
|
| 5 |
# Fetch pre-open data
|
| 6 |
-
p = nsefetch("https://www.nseindia.com/api/market-data-pre-open?key="
|
| 7 |
-
|
| 8 |
data_df = df_from_data(p.pop("data"))
|
| 9 |
rem_df = df_from_data([p])
|
| 10 |
|
| 11 |
main_df = data_df.iloc[[0]] if not data_df.empty else pd.DataFrame()
|
| 12 |
const_df = data_df.iloc[1:] if len(data_df) > 1 else pd.DataFrame()
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# ================= HELPER FUNCTION =================
|
| 15 |
def df_to_html_color(df, metric_col=None):
|
| 16 |
df_html = df.copy()
|
| 17 |
-
top3_up = []
|
| 18 |
-
top3_down = []
|
| 19 |
if metric_col and metric_col in df_html.columns and pd.api.types.is_numeric_dtype(df_html[metric_col]):
|
| 20 |
col_numeric = df_html[metric_col].dropna()
|
| 21 |
top3_up = col_numeric.nlargest(3).index.tolist()
|
|
@@ -25,13 +31,13 @@ def build_preopen_html(key="NIFTY"):
|
|
| 25 |
for col in df_html.columns:
|
| 26 |
val = row[col]
|
| 27 |
style = ""
|
| 28 |
-
if
|
| 29 |
val_fmt = f"{val:.2f}"
|
| 30 |
if val > 0:
|
| 31 |
style = "numeric-positive"
|
| 32 |
elif val < 0:
|
| 33 |
style = "numeric-negative"
|
| 34 |
-
if metric_col
|
| 35 |
if idx in top3_up:
|
| 36 |
style += " top-up"
|
| 37 |
elif idx in top3_down:
|
|
@@ -41,10 +47,12 @@ def build_preopen_html(key="NIFTY"):
|
|
| 41 |
df_html.at[idx, col] = str(val)
|
| 42 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
| 43 |
|
| 44 |
-
# =================
|
| 45 |
def merge_info_main_cards(rem_df, main_df):
|
| 46 |
combined = pd.concat([rem_df, main_df], axis=1)
|
| 47 |
combined = combined.loc[:, ~combined.columns.duplicated()]
|
|
|
|
|
|
|
| 48 |
cards_html = '<div class="mini-card-container">'
|
| 49 |
for col in combined.columns:
|
| 50 |
val = combined.at[0, col] if not combined.empty else ""
|
|
@@ -59,11 +67,11 @@ def build_preopen_html(key="NIFTY"):
|
|
| 59 |
|
| 60 |
info_cards_html = merge_info_main_cards(rem_df, main_df)
|
| 61 |
|
| 62 |
-
# Constituents table
|
| 63 |
cons_html = df_to_html_color(const_df) if not const_df.empty else "<i>No pre-open constituents</i>"
|
| 64 |
|
| 65 |
-
# Metric tables
|
| 66 |
-
metric_cols = [c for c in const_df.columns if pd.api.types.is_numeric_dtype(const_df[c])] if not const_df.empty else []
|
| 67 |
metric_tables = ""
|
| 68 |
for col in metric_cols:
|
| 69 |
df_const = const_df.copy()
|
|
@@ -84,79 +92,21 @@ def build_preopen_html(key="NIFTY"):
|
|
| 84 |
<head>
|
| 85 |
<meta charset="UTF-8">
|
| 86 |
<style>
|
| 87 |
-
body {{
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
font-size: 14px;
|
| 93 |
-
}}
|
| 94 |
-
h2, h3 {{
|
| 95 |
-
margin: 12px 0 6px 0;
|
| 96 |
-
font-weight: 600;
|
| 97 |
-
}}
|
| 98 |
-
table {{
|
| 99 |
-
border-collapse: collapse;
|
| 100 |
-
width: 100%;
|
| 101 |
-
table-layout: auto;
|
| 102 |
-
}}
|
| 103 |
-
th, td {{
|
| 104 |
-
border: 1px solid #bbb;
|
| 105 |
-
padding: 5px 8px;
|
| 106 |
-
text-align: left;
|
| 107 |
-
font-size: 13px;
|
| 108 |
-
}}
|
| 109 |
-
th {{
|
| 110 |
-
background: #333;
|
| 111 |
-
color: white;
|
| 112 |
-
font-weight: 600;
|
| 113 |
-
}}
|
| 114 |
.compact-table td.numeric-positive {{ color: green; font-weight: bold; }}
|
| 115 |
.compact-table td.numeric-negative {{ color: red; font-weight: bold; }}
|
| 116 |
.compact-table td.top-up {{ background: #a8f0a5; }}
|
| 117 |
.compact-table td.top-down {{ background: #f0a8a8; }}
|
| 118 |
-
.small-table {{
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
overflow-y: auto;
|
| 125 |
-
}}
|
| 126 |
-
.st-title {{
|
| 127 |
-
font-size: 14px;
|
| 128 |
-
text-align: center;
|
| 129 |
-
margin-bottom: 6px;
|
| 130 |
-
font-weight: bold;
|
| 131 |
-
background: #222;
|
| 132 |
-
color: white;
|
| 133 |
-
padding: 5px 0;
|
| 134 |
-
border-radius: 4px;
|
| 135 |
-
}}
|
| 136 |
-
.st-body {{
|
| 137 |
-
max-height: 300px;
|
| 138 |
-
overflow-y: auto;
|
| 139 |
-
font-size: 12px;
|
| 140 |
-
}}
|
| 141 |
-
.grid {{
|
| 142 |
-
display: grid;
|
| 143 |
-
grid-template-columns: repeat(5, 1fr);
|
| 144 |
-
gap: 12px;
|
| 145 |
-
margin-top: 12px;
|
| 146 |
-
}}
|
| 147 |
-
.mini-card-container {{
|
| 148 |
-
display: flex;
|
| 149 |
-
flex-wrap: wrap;
|
| 150 |
-
gap: 10px;
|
| 151 |
-
}}
|
| 152 |
-
.mini-card {{
|
| 153 |
-
background: #fff;
|
| 154 |
-
padding: 8px 10px;
|
| 155 |
-
border-radius: 6px;
|
| 156 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.12);
|
| 157 |
-
min-width: 120px;
|
| 158 |
-
font-size: 13px;
|
| 159 |
-
}}
|
| 160 |
.card-key {{ font-weight: bold; color: #333; margin-bottom: 2px; }}
|
| 161 |
.card-val {{ color: #222; }}
|
| 162 |
</style>
|
|
|
|
| 3 |
|
| 4 |
def build_preopen_html(key="NIFTY"):
|
| 5 |
# Fetch pre-open data
|
| 6 |
+
p = nsefetch(f"https://www.nseindia.com/api/market-data-pre-open?key={key}")
|
|
|
|
| 7 |
data_df = df_from_data(p.pop("data"))
|
| 8 |
rem_df = df_from_data([p])
|
| 9 |
|
| 10 |
main_df = data_df.iloc[[0]] if not data_df.empty else pd.DataFrame()
|
| 11 |
const_df = data_df.iloc[1:] if len(data_df) > 1 else pd.DataFrame()
|
| 12 |
|
| 13 |
+
# ================= REMOVE ALL *_x COLUMNS =================
|
| 14 |
+
def remove_x_columns(df):
|
| 15 |
+
return df[[c for c in df.columns if not c.endswith("_x")]]
|
| 16 |
+
|
| 17 |
+
main_df = remove_x_columns(main_df)
|
| 18 |
+
const_df = remove_x_columns(const_df)
|
| 19 |
+
rem_df = remove_x_columns(rem_df)
|
| 20 |
+
|
| 21 |
# ================= HELPER FUNCTION =================
|
| 22 |
def df_to_html_color(df, metric_col=None):
|
| 23 |
df_html = df.copy()
|
| 24 |
+
top3_up, top3_down = [], []
|
|
|
|
| 25 |
if metric_col and metric_col in df_html.columns and pd.api.types.is_numeric_dtype(df_html[metric_col]):
|
| 26 |
col_numeric = df_html[metric_col].dropna()
|
| 27 |
top3_up = col_numeric.nlargest(3).index.tolist()
|
|
|
|
| 31 |
for col in df_html.columns:
|
| 32 |
val = row[col]
|
| 33 |
style = ""
|
| 34 |
+
if isinstance(val, (int, float)):
|
| 35 |
val_fmt = f"{val:.2f}"
|
| 36 |
if val > 0:
|
| 37 |
style = "numeric-positive"
|
| 38 |
elif val < 0:
|
| 39 |
style = "numeric-negative"
|
| 40 |
+
if metric_col == col:
|
| 41 |
if idx in top3_up:
|
| 42 |
style += " top-up"
|
| 43 |
elif idx in top3_down:
|
|
|
|
| 47 |
df_html.at[idx, col] = str(val)
|
| 48 |
return df_html.to_html(index=False, escape=False, classes="compact-table")
|
| 49 |
|
| 50 |
+
# ================= MINI-CARDS =================
|
| 51 |
def merge_info_main_cards(rem_df, main_df):
|
| 52 |
combined = pd.concat([rem_df, main_df], axis=1)
|
| 53 |
combined = combined.loc[:, ~combined.columns.duplicated()]
|
| 54 |
+
# Remove *_x columns
|
| 55 |
+
combined = combined[[c for c in combined.columns if not c.endswith("_x")]]
|
| 56 |
cards_html = '<div class="mini-card-container">'
|
| 57 |
for col in combined.columns:
|
| 58 |
val = combined.at[0, col] if not combined.empty else ""
|
|
|
|
| 67 |
|
| 68 |
info_cards_html = merge_info_main_cards(rem_df, main_df)
|
| 69 |
|
| 70 |
+
# ================= Constituents table =================
|
| 71 |
cons_html = df_to_html_color(const_df) if not const_df.empty else "<i>No pre-open constituents</i>"
|
| 72 |
|
| 73 |
+
# ================= Metric tables =================
|
| 74 |
+
metric_cols = [c for c in const_df.columns if pd.api.types.is_numeric_dtype(const_df[c]) and not c.endswith("_x")] if not const_df.empty else []
|
| 75 |
metric_tables = ""
|
| 76 |
for col in metric_cols:
|
| 77 |
df_const = const_df.copy()
|
|
|
|
| 92 |
<head>
|
| 93 |
<meta charset="UTF-8">
|
| 94 |
<style>
|
| 95 |
+
body {{ font-family: Arial; margin: 12px; background: #f5f5f5; color: #222; font-size: 14px; }}
|
| 96 |
+
h2, h3 {{ margin: 12px 0 6px 0; font-weight: 600; }}
|
| 97 |
+
table {{ border-collapse: collapse; width: 100%; table-layout: auto; }}
|
| 98 |
+
th, td {{ border: 1px solid #bbb; padding: 5px 8px; text-align: left; font-size: 13px; }}
|
| 99 |
+
th {{ background: #333; color: white; font-weight: 600; }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
.compact-table td.numeric-positive {{ color: green; font-weight: bold; }}
|
| 101 |
.compact-table td.numeric-negative {{ color: red; font-weight: bold; }}
|
| 102 |
.compact-table td.top-up {{ background: #a8f0a5; }}
|
| 103 |
.compact-table td.top-down {{ background: #f0a8a8; }}
|
| 104 |
+
.small-table {{ background: white; border-radius: 6px; padding: 8px; box-shadow: 0px 1px 4px rgba(0,0,0,0.15); border: 1px solid #ddd; overflow-y: auto; }}
|
| 105 |
+
.st-title {{ font-size: 14px; text-align: center; margin-bottom: 6px; font-weight: bold; background: #222; color: white; padding: 5px 0; border-radius: 4px; }}
|
| 106 |
+
.st-body {{ max-height: 300px; overflow-y: auto; font-size: 12px; }}
|
| 107 |
+
.grid {{ display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; margin-top: 12px; }}
|
| 108 |
+
.mini-card-container {{ display: flex; flex-wrap: wrap; gap: 10px; }}
|
| 109 |
+
.mini-card {{ background: #fff; padding: 8px 10px; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.12); min-width: 120px; font-size: 13px; }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
.card-key {{ font-weight: bold; color: #333; margin-bottom: 2px; }}
|
| 111 |
.card-val {{ color: #222; }}
|
| 112 |
</style>
|