Update bhavcopy_html.py
Browse files- bhavcopy_html.py +51 -84
bhavcopy_html.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import datetime
|
| 3 |
from nsepython import *
|
| 4 |
-
from backblaze import upload_file
|
|
|
|
| 5 |
def build_bhavcopy_html(date_str):
|
| 6 |
# -------------------------------------------------------
|
| 7 |
# 1) Validate Date
|
|
@@ -15,32 +16,24 @@ def build_bhavcopy_html(date_str):
|
|
| 15 |
# 2) Fetch Bhavcopy
|
| 16 |
# -------------------------------------------------------
|
| 17 |
try:
|
| 18 |
-
df = nse_bhavcopy(date_str)
|
| 19 |
-
upload_files("eshanhf","bhav.csv",df.to_csv())
|
| 20 |
df.columns = df.columns.str.strip()
|
| 21 |
except:
|
| 22 |
return f"<h3>No Bhavcopy found for {date_str}.</h3>"
|
| 23 |
|
| 24 |
# -------------------------------------------------------
|
| 25 |
-
# 3) Drop unwanted columns
|
| 26 |
# -------------------------------------------------------
|
| 27 |
remove = ["DATE1", "LAST_PRICE", "AVG_PRICE"]
|
| 28 |
-
df.drop(columns=[
|
| 29 |
|
| 30 |
# -------------------------------------------------------
|
| 31 |
-
# 4) Convert numeric columns
|
| 32 |
# -------------------------------------------------------
|
| 33 |
numeric_cols = [
|
| 34 |
-
"PREV_CLOSE",
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"LOW_PRICE",
|
| 38 |
-
"CLOSE_PRICE",
|
| 39 |
-
"TTL_TRD_QNTY",
|
| 40 |
-
"TURNOVER_LACS",
|
| 41 |
-
"NO_OF_TRADES",
|
| 42 |
-
"DELIV_QTY",
|
| 43 |
-
"DELIV_PER"
|
| 44 |
]
|
| 45 |
|
| 46 |
for col in numeric_cols:
|
|
@@ -54,19 +47,34 @@ def build_bhavcopy_html(date_str):
|
|
| 54 |
df[col] = pd.to_numeric(df[col], errors="coerce").fillna(0)
|
| 55 |
|
| 56 |
# -------------------------------------------------------
|
| 57 |
-
# 5) Filter
|
| 58 |
# -------------------------------------------------------
|
| 59 |
df = df[df["TURNOVER_LACS"] > 1000]
|
| 60 |
-
df = df.sort_values(
|
|
|
|
| 61 |
# -------------------------------------------------------
|
| 62 |
-
# 6)
|
| 63 |
# -------------------------------------------------------
|
| 64 |
df["change"] = df["CLOSE_PRICE"] - df["PREV_CLOSE"]
|
| 65 |
df["perchange"] = (df["change"] / df["PREV_CLOSE"].replace(0, 1)) * 100
|
| 66 |
-
df["pergap"] = (
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# -------------------------------------------------------
|
| 69 |
-
# 7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
# -------------------------------------------------------
|
| 71 |
main_html = f"""
|
| 72 |
<div class="main-table-container">
|
|
@@ -74,84 +82,43 @@ def build_bhavcopy_html(date_str):
|
|
| 74 |
</div>
|
| 75 |
"""
|
| 76 |
|
| 77 |
-
# -------------------------------------------------------
|
| 78 |
-
# 8) GRID TABLE (SYMBOL vs metric)
|
| 79 |
-
# -------------------------------------------------------
|
| 80 |
metrics = ["perchange", "pergap", "TURNOVER_LACS", "NO_OF_TRADES", "DELIV_PER"]
|
| 81 |
-
existing_metrics = [m for m in metrics if m in df.columns]
|
| 82 |
-
|
| 83 |
col_html = []
|
| 84 |
-
for metric in existing_metrics:
|
| 85 |
-
temp_df = df[["SYMBOL", metric]].sort_values(metric, ascending=False)
|
| 86 |
-
col_html.append(
|
| 87 |
-
f"""
|
| 88 |
-
<div class="col">
|
| 89 |
-
<h4>{metric}</h4>
|
| 90 |
-
{temp_df.to_html(index=False, escape=False)}
|
| 91 |
-
</div>
|
| 92 |
-
"""
|
| 93 |
-
)
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
<div class="grid">
|
| 97 |
-
|
| 98 |
</div>
|
| 99 |
"""
|
| 100 |
|
| 101 |
-
# -------------------------------------------------------
|
| 102 |
-
# 9) CSS (improved header style)
|
| 103 |
-
# -------------------------------------------------------
|
| 104 |
css = """
|
| 105 |
<style>
|
| 106 |
-
.grid {
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
margin-top: 20px;
|
| 111 |
-
}
|
| 112 |
-
.col {
|
| 113 |
-
max-height: 480px;
|
| 114 |
-
overflow-y: scroll;
|
| 115 |
-
border: 1px solid #ccc;
|
| 116 |
-
padding: 4px;
|
| 117 |
-
background: #fafafa;
|
| 118 |
-
}
|
| 119 |
-
.main-table-container {
|
| 120 |
-
max-height: 480px;
|
| 121 |
-
overflow-y: scroll;
|
| 122 |
-
border: 1px solid #ccc;
|
| 123 |
-
padding: 4px;
|
| 124 |
-
background: #fff;
|
| 125 |
-
margin-bottom: 20px;
|
| 126 |
-
}
|
| 127 |
-
table {
|
| 128 |
-
font-size: 12px;
|
| 129 |
-
border-collapse: collapse;
|
| 130 |
-
width: 100%;
|
| 131 |
-
}
|
| 132 |
-
th, td {
|
| 133 |
-
padding: 4px 8px;
|
| 134 |
-
border: 1px solid #ddd;
|
| 135 |
}
|
|
|
|
|
|
|
| 136 |
th {
|
| 137 |
-
background:
|
| 138 |
-
|
| 139 |
-
font-weight: bold;
|
| 140 |
-
text-align: center;
|
| 141 |
-
position: sticky;
|
| 142 |
-
top: 0;
|
| 143 |
-
z-index: 3;
|
| 144 |
-
box-shadow: 0 2px 2px -1px rgba(0,0,0,0.4);
|
| 145 |
-
}
|
| 146 |
-
td {
|
| 147 |
-
text-align: right;
|
| 148 |
}
|
| 149 |
</style>
|
| 150 |
"""
|
| 151 |
|
| 152 |
-
# -------------------------------------------------------
|
| 153 |
-
# 10) Final Output
|
| 154 |
-
# -------------------------------------------------------
|
| 155 |
return (
|
| 156 |
css +
|
| 157 |
"<h2>Main Bhavcopy Table</h2>" +
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import datetime
|
| 3 |
from nsepython import *
|
| 4 |
+
from backblaze import upload_file
|
| 5 |
+
|
| 6 |
def build_bhavcopy_html(date_str):
|
| 7 |
# -------------------------------------------------------
|
| 8 |
# 1) Validate Date
|
|
|
|
| 16 |
# 2) Fetch Bhavcopy
|
| 17 |
# -------------------------------------------------------
|
| 18 |
try:
|
| 19 |
+
df = nse_bhavcopy(date_str)
|
|
|
|
| 20 |
df.columns = df.columns.str.strip()
|
| 21 |
except:
|
| 22 |
return f"<h3>No Bhavcopy found for {date_str}.</h3>"
|
| 23 |
|
| 24 |
# -------------------------------------------------------
|
| 25 |
+
# 3) Drop unwanted columns
|
| 26 |
# -------------------------------------------------------
|
| 27 |
remove = ["DATE1", "LAST_PRICE", "AVG_PRICE"]
|
| 28 |
+
df.drop(columns=[c for c in remove if c in df.columns], inplace=True)
|
| 29 |
|
| 30 |
# -------------------------------------------------------
|
| 31 |
+
# 4) Convert numeric columns
|
| 32 |
# -------------------------------------------------------
|
| 33 |
numeric_cols = [
|
| 34 |
+
"PREV_CLOSE", "OPEN_PRICE", "HIGH_PRICE", "LOW_PRICE",
|
| 35 |
+
"CLOSE_PRICE", "TTL_TRD_QNTY", "TURNOVER_LACS",
|
| 36 |
+
"NO_OF_TRADES", "DELIV_QTY", "DELIV_PER"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
]
|
| 38 |
|
| 39 |
for col in numeric_cols:
|
|
|
|
| 47 |
df[col] = pd.to_numeric(df[col], errors="coerce").fillna(0)
|
| 48 |
|
| 49 |
# -------------------------------------------------------
|
| 50 |
+
# 5) Filter & sort
|
| 51 |
# -------------------------------------------------------
|
| 52 |
df = df[df["TURNOVER_LACS"] > 1000]
|
| 53 |
+
df = df.sort_values("TURNOVER_LACS", ascending=False)
|
| 54 |
+
|
| 55 |
# -------------------------------------------------------
|
| 56 |
+
# 6) Computed columns
|
| 57 |
# -------------------------------------------------------
|
| 58 |
df["change"] = df["CLOSE_PRICE"] - df["PREV_CLOSE"]
|
| 59 |
df["perchange"] = (df["change"] / df["PREV_CLOSE"].replace(0, 1)) * 100
|
| 60 |
+
df["pergap"] = (
|
| 61 |
+
(df["OPEN_PRICE"] - df["PREV_CLOSE"]) /
|
| 62 |
+
df["PREV_CLOSE"].replace(0, 1)
|
| 63 |
+
) * 100
|
| 64 |
|
| 65 |
# -------------------------------------------------------
|
| 66 |
+
# 7) Upload to Backblaze (FINAL DF)
|
| 67 |
+
# -------------------------------------------------------
|
| 68 |
+
file_name = f"bhav/bhav_{date_str.replace('-', '_')}.csv"
|
| 69 |
+
|
| 70 |
+
upload_file(
|
| 71 |
+
bucket_name="eshanhf",
|
| 72 |
+
file_name=file_name,
|
| 73 |
+
file_content=df
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# -------------------------------------------------------
|
| 77 |
+
# 8) HTML Output
|
| 78 |
# -------------------------------------------------------
|
| 79 |
main_html = f"""
|
| 80 |
<div class="main-table-container">
|
|
|
|
| 82 |
</div>
|
| 83 |
"""
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
metrics = ["perchange", "pergap", "TURNOVER_LACS", "NO_OF_TRADES", "DELIV_PER"]
|
|
|
|
|
|
|
| 86 |
col_html = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
for m in metrics:
|
| 89 |
+
if m in df.columns:
|
| 90 |
+
temp = df[["SYMBOL", m]].sort_values(m, ascending=False)
|
| 91 |
+
col_html.append(
|
| 92 |
+
f"""
|
| 93 |
+
<div class="col">
|
| 94 |
+
<h4>{m}</h4>
|
| 95 |
+
{temp.to_html(index=False, escape=False)}
|
| 96 |
+
</div>
|
| 97 |
+
"""
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
grid_html = f"""
|
| 101 |
<div class="grid">
|
| 102 |
+
{''.join(col_html)}
|
| 103 |
</div>
|
| 104 |
"""
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
css = """
|
| 107 |
<style>
|
| 108 |
+
.grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
|
| 109 |
+
.col, .main-table-container {
|
| 110 |
+
max-height: 480px; overflow-y: auto;
|
| 111 |
+
border: 1px solid #ccc; padding: 4px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
+
table { font-size: 12px; width: 100%; border-collapse: collapse; }
|
| 114 |
+
th, td { border: 1px solid #ddd; padding: 4px; }
|
| 115 |
th {
|
| 116 |
+
background: #2E7D32; color: white;
|
| 117 |
+
position: sticky; top: 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
</style>
|
| 120 |
"""
|
| 121 |
|
|
|
|
|
|
|
|
|
|
| 122 |
return (
|
| 123 |
css +
|
| 124 |
"<h2>Main Bhavcopy Table</h2>" +
|