eshan6704 commited on
Commit
eeba0bb
·
verified ·
1 Parent(s): c94ccb4

Delete bhavcopy_html.py

Browse files
Files changed (1) hide show
  1. bhavcopy_html.py +0 -147
bhavcopy_html.py DELETED
@@ -1,147 +0,0 @@
1
- import pandas as pd
2
- import nsepython as nse
3
- import persist
4
- from datetime import datetime as dt
5
-
6
-
7
- def build_bhavcopy_html(date_str):
8
- key = f"bhavcopy_{date_str}"
9
-
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"[{dt.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
- dt.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 expects 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",
55
- "CLOSE_PRICE", "TTL_TRD_QNTY", "TURNOVER_LACS",
56
- "NO_OF_TRADES", "DELIV_QTY", "DELIV_PER"
57
- ]
58
-
59
- for col in numeric_cols:
60
- if col in df.columns:
61
- df[col] = (
62
- df[col]
63
- .astype(str)
64
- .str.replace(",", "", regex=False)
65
- .str.strip()
66
- )
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
80
- df["pergap"] = (
81
- (df["OPEN_PRICE"] - df["PREV_CLOSE"]) /
82
- df["PREV_CLOSE"].replace(0, 1)
83
- ) * 100
84
-
85
- # -------------------------------------------------------
86
- # 7) HTML Output
87
- # -------------------------------------------------------
88
- main_html = f"""
89
- <div class="main-table-container">
90
- {df.to_html(index=False, escape=False)}
91
- </div>
92
- """
93
-
94
- metrics = ["perchange", "pergap", "TURNOVER_LACS", "NO_OF_TRADES", "DELIV_PER"]
95
- col_html = []
96
-
97
- for m in metrics:
98
- if m in df.columns:
99
- temp = df[["SYMBOL", m]].sort_values(m, ascending=False)
100
- col_html.append(
101
- f"""
102
- <div class="col">
103
- <h4>{m}</h4>
104
- {temp.to_html(index=False, escape=False)}
105
- </div>
106
- """
107
- )
108
-
109
- grid_html = f"""
110
- <div class="grid">
111
- {''.join(col_html)}
112
- </div>
113
- """
114
-
115
- css = """
116
- <style>
117
- .grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
118
- .col, .main-table-container {
119
- max-height: 480px; overflow-y: auto;
120
- border: 1px solid #ccc; padding: 4px;
121
- }
122
- table { font-size: 12px; width: 100%; border-collapse: collapse; }
123
- th, td { border: 1px solid #ddd; padding: 4px; }
124
- th {
125
- background: #2E7D32; color: white;
126
- position: sticky; top: 0;
127
- }
128
- </style>
129
- """
130
-
131
- html = (
132
- css +
133
- "<h2>Main Bhavcopy Table</h2>" +
134
- main_html +
135
- "<h2>Matrix/Grid Table</h2>" +
136
- grid_html
137
- )
138
-
139
- persist.save(key, html, "html")
140
- return html
141
-
142
- except Exception as e:
143
- print(
144
- f"[{dt.now().strftime('%Y-%m-%d %H:%M:%S')}] "
145
- f"Error build_bhavcopy_html: {e}"
146
- )
147
- return f"<h3>Error: {e}</h3>"