eshan6704 commited on
Commit
0c2f0cc
·
verified ·
1 Parent(s): c87d091

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +265 -0
app.py ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from stock import *
3
+ from indices_html import *
4
+ from index_live_html import *
5
+ from preopen_html import *
6
+ from eq_html import *
7
+ import pandas as pd
8
+ from bhavcopy_html import *
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;
19
+ overflow-y: auto;
20
+ overflow-x: auto;
21
+ padding: 10px;
22
+ border: 1px solid #ccc;
23
+ border-radius: 6px;
24
+ ">
25
+ {{HTML}}
26
+ </div>
27
+ """
28
+
29
+ # ======================================================
30
+ # Date helpers
31
+ # ======================================================
32
+ def today_str():
33
+ return datetime.date.today().strftime("%d-%m-%Y")
34
+
35
+ def yesterday_str():
36
+ return (datetime.date.today() - datetime.timedelta(days=1)).strftime("%d-%m-%Y")
37
+
38
+ def last_year_date(d):
39
+ dt = datetime.datetime.strptime(d, "%d-%m-%Y")
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"
57
+ ]
58
+
59
+ INDEX_REQ = [
60
+ "indices", "nse_open", "nse_preopen", "nse_fno", "nse_fiidii",
61
+ "nse_events", "nse_future", "nse_bhav", "nse_highlow",
62
+ "index_history", "nse_largedeals", "nse_most_active",
63
+ "largedeals_historical", "nse_bulkdeals", "nse_blockdeals",
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 (
73
+ gr.update(choices=STOCK_REQ, value="info"),
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)