eshan6704 commited on
Commit
e241711
·
verified ·
1 Parent(s): fddb978

Update bhavcopy_html.py

Browse files
Files changed (1) hide show
  1. bhavcopy_html.py +113 -94
bhavcopy_html.py CHANGED
@@ -1,113 +1,132 @@
1
- def build_bhav_html(df):
2
- if df is None or df.empty:
3
- return "<h3>No Bhavcopy Found</h3>"
4
-
5
- # ------------------------------
6
- # 1) RENAME + FILTER REQUIRED COLS
7
- # ------------------------------
8
- df = df.rename(columns={
9
- "CH_PREV_CLOSE": "preclose",
10
- "CH_OPENING_PRICE": "open",
11
- "CH_CLOSING_PRICE": "close",
12
- "CH_TOT_TRADED_QTY": "volume",
13
- "CH_TOT_TRADED_VAL": "turnover",
14
- "CH_LAST_TRADED_PRICE": "ltp",
15
- "CH_TRADED_ORDERS": "order",
16
- "CH_DELIV_QTY": "delqty",
17
- "CH_DELIV_PER": "perdel"
18
- })
19
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  required = [
21
- "symbol","series","preclose","open","high","low",
22
- "close","volume","turnover","order","perdel"
23
  ]
24
- df = df[required].copy()
25
 
26
- # ------------------------------
27
- # 2) ADD NEW COLUMNS
28
- # ------------------------------
29
- df["change"] = df["close"] - df["preclose"]
30
- df["perchange"] = (df["change"] / df["preclose"] * 100).round(2)
31
- df["pergap"] = ((df["open"] - df["preclose"]) / df["preclose"] * 100).round(2)
32
 
33
- # Sorting for grid tables
34
- df_sorted = df.sort_values("perchange", ascending=False)
35
 
36
- # ------------------------------
37
- # 3) MAIN TABLE (top)
38
- # ------------------------------
39
- main_table_html = df.to_html(index=False, classes="main-table")
 
 
40
 
41
- # ------------------------------
42
- # 4) 5-COLUMN GRID (each column scrollable)
43
- # ------------------------------
 
44
 
45
- col_names = ["perchange", "turnover", "order", "volume", "perdel"]
46
- tables_html = ""
 
 
47
 
48
- for col in col_names:
49
- sub = df_sorted[["symbol", col]].copy()
50
- sub = sub.sort_values(col, ascending=False)
51
- tables_html += f"""
52
- <div class="grid-col">
53
- <h4>{col.upper()}</h4>
54
- <div class="grid-scroll">
55
- {sub.to_html(index=False, classes='sub-table')}
 
 
 
56
  </div>
57
- </div>
58
- """
59
 
60
- full_html = f"""
61
- <style>
62
- .main-table {{
63
- width: 100%;
64
- border-collapse: collapse;
65
- background: white;
66
- margin-bottom: 18px;
67
- }}
68
- .main-table th, .main-table td {{
69
- padding: 6px 8px;
70
- border: 1px solid #ddd;
71
- }}
72
 
73
- /* GRID */
74
- .grid-container {{
 
 
 
 
75
  display: grid;
76
  grid-template-columns: repeat(5, 1fr);
77
- gap: 12px;
78
- width: 100%;
79
- }}
80
-
81
- .grid-col {{
82
- background: white;
83
- border: 1px solid #ccc;
84
- padding: 6px;
85
- border-radius: 4px;
86
- }}
87
-
88
- .grid-scroll {{
89
- max-height: 420px;
90
  overflow-y: scroll;
91
- }}
92
-
93
- .sub-table {{
94
- width: 100%;
 
 
95
  border-collapse: collapse;
96
- }}
97
- .sub-table th, .sub-table td {{
 
98
  border: 1px solid #ddd;
99
- padding: 4px 6px;
100
- font-size: 13px;
101
- }}
 
 
 
 
102
  </style>
103
-
104
- <h3>Bhavcopy Table</h3>
105
- {main_table_html}
106
-
107
- <h3>Matrix Grid</h3>
108
- <div class="grid-container">
109
- {tables_html}
110
- </div>
111
  """
112
 
113
- return full_html
 
 
 
 
1
+ import pandas as pd
2
+ import datetime
3
+
4
+ def build_bhavcopy_html(date_str):
5
+ # -------------------------------------------------------
6
+ # 1) Validate Date
7
+ # -------------------------------------------------------
8
+ try:
9
+ datetime.datetime.strptime(date_str, "%d-%m-%Y")
10
+ except:
11
+ return "<h3>Invalid date format. Use DD-MM-YYYY.</h3>"
12
+
13
+ # -------------------------------------------------------
14
+ # 2) Fetch using YOUR nse_bhavcopy definition
15
+ # -------------------------------------------------------
16
+ try:
17
+ df = nse_bhavcopy(date_str) # <-- your custom loader
18
+ except:
19
+ return f"<h3>No Bhavcopy found for {date_str}.</h3>"
20
+
21
+ # -------------------------------------------------------
22
+ # 3) Rename columns
23
+ # -------------------------------------------------------
24
+ rename_map = {
25
+ "SYMBOL": "symbol",
26
+ "SERIES": "series",
27
+ "PREV_CLOSE": "preclose",
28
+ "OPEN_PRICE": "open",
29
+ "HIGH_PRICE": "high",
30
+ "LOW_PRICE": "low",
31
+ "CLOSE_PRICE": "close",
32
+ "TTL_TRD_QNTY": "volume",
33
+ "TURNOVER_LACS": "turnover",
34
+ "NO_OF_TRADES": "order",
35
+ "DELIV_QTY": "del",
36
+ "DELIV_PER": "perdel",
37
+ }
38
+
39
+ df = df.rename(columns=rename_map)
40
+
41
+ # -------------------------------------------------------
42
+ # 4) Select required columns
43
+ # -------------------------------------------------------
44
  required = [
45
+ "symbol","series","preclose","open","high",
46
+ "low","close","volume","turnover","order","perdel"
47
  ]
 
48
 
49
+ # ensure missing ones are created
50
+ for col in required:
51
+ if col not in df.columns:
52
+ df[col] = 0
 
 
53
 
54
+ df = df[required]
 
55
 
56
+ # -------------------------------------------------------
57
+ # 5) Add new computed columns
58
+ # -------------------------------------------------------
59
+ df["change"] = df["close"] - df["preclose"]
60
+ df["perchange"] = (df["change"] / df["preclose"].replace(0, 1)) * 100
61
+ df["pergap"] = ((df["open"] - df["preclose"]) / df["preclose"].replace(0, 1)) * 100
62
 
63
+ # -------------------------------------------------------
64
+ # 6) Build MAIN TABLE
65
+ # -------------------------------------------------------
66
+ main_html = df.to_html(index=False, escape=False)
67
 
68
+ # -------------------------------------------------------
69
+ # 7) Build GRID TABLES (5 columns)
70
+ # -------------------------------------------------------
71
+ df_sorted = df.sort_values("perchange", ascending=False)
72
 
73
+ n = len(df_sorted)
74
+ chunk_size = (n + 4) // 5
75
+ chunks = [df_sorted.iloc[i:i+chunk_size] for i in range(0, n, chunk_size)]
76
+
77
+ # each chunk to HTML
78
+ col_html = []
79
+ for ch in chunks:
80
+ col_html.append(
81
+ f"""
82
+ <div class="col">
83
+ {ch.to_html(index=False, escape=False)}
84
  </div>
85
+ """
86
+ )
87
 
88
+ grid_html = """
89
+ <div class="grid">
90
+ """ + "\n".join(col_html) + """
91
+ </div>
92
+ """
 
 
 
 
 
 
 
93
 
94
+ # -------------------------------------------------------
95
+ # 8) CSS for layout
96
+ # -------------------------------------------------------
97
+ css = """
98
+ <style>
99
+ .grid {
100
  display: grid;
101
  grid-template-columns: repeat(5, 1fr);
102
+ gap: 10px;
103
+ margin-top: 20px;
104
+ }
105
+ .col {
106
+ max-height: 480px;
 
 
 
 
 
 
 
 
107
  overflow-y: scroll;
108
+ border: 1px solid #ccc;
109
+ padding: 4px;
110
+ background: #fafafa;
111
+ }
112
+ table {
113
+ font-size: 12px;
114
  border-collapse: collapse;
115
+ }
116
+ th, td {
117
+ padding: 3px 6px;
118
  border: 1px solid #ddd;
119
+ }
120
+ th {
121
+ background: #eee;
122
+ position: sticky;
123
+ top: 0;
124
+ z-index: 1;
125
+ }
126
  </style>
 
 
 
 
 
 
 
 
127
  """
128
 
129
+ # -------------------------------------------------------
130
+ # 9) Final Combined Output
131
+ # -------------------------------------------------------
132
+ return css + "<h2>Main Bhavcopy Table</h2>" + main_html + "<h2>5-Column Grid View</h2>" + grid_html