Spaces:
Running
Running
Update bhavcopy_html.py
Browse files- bhavcopy_html.py +12 -18
bhavcopy_html.py
CHANGED
|
@@ -3,6 +3,7 @@ import datetime
|
|
| 3 |
from nsepython import *
|
| 4 |
from persist import *
|
| 5 |
|
|
|
|
| 6 |
def build_bhavcopy_html(date_str):
|
| 7 |
key = f"bhavcopy_{date_str}"
|
| 8 |
|
|
@@ -20,34 +21,24 @@ def build_bhavcopy_html(date_str):
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
# -------------------------------------------------------
|
| 23 |
-
# 1)
|
| 24 |
-
# -------------------------------------------------------
|
| 25 |
-
try:
|
| 26 |
-
datetime.datetime.strptime(date_str, "%d-%m-%Y")
|
| 27 |
-
except:
|
| 28 |
-
html = "<h3>Invalid date format. Use DD-MM-YYYY.</h3>"
|
| 29 |
-
save(key, html, "html")
|
| 30 |
-
return html
|
| 31 |
-
|
| 32 |
-
# -------------------------------------------------------
|
| 33 |
-
# 2) Fetch Bhavcopy
|
| 34 |
# -------------------------------------------------------
|
| 35 |
try:
|
| 36 |
df = nse_bhavcopy(date_str)
|
| 37 |
df.columns = df.columns.str.strip()
|
| 38 |
-
except:
|
| 39 |
html = f"<h3>No Bhavcopy found for {date_str}.</h3>"
|
| 40 |
save(key, html, "html")
|
| 41 |
return html
|
| 42 |
|
| 43 |
# -------------------------------------------------------
|
| 44 |
-
#
|
| 45 |
# -------------------------------------------------------
|
| 46 |
remove = ["DATE1", "LAST_PRICE", "AVG_PRICE"]
|
| 47 |
df.drop(columns=[c for c in remove if c in df.columns], inplace=True)
|
| 48 |
|
| 49 |
# -------------------------------------------------------
|
| 50 |
-
#
|
| 51 |
# -------------------------------------------------------
|
| 52 |
numeric_cols = [
|
| 53 |
"PREV_CLOSE", "OPEN_PRICE", "HIGH_PRICE", "LOW_PRICE",
|
|
@@ -66,13 +57,13 @@ def build_bhavcopy_html(date_str):
|
|
| 66 |
df[col] = pd.to_numeric(df[col], errors="coerce").fillna(0)
|
| 67 |
|
| 68 |
# -------------------------------------------------------
|
| 69 |
-
#
|
| 70 |
# -------------------------------------------------------
|
| 71 |
df = df[df["TURNOVER_LACS"] > 1000]
|
| 72 |
df = df.sort_values("TURNOVER_LACS", ascending=False)
|
| 73 |
|
| 74 |
# -------------------------------------------------------
|
| 75 |
-
#
|
| 76 |
# -------------------------------------------------------
|
| 77 |
df["change"] = df["CLOSE_PRICE"] - df["PREV_CLOSE"]
|
| 78 |
df["perchange"] = (df["change"] / df["PREV_CLOSE"].replace(0, 1)) * 100
|
|
@@ -82,7 +73,7 @@ def build_bhavcopy_html(date_str):
|
|
| 82 |
) * 100
|
| 83 |
|
| 84 |
# -------------------------------------------------------
|
| 85 |
-
#
|
| 86 |
# -------------------------------------------------------
|
| 87 |
main_html = f"""
|
| 88 |
<div class="main-table-container">
|
|
@@ -135,6 +126,9 @@ def build_bhavcopy_html(date_str):
|
|
| 135 |
grid_html
|
| 136 |
)
|
| 137 |
|
|
|
|
|
|
|
|
|
|
| 138 |
save(key, html, "html")
|
| 139 |
return html
|
| 140 |
|
|
@@ -143,4 +137,4 @@ def build_bhavcopy_html(date_str):
|
|
| 143 |
f"[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] "
|
| 144 |
f"Error build_bhavcopy_html: {e}"
|
| 145 |
)
|
| 146 |
-
return f"<h3>Error: {e}</h3>"
|
|
|
|
| 3 |
from nsepython import *
|
| 4 |
from persist import *
|
| 5 |
|
| 6 |
+
|
| 7 |
def build_bhavcopy_html(date_str):
|
| 8 |
key = f"bhavcopy_{date_str}"
|
| 9 |
|
|
|
|
| 21 |
|
| 22 |
try:
|
| 23 |
# -------------------------------------------------------
|
| 24 |
+
# 1) Fetch Bhavcopy (DD-MM-YYYY passed as-is)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# -------------------------------------------------------
|
| 26 |
try:
|
| 27 |
df = nse_bhavcopy(date_str)
|
| 28 |
df.columns = df.columns.str.strip()
|
| 29 |
+
except Exception:
|
| 30 |
html = f"<h3>No Bhavcopy found for {date_str}.</h3>"
|
| 31 |
save(key, html, "html")
|
| 32 |
return html
|
| 33 |
|
| 34 |
# -------------------------------------------------------
|
| 35 |
+
# 2) Drop unwanted columns
|
| 36 |
# -------------------------------------------------------
|
| 37 |
remove = ["DATE1", "LAST_PRICE", "AVG_PRICE"]
|
| 38 |
df.drop(columns=[c for c in remove if c in df.columns], inplace=True)
|
| 39 |
|
| 40 |
# -------------------------------------------------------
|
| 41 |
+
# 3) Convert numeric columns
|
| 42 |
# -------------------------------------------------------
|
| 43 |
numeric_cols = [
|
| 44 |
"PREV_CLOSE", "OPEN_PRICE", "HIGH_PRICE", "LOW_PRICE",
|
|
|
|
| 57 |
df[col] = pd.to_numeric(df[col], errors="coerce").fillna(0)
|
| 58 |
|
| 59 |
# -------------------------------------------------------
|
| 60 |
+
# 4) Filter & sort
|
| 61 |
# -------------------------------------------------------
|
| 62 |
df = df[df["TURNOVER_LACS"] > 1000]
|
| 63 |
df = df.sort_values("TURNOVER_LACS", ascending=False)
|
| 64 |
|
| 65 |
# -------------------------------------------------------
|
| 66 |
+
# 5) Computed columns
|
| 67 |
# -------------------------------------------------------
|
| 68 |
df["change"] = df["CLOSE_PRICE"] - df["PREV_CLOSE"]
|
| 69 |
df["perchange"] = (df["change"] / df["PREV_CLOSE"].replace(0, 1)) * 100
|
|
|
|
| 73 |
) * 100
|
| 74 |
|
| 75 |
# -------------------------------------------------------
|
| 76 |
+
# 6) HTML Output
|
| 77 |
# -------------------------------------------------------
|
| 78 |
main_html = f"""
|
| 79 |
<div class="main-table-container">
|
|
|
|
| 126 |
grid_html
|
| 127 |
)
|
| 128 |
|
| 129 |
+
# -------------------------------------------------------
|
| 130 |
+
# 7) Save ONLY newly generated HTML
|
| 131 |
+
# -------------------------------------------------------
|
| 132 |
save(key, html, "html")
|
| 133 |
return html
|
| 134 |
|
|
|
|
| 137 |
f"[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] "
|
| 138 |
f"Error build_bhavcopy_html: {e}"
|
| 139 |
)
|
| 140 |
+
return f"<h3>Error: {e}</h3>"
|