eshan6704 commited on
Commit
18e680e
·
verified ·
1 Parent(s): 2f3a11f

Create bhabcopy_html.py

Browse files
Files changed (1) hide show
  1. bhabcopy_html.py +146 -0
bhabcopy_html.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import datetime
3
+ from nsepython import *
4
+
5
+
6
+ def build_bhavcopy_html(date_str):
7
+ key = f"bhavcopy_{date_str}"
8
+
9
+ # -------------------------------------------------------
10
+ # 0) Use cached HTML if present
11
+ # -------------------------------------------------------
12
+ if exists(key, "html"):
13
+ cached = load(key, "html")
14
+ if cached is not False:
15
+ print(
16
+ f"[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] "
17
+ f"Using cached bhavcopy for {date_str}"
18
+ )
19
+ return cached
20
+
21
+ try:
22
+ # -------------------------------------------------------
23
+ # 1) Validate Date
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
+ # 3) Drop unwanted columns
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
+ # 4) Convert numeric columns
51
+ # -------------------------------------------------------
52
+ numeric_cols = [
53
+ "PREV_CLOSE", "OPEN_PRICE", "HIGH_PRICE", "LOW_PRICE",
54
+ "CLOSE_PRICE", "TTL_TRD_QNTY", "TURNOVER_LACS",
55
+ "NO_OF_TRADES", "DELIV_QTY", "DELIV_PER"
56
+ ]
57
+
58
+ for col in numeric_cols:
59
+ if col in df.columns:
60
+ df[col] = (
61
+ df[col]
62
+ .astype(str)
63
+ .str.replace(",", "", regex=False)
64
+ .str.strip()
65
+ )
66
+ df[col] = pd.to_numeric(df[col], errors="coerce").fillna(0)
67
+
68
+ # -------------------------------------------------------
69
+ # 5) Filter & sort
70
+ # -------------------------------------------------------
71
+ df = df[df["TURNOVER_LACS"] > 1000]
72
+ df = df.sort_values("TURNOVER_LACS", ascending=False)
73
+
74
+ # -------------------------------------------------------
75
+ # 6) Computed columns
76
+ # -------------------------------------------------------
77
+ df["change"] = df["CLOSE_PRICE"] - df["PREV_CLOSE"]
78
+ df["perchange"] = (df["change"] / df["PREV_CLOSE"].replace(0, 1)) * 100
79
+ df["pergap"] = (
80
+ (df["OPEN_PRICE"] - df["PREV_CLOSE"]) /
81
+ df["PREV_CLOSE"].replace(0, 1)
82
+ ) * 100
83
+
84
+ # -------------------------------------------------------
85
+ # 7) HTML Output
86
+ # -------------------------------------------------------
87
+ main_html = f"""
88
+ <div class="main-table-container">
89
+ {df.to_html(index=False, escape=False)}
90
+ </div>
91
+ """
92
+
93
+ metrics = ["perchange", "pergap", "TURNOVER_LACS", "NO_OF_TRADES", "DELIV_PER"]
94
+ col_html = []
95
+
96
+ for m in metrics:
97
+ if m in df.columns:
98
+ temp = df[["SYMBOL", m]].sort_values(m, ascending=False)
99
+ col_html.append(
100
+ f"""
101
+ <div class="col">
102
+ <h4>{m}</h4>
103
+ {temp.to_html(index=False, escape=False)}
104
+ </div>
105
+ """
106
+ )
107
+
108
+ grid_html = f"""
109
+ <div class="grid">
110
+ {''.join(col_html)}
111
+ </div>
112
+ """
113
+
114
+ css = """
115
+ <style>
116
+ .grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
117
+ .col, .main-table-container {
118
+ max-height: 480px; overflow-y: auto;
119
+ border: 1px solid #ccc; padding: 4px;
120
+ }
121
+ table { font-size: 12px; width: 100%; border-collapse: collapse; }
122
+ th, td { border: 1px solid #ddd; padding: 4px; }
123
+ th {
124
+ background: #2E7D32; color: white;
125
+ position: sticky; top: 0;
126
+ }
127
+ </style>
128
+ """
129
+
130
+ html = (
131
+ css +
132
+ "<h2>Main Bhavcopy Table</h2>" +
133
+ main_html +
134
+ "<h2>Matrix/Grid Table</h2>" +
135
+ grid_html
136
+ )
137
+
138
+ save(key, html, "html")
139
+ return html
140
+
141
+ except Exception as e:
142
+ print(
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>"