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