Spaces:
Running
Running
Delete bhavcopy_html2.py
Browse files- bhavcopy_html2.py +0 -118
bhavcopy_html2.py
DELETED
|
@@ -1,118 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import datetime
|
| 3 |
-
from nsepython import *
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
def build_bhavcopy_html(date_str):
|
| 7 |
-
# -------------------------------------------------------
|
| 8 |
-
# 1) Validate Date
|
| 9 |
-
# -------------------------------------------------------
|
| 10 |
-
try:
|
| 11 |
-
datetime.datetime.strptime(date_str, "%d-%m-%Y")
|
| 12 |
-
except:
|
| 13 |
-
return "<h3>Invalid date format. Use DD-MM-YYYY.</h3>"
|
| 14 |
-
|
| 15 |
-
# -------------------------------------------------------
|
| 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:
|
| 40 |
-
if col in df.columns:
|
| 41 |
-
df[col] = (
|
| 42 |
-
df[col]
|
| 43 |
-
.astype(str)
|
| 44 |
-
.str.replace(",", "", regex=False)
|
| 45 |
-
.str.strip()
|
| 46 |
-
)
|
| 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 |
-
# -------------------------------------------------------
|
| 67 |
-
# 8) HTML Output
|
| 68 |
-
# -------------------------------------------------------
|
| 69 |
-
main_html = f"""
|
| 70 |
-
<div class="main-table-container">
|
| 71 |
-
{df.to_html(index=False, escape=False)}
|
| 72 |
-
</div>
|
| 73 |
-
"""
|
| 74 |
-
|
| 75 |
-
metrics = ["perchange", "pergap", "TURNOVER_LACS", "NO_OF_TRADES", "DELIV_PER"]
|
| 76 |
-
col_html = []
|
| 77 |
-
|
| 78 |
-
for m in metrics:
|
| 79 |
-
if m in df.columns:
|
| 80 |
-
temp = df[["SYMBOL", m]].sort_values(m, ascending=False)
|
| 81 |
-
col_html.append(
|
| 82 |
-
f"""
|
| 83 |
-
<div class="col">
|
| 84 |
-
<h4>{m}</h4>
|
| 85 |
-
{temp.to_html(index=False, escape=False)}
|
| 86 |
-
</div>
|
| 87 |
-
"""
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
grid_html = f"""
|
| 91 |
-
<div class="grid">
|
| 92 |
-
{''.join(col_html)}
|
| 93 |
-
</div>
|
| 94 |
-
"""
|
| 95 |
-
|
| 96 |
-
css = """
|
| 97 |
-
<style>
|
| 98 |
-
.grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
|
| 99 |
-
.col, .main-table-container {
|
| 100 |
-
max-height: 480px; overflow-y: auto;
|
| 101 |
-
border: 1px solid #ccc; padding: 4px;
|
| 102 |
-
}
|
| 103 |
-
table { font-size: 12px; width: 100%; border-collapse: collapse; }
|
| 104 |
-
th, td { border: 1px solid #ddd; padding: 4px; }
|
| 105 |
-
th {
|
| 106 |
-
background: #2E7D32; color: white;
|
| 107 |
-
position: sticky; top: 0;
|
| 108 |
-
}
|
| 109 |
-
</style>
|
| 110 |
-
"""
|
| 111 |
-
|
| 112 |
-
return (
|
| 113 |
-
css +
|
| 114 |
-
"<h2>Main Bhavcopy Table</h2>" +
|
| 115 |
-
main_html +
|
| 116 |
-
"<h2>Matrix/Grid Table</h2>" +
|
| 117 |
-
grid_html
|
| 118 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|