eshan6704 commited on
Commit
18f1b43
·
verified ·
1 Parent(s): 2f9d10e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -71
app.py CHANGED
@@ -9,12 +9,10 @@ from bhavcopy_html import *
9
  from nsepython import *
10
  from yahooinfo import fetch_info
11
  import datetime
12
- from datetime import datetime
13
- from backblaze import upload_file, read_file # for optional file writing
14
 
15
- # ===========================
16
  # Scrollable HTML wrapper
17
- # ===========================
18
  SCROLL_WRAP = """
19
  <div style="
20
  max-height: 80vh;
@@ -28,9 +26,9 @@ SCROLL_WRAP = """
28
  </div>
29
  """
30
 
31
- # ===========================
32
  # Date helpers
33
- # ===========================
34
  def today_str():
35
  return datetime.date.today().strftime("%d-%m-%Y")
36
 
@@ -42,17 +40,17 @@ def last_year_date(d):
42
  new_dt = dt.replace(year=dt.year - 1)
43
  return new_dt.strftime("%d-%m-%Y")
44
 
45
- # ===========================
46
  # HTML wrapper
47
- # ===========================
48
  def wrap(html):
49
  if html is None:
50
  return "<h3>No Data</h3>"
51
  return SCROLL_WRAP.replace("{{HTML}}", html)
52
 
53
- # ===========================
54
  # Request Type Options
55
- # ===========================
56
  STOCK_REQ = [
57
  "info", "intraday", "daily", "nse_eq", "qresult", "result",
58
  "balance", "cashflow", "dividend", "split", "other", "stock_hist"
@@ -66,9 +64,9 @@ INDEX_REQ = [
66
  "index_pe_pb_div", "index_total_returns"
67
  ]
68
 
69
- # ===========================
70
- # Update UI defaults
71
- # ===========================
72
  def update_on_mode(mode):
73
  if mode == "stock":
74
  return (
@@ -76,130 +74,192 @@ def update_on_mode(mode):
76
  gr.update(value="ITC"),
77
  gr.update(value=yesterday_str())
78
  )
 
79
  elif mode == "index":
80
  return (
81
  gr.update(choices=INDEX_REQ, value="indices"),
82
  gr.update(value="NIFTY 50"),
83
  gr.update(value=yesterday_str())
84
  )
85
- else:
86
- return (
87
- gr.update(choices=[], value=""),
88
- gr.update(value=""),
89
- gr.update(value="")
90
- )
91
 
92
- # ===========================
93
- # Fetch data
94
- # ===========================
 
 
 
 
 
 
95
  def fetch_data(mode, req_type, name, date_str):
96
  req_type = req_type.lower()
97
  name = name.strip()
98
  date_str = date_str.strip()
 
 
99
  if not date_str:
100
  date_str = yesterday_str()
 
101
  date_start = last_year_date(date_str)
102
 
103
- # ======= Index Mode =======
104
  if mode == "index":
 
105
  if req_type == "indices":
106
- result = build_indices_html()
 
107
  elif req_type == "nse_open":
108
- result = build_index_live_html()
 
109
  elif req_type == "nse_preopen":
110
- result = build_preopen_html()
 
111
  elif req_type == "nse_fno":
112
- result = wrap(nse_fno(name))
 
113
  elif req_type == "nse_events":
114
- result = nse_events().to_html()
 
115
  elif req_type == "nse_fiidii":
116
- result = nse_fiidii().to_html()
 
117
  elif req_type == "nse_future":
118
- result = wrap(nse_future(name))
 
119
  elif req_type == "nse_highlow":
120
- result = wrap(nse_highlow())
 
121
  elif req_type == "nse_bhav":
122
- result = build_bhavcopy_html(date_str)
 
123
  elif req_type == "nse_largedeals":
124
- result = nse_largedeals().to_html()
 
125
  elif req_type == "nse_bulkdeals":
126
- result = nse_bulkdeals().to_html()
 
127
  elif req_type == "nse_blockdeals":
128
- result = nse_blockdeals().to_html()
 
129
  elif req_type == "nse_most_active":
130
- result = nse_most_active().to_html()
 
131
  elif req_type == "index_history":
132
- result = index_history("NIFTY 50", date_start, date_str).to_html()
 
133
  elif req_type == "largedeals_historical":
134
- result = nse_largedeals_historical(date_start, date_str).to_html()
 
135
  elif req_type == "index_pe_pb_div":
136
- result = index_pe_pb_div("NIFTY 50", date_start, date_str).to_html()
 
137
  elif req_type == "index_total_returns":
138
- result = index_total_returns("NIFTY 50", date_start, date_str).to_html()
 
139
  else:
140
- result = wrap(f"<h3>No handler for {req_type}</h3>")
141
 
142
- # ======= Stock Mode =======
143
  elif mode == "stock":
 
144
  if req_type == "daily":
145
- result = wrap(fetch_daily(name))
 
146
  elif req_type == "nse_eq":
147
- result = build_eq_html(name)
 
148
  elif req_type == "intraday":
149
- result = wrap(fetch_intraday(name))
 
150
  elif req_type == "info":
151
- result = wrap(fetch_info(name))
 
152
  elif req_type == "qresult":
153
- result = wrap(fetch_qresult(name))
 
154
  elif req_type == "result":
155
- result = wrap(fetch_result(name))
 
156
  elif req_type == "balance":
157
- result = wrap(fetch_balance(name))
 
158
  elif req_type == "cashflow":
159
- result = wrap(fetch_cashflow(name))
 
160
  elif req_type == "dividend":
161
- result = wrap(fetch_dividend(name))
 
162
  elif req_type == "split":
163
- result = wrap(fetch_split(name))
 
164
  elif req_type == "other":
165
- result = wrap(fetch_other(name))
 
166
  elif req_type == "stock_hist":
167
- result = nse_stock_hist(date_start, date_str, name).to_html()
168
- else:
169
- result = wrap(f"<h3>No handler for {req_type}</h3>")
170
 
171
- else:
172
- result = wrap(f"<h3>No valid mode: {mode}</h3>")
173
 
174
- return result
175
 
176
- # ===========================
177
  # UI
178
- # ===========================
179
  with gr.Blocks(title="Stock / Index App") as iface:
180
 
181
  gr.Markdown("### **Stock / Index Data Fetcher**")
182
 
183
  with gr.Row():
184
- mode_input = gr.Radio(["stock", "index"], label="Mode", value="stock", scale=1)
185
- name_input = gr.Textbox(label="Symbol / Index Name", scale=2)
186
- req_type_input = gr.Dropdown(label="Request Type", allow_custom_value=True, scale=2)
187
- date_input = gr.Textbox(label="Date (DD-MM-YYYY)", placeholder="Leave empty = yesterday", scale=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  fetch_btn = gr.Button("Fetch", scale=1)
189
 
190
  output = gr.HTML(label="Output")
191
 
192
  # Mode change → auto defaults
193
- mode_input.change(update_on_mode, inputs=mode_input, outputs=[req_type_input, name_input, date_input])
 
 
 
 
194
 
195
  # Initial load defaults
196
- iface.load(update_on_mode, inputs=mode_input, outputs=[req_type_input, name_input, date_input])
 
 
 
 
197
 
198
  # Fetch
199
- fetch_btn.click(fetch_data, inputs=[mode_input, req_type_input, name_input, date_input], outputs=output)
 
 
 
 
200
 
201
- # ===========================
202
  # Launch
203
- # ===========================
204
  if __name__ == "__main__":
205
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
9
  from nsepython import *
10
  from yahooinfo import fetch_info
11
  import datetime
 
 
12
 
13
+ # ======================================================
14
  # Scrollable HTML wrapper
15
+ # ======================================================
16
  SCROLL_WRAP = """
17
  <div style="
18
  max-height: 80vh;
 
26
  </div>
27
  """
28
 
29
+ # ======================================================
30
  # Date helpers
31
+ # ======================================================
32
  def today_str():
33
  return datetime.date.today().strftime("%d-%m-%Y")
34
 
 
40
  new_dt = dt.replace(year=dt.year - 1)
41
  return new_dt.strftime("%d-%m-%Y")
42
 
43
+ # ======================================================
44
  # HTML wrapper
45
+ # ======================================================
46
  def wrap(html):
47
  if html is None:
48
  return "<h3>No Data</h3>"
49
  return SCROLL_WRAP.replace("{{HTML}}", html)
50
 
51
+ # ======================================================
52
  # Request Type Options
53
+ # ======================================================
54
  STOCK_REQ = [
55
  "info", "intraday", "daily", "nse_eq", "qresult", "result",
56
  "balance", "cashflow", "dividend", "split", "other", "stock_hist"
 
64
  "index_pe_pb_div", "index_total_returns"
65
  ]
66
 
67
+ # ======================================================
68
+ # Update UI based on mode
69
+ # ======================================================
70
  def update_on_mode(mode):
71
  if mode == "stock":
72
  return (
 
74
  gr.update(value="ITC"),
75
  gr.update(value=yesterday_str())
76
  )
77
+
78
  elif mode == "index":
79
  return (
80
  gr.update(choices=INDEX_REQ, value="indices"),
81
  gr.update(value="NIFTY 50"),
82
  gr.update(value=yesterday_str())
83
  )
 
 
 
 
 
 
84
 
85
+ return (
86
+ gr.update(choices=[], value=""),
87
+ gr.update(value=""),
88
+ gr.update(value="")
89
+ )
90
+
91
+ # ======================================================
92
+ # Data Fetcher (API logic untouched)
93
+ # ======================================================
94
  def fetch_data(mode, req_type, name, date_str):
95
  req_type = req_type.lower()
96
  name = name.strip()
97
  date_str = date_str.strip()
98
+
99
+ # ✅ Frontend may send empty date → auto yesterday
100
  if not date_str:
101
  date_str = yesterday_str()
102
+
103
  date_start = last_year_date(date_str)
104
 
 
105
  if mode == "index":
106
+
107
  if req_type == "indices":
108
+ return build_indices_html()
109
+
110
  elif req_type == "nse_open":
111
+ return build_index_live_html()
112
+
113
  elif req_type == "nse_preopen":
114
+ return build_preopen_html()
115
+
116
  elif req_type == "nse_fno":
117
+ return wrap(nse_fno(name))
118
+
119
  elif req_type == "nse_events":
120
+ return nse_events().to_html()
121
+
122
  elif req_type == "nse_fiidii":
123
+ return nse_fiidii().to_html()
124
+
125
  elif req_type == "nse_future":
126
+ return wrap(nse_future(name))
127
+
128
  elif req_type == "nse_highlow":
129
+ return wrap(nse_highlow())
130
+
131
  elif req_type == "nse_bhav":
132
+ return build_bhavcopy_html(date_str)
133
+
134
  elif req_type == "nse_largedeals":
135
+ return nse_largedeals().to_html()
136
+
137
  elif req_type == "nse_bulkdeals":
138
+ return nse_bulkdeals().to_html()
139
+
140
  elif req_type == "nse_blockdeals":
141
+ return nse_blockdeals().to_html()
142
+
143
  elif req_type == "nse_most_active":
144
+ return nse_most_active().to_html()
145
+
146
  elif req_type == "index_history":
147
+ return index_history("NIFTY 50", date_start, date_str).to_html()
148
+
149
  elif req_type == "largedeals_historical":
150
+ return nse_largedeals_historical(date_start, date_str).to_html()
151
+
152
  elif req_type == "index_pe_pb_div":
153
+ return index_pe_pb_div("NIFTY 50", date_start, date_str).to_html()
154
+
155
  elif req_type == "index_total_returns":
156
+ return index_total_returns("NIFTY 50", date_start, date_str).to_html()
157
+
158
  else:
159
+ return wrap(f"<h3>No handler for {req_type}</h3>")
160
 
 
161
  elif mode == "stock":
162
+
163
  if req_type == "daily":
164
+ return wrap(fetch_daily(name))
165
+
166
  elif req_type == "nse_eq":
167
+ return build_eq_html(name)
168
+
169
  elif req_type == "intraday":
170
+ return wrap(fetch_intraday(name))
171
+
172
  elif req_type == "info":
173
+ return wrap(fetch_info(name))
174
+
175
  elif req_type == "qresult":
176
+ return wrap(fetch_qresult(name))
177
+
178
  elif req_type == "result":
179
+ return wrap(fetch_result(name))
180
+
181
  elif req_type == "balance":
182
+ return wrap(fetch_balance(name))
183
+
184
  elif req_type == "cashflow":
185
+ return wrap(fetch_cashflow(name))
186
+
187
  elif req_type == "dividend":
188
+ return wrap(fetch_dividend(name))
189
+
190
  elif req_type == "split":
191
+ return wrap(fetch_split(name))
192
+
193
  elif req_type == "other":
194
+ return wrap(fetch_other(name))
195
+
196
  elif req_type == "stock_hist":
197
+ return nse_stock_hist(date_start, date_str, name).to_html()
 
 
198
 
199
+ else:
200
+ return wrap(f"<h3>No handler for {req_type}</h3>")
201
 
202
+ return wrap(f"<h3>No valid mode: {mode}</h3>")
203
 
204
+ # ======================================================
205
  # UI
206
+ # ======================================================
207
  with gr.Blocks(title="Stock / Index App") as iface:
208
 
209
  gr.Markdown("### **Stock / Index Data Fetcher**")
210
 
211
  with gr.Row():
212
+ mode_input = gr.Radio(
213
+ ["stock", "index"],
214
+ label="Mode",
215
+ value="stock",
216
+ scale=1
217
+ )
218
+
219
+ name_input = gr.Textbox(
220
+ label="Symbol / Index Name",
221
+ scale=2
222
+ )
223
+
224
+ req_type_input = gr.Dropdown(
225
+ label="Request Type",
226
+ allow_custom_value=True,
227
+ scale=2
228
+ )
229
+
230
+ date_input = gr.Textbox(
231
+ label="Date (DD-MM-YYYY)",
232
+ placeholder="Leave empty = yesterday",
233
+ scale=1
234
+ )
235
+
236
  fetch_btn = gr.Button("Fetch", scale=1)
237
 
238
  output = gr.HTML(label="Output")
239
 
240
  # Mode change → auto defaults
241
+ mode_input.change(
242
+ update_on_mode,
243
+ inputs=mode_input,
244
+ outputs=[req_type_input, name_input, date_input]
245
+ )
246
 
247
  # Initial load defaults
248
+ iface.load(
249
+ update_on_mode,
250
+ inputs=mode_input,
251
+ outputs=[req_type_input, name_input, date_input]
252
+ )
253
 
254
  # Fetch
255
+ fetch_btn.click(
256
+ fetch_data,
257
+ inputs=[mode_input, req_type_input, name_input, date_input],
258
+ outputs=output
259
+ )
260
 
261
+ # ======================================================
262
  # Launch
263
+ # ======================================================
264
  if __name__ == "__main__":
265
+ iface.launch(server_name="0.0.0.0", server_port=7860)