eshan6704 commited on
Commit
af0744d
·
verified ·
1 Parent(s): e10f034

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -42
app.py CHANGED
@@ -40,45 +40,37 @@ INDEX_REQ = [
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:
@@ -86,25 +78,25 @@ def fetch_data(mode, req_type, symbol, date_str):
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
 
@@ -119,7 +111,6 @@ with gr.Blocks(title="Stock / Index App") as iface:
119
  gr.Markdown("### **Stock / Index Data Fetcher**")
120
 
121
  with gr.Row():
122
-
123
  mode_input = gr.Radio(
124
  ["stock", "index"],
125
  label="Mode",
@@ -127,40 +118,45 @@ with gr.Blocks(title="Stock / Index App") as iface:
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=STOCK_REQ, # initial choices for default stock mode
140
- value="info", # initial value
 
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
- # Fetch button click
163
- btn.click(fetch_data, inputs=[mode_input, req_type, symbol, date_field], outputs=output)
 
 
 
 
164
 
165
 
166
  # ======================================================
 
40
 
41
 
42
  # ======================================================
43
+ # Update Dropdown based on mode
44
  # ======================================================
45
  def update_on_mode(mode):
46
  if mode == "stock":
47
+ return gr.update(choices=STOCK_REQ, value="info")
 
 
 
48
  elif mode == "index":
49
+ return gr.update(choices=INDEX_REQ, value="indices")
50
+ return gr.update(choices=[], value="")
 
 
 
51
 
52
 
53
  # ======================================================
54
+ # Data Fetcher (no defaults, use exactly frontend input)
55
  # ======================================================
56
+ def fetch_data(mode, req_type, name, date_str):
57
  req_type = req_type.lower()
58
+ name = name.strip()
59
  date_str = date_str.strip()
60
 
61
  if mode == "index":
62
  if req_type == "indices":
63
  return build_indices_html()
64
  elif req_type == "nse_open":
65
+ return wrap(nse_open(name))
66
  elif req_type == "nse_preopen":
67
+ return wrap(nse_preopen(name))
68
  elif req_type == "nse_fno":
69
+ return wrap(nse_fno(name))
70
  elif req_type == "nse_future":
71
+ return wrap(nse_future(name))
72
  elif req_type == "nse_bhav":
73
+ return wrap(nse_bhav(date_str)) # no default
 
 
74
  elif req_type == "nse_highlow":
75
  return wrap(nse_highlow())
76
  else:
 
78
 
79
  elif mode == "stock":
80
  if req_type == "daily":
81
+ return wrap(fetch_daily(name))
82
  elif req_type == "intraday":
83
+ return wrap(fetch_intraday(name))
84
  elif req_type == "info":
85
+ return wrap(fetch_info(name))
86
  elif req_type == "qresult":
87
+ return wrap(fetch_qresult(name))
88
  elif req_type == "result":
89
+ return wrap(fetch_result(name))
90
  elif req_type == "balance":
91
+ return wrap(fetch_balance(name))
92
  elif req_type == "cashflow":
93
+ return wrap(fetch_cashflow(name))
94
  elif req_type == "dividend":
95
+ return wrap(fetch_dividend(name))
96
  elif req_type == "split":
97
+ return wrap(fetch_split(name))
98
  elif req_type == "other":
99
+ return wrap(fetch_other(name))
100
  else:
101
  return wrap(f"<h3>No handler for {req_type}</h3>")
102
 
 
111
  gr.Markdown("### **Stock / Index Data Fetcher**")
112
 
113
  with gr.Row():
 
114
  mode_input = gr.Radio(
115
  ["stock", "index"],
116
  label="Mode",
 
118
  scale=1
119
  )
120
 
121
+ name_input = gr.Textbox(
122
  label="Symbol / Index Name",
123
  value="ITC",
124
+ placeholder="Enter stock symbol or index",
125
  scale=2
126
  )
127
 
128
+ req_type_input = gr.Dropdown(
129
  label="Request Type",
130
+ choices=STOCK_REQ + INDEX_REQ,
131
+ value="info",
132
+ allow_custom_value=True,
133
  scale=2
134
  )
135
 
136
+ date_input = gr.Textbox(
137
  label="Date",
138
  value="",
139
  placeholder="DD-MM-YYYY",
140
  scale=1
141
  )
142
 
143
+ fetch_btn = gr.Button("Fetch", scale=1)
144
 
145
  output = gr.HTML(label="Output")
146
 
147
+ # Update dropdown choices when mode changes
148
  mode_input.change(
149
  update_on_mode,
150
  inputs=mode_input,
151
+ outputs=req_type_input
152
  )
153
 
154
+ # Fetch button
155
+ fetch_btn.click(
156
+ fetch_data,
157
+ inputs=[mode_input, req_type_input, name_input, date_input],
158
+ outputs=output
159
+ )
160
 
161
 
162
  # ======================================================