eshan6704 commited on
Commit
5d0d0b2
·
verified ·
1 Parent(s): df5a68c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -0
app.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from stock import *
3
+ from nsepython import *
4
+ import pandas as pd
5
+
6
+ # ======================================================
7
+ # Scrollable HTML wrapper
8
+ # ======================================================
9
+ SCROLL_WRAP = """
10
+ <div style="
11
+ max-height: 80vh;
12
+ overflow-y: auto;
13
+ overflow-x: auto;
14
+ padding: 10px;
15
+ border: 1px solid #ccc;
16
+ border-radius: 6px;
17
+ ">
18
+ {{HTML}}
19
+ </div>
20
+ """
21
+
22
+ def wrap(html):
23
+ if html is None:
24
+ return "<h3>No Data</h3>"
25
+ return SCROLL_WRAP.replace("{{HTML}}", html)
26
+
27
+
28
+ # ======================================================
29
+ # Request Type Options
30
+ # ======================================================
31
+ STOCK_REQ = [
32
+ "info", "intraday", "daily", "qresult", "result", "balance",
33
+ "cashflow", "dividend", "split", "other"
34
+ ]
35
+
36
+ INDEX_REQ = [
37
+ "indices", "nse_open", "nse_preopen", "nse_fno",
38
+ "nse_future", "nse_bhav", "nse_highlow"
39
+ ]
40
+
41
+
42
+ # ======================================================
43
+ # Update Dropdown + Symbol based on mode
44
+ # ======================================================
45
+ def update_on_mode(mode):
46
+ if mode == "stock":
47
+ return (
48
+ gr.update(choices=STOCK_REQ, value="info"),
49
+ gr.update(value="ITC", placeholder="Enter stock symbol")
50
+ )
51
+ elif mode == "index":
52
+ return (
53
+ gr.update(choices=INDEX_REQ, value="indices"),
54
+ gr.update(value="NIFTY 50", placeholder="Enter index name or leave blank for today bhavcopy")
55
+ )
56
+ return gr.update(visible=False), gr.update(value="")
57
+
58
+
59
+ # ======================================================
60
+ # Data Fetcher
61
+ # ======================================================
62
+ def fetch_data(mode, req_type, symbol, date_str):
63
+ req_type = req_type.lower()
64
+ symbol = symbol.strip()
65
+ date_str = date_str.strip()
66
+
67
+ if mode == "index":
68
+ if req_type == "indices":
69
+ return build_indices_html()
70
+ elif req_type == "nse_open":
71
+ return wrap(nse_open(symbol))
72
+ elif req_type == "nse_preopen":
73
+ return wrap(nse_preopen(symbol))
74
+ elif req_type == "nse_fno":
75
+ return wrap(nse_fno(symbol))
76
+ elif req_type == "nse_future":
77
+ return wrap(nse_future(symbol))
78
+ elif req_type == "nse_bhav":
79
+ # Use date if provided, else today
80
+ date_input = date_str or pd.Timestamp.today().strftime("%d-%m-%Y")
81
+ return wrap(nse_bhav(date_input))
82
+ elif req_type == "nse_highlow":
83
+ return wrap(nse_highlow())
84
+ else:
85
+ return wrap(f"<h3>No handler for {req_type}</h3>")
86
+
87
+ elif mode == "stock":
88
+ if req_type == "daily":
89
+ return wrap(fetch_daily(symbol))
90
+ elif req_type == "intraday":
91
+ return wrap(fetch_intraday(symbol))
92
+ elif req_type == "info":
93
+ return wrap(fetch_info(symbol))
94
+ elif req_type == "qresult":
95
+ return wrap(fetch_qresult(symbol))
96
+ elif req_type == "result":
97
+ return wrap(fetch_result(symbol))
98
+ elif req_type == "balance":
99
+ return wrap(fetch_balance(symbol))
100
+ elif req_type == "cashflow":
101
+ return wrap(fetch_cashflow(symbol))
102
+ elif req_type == "dividend":
103
+ return wrap(fetch_dividend(symbol))
104
+ elif req_type == "split":
105
+ return wrap(fetch_split(symbol))
106
+ elif req_type == "other":
107
+ return wrap(fetch_other(symbol))
108
+ else:
109
+ return wrap(f"<h3>No handler for {req_type}</h3>")
110
+
111
+ return wrap(f"<h3>No valid mode: {mode}</h3>")
112
+
113
+
114
+ # ======================================================
115
+ # UI
116
+ # ======================================================
117
+ with gr.Blocks(title="Stock / Index App") as iface:
118
+
119
+ gr.Markdown("### **Stock / Index Data Fetcher**")
120
+
121
+ with gr.Row():
122
+
123
+ mode_input = gr.Radio(
124
+ ["stock", "index"],
125
+ label="Mode",
126
+ value="stock",
127
+ scale=1
128
+ )
129
+
130
+ symbol = gr.Textbox(
131
+ label="Symbol / Index Name",
132
+ value="ITC",
133
+ placeholder="Enter stock symbol",
134
+ scale=2
135
+ )
136
+
137
+ req_type = gr.Dropdown(
138
+ label="Request Type",
139
+ choices=[], # will be updated dynamically
140
+ value="", # will be updated dynamically
141
+ scale=2
142
+ )
143
+
144
+ date_field = gr.Textbox(
145
+ label="Date",
146
+ value="",
147
+ placeholder="DD-MM-YYYY",
148
+ scale=1
149
+ )
150
+
151
+ btn = gr.Button("Fetch", scale=1)
152
+
153
+ output = gr.HTML(label="Output")
154
+
155
+ # Update dropdown + symbol when mode changes
156
+ mode_input.change(
157
+ update_on_mode,
158
+ inputs=mode_input,
159
+ outputs=[req_type, symbol]
160
+ )
161
+
162
+ # Initialize dropdown for default mode
163
+ req_type.update(choices=STOCK_REQ, value="info")
164
+ symbol.update(value="ITC")
165
+
166
+ # Fetch button click
167
+ btn.click(fetch_data, inputs=[mode_input, req_type, symbol, date_field], outputs=output)
168
+
169
+
170
+ # ======================================================
171
+ # Launch
172
+ # ======================================================
173
+ if __name__ == "__main__":
174
+ iface.launch(server_name="0.0.0.0", server_port=7860)