eshan6704 commited on
Commit
ad45030
·
verified ·
1 Parent(s): 6a3c696

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -40,28 +40,39 @@ 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", visible=True),
49
- gr.update(value="ITC", placeholder="Enter stock symbol")
 
50
  )
 
51
  elif mode == "index":
52
- return (
53
- gr.update(choices=INDEX_REQ, value="nse_indices", visible=True),
54
- gr.update(value="NIFTY 50", placeholder="Enter index name or date for bhavcopy (DDMMYYYY / DD-MM-YYYY / DD/MM/YYYY)")
55
- )
56
- return (gr.update(visible=False), gr.update(value="", placeholder=""))
 
 
 
 
 
 
57
 
58
 
59
  # ======================================================
60
  # Data Fetcher
61
  # ======================================================
62
- def fetch_data(mode, req_type, name):
63
  req_type = req_type.lower()
64
- symbol = name.strip()
65
 
66
  if mode == "index":
67
  if req_type == "nse_indices":
@@ -75,8 +86,8 @@ def fetch_data(mode, req_type, name):
75
  elif req_type == "nse_future":
76
  return wrap(nse_future(symbol))
77
  elif req_type == "nse_bhav":
78
- # If blank, use today
79
- date_input = symbol or pd.Timestamp.today().strftime("%d-%m-%Y")
80
  return wrap(nse_bhav(date_input))
81
  elif req_type == "nse_highlow":
82
  return wrap(nse_highlow())
@@ -140,19 +151,32 @@ with gr.Blocks(title="Stock / Index App") as iface:
140
  scale=2
141
  )
142
 
 
 
 
 
 
 
 
 
143
  btn = gr.Button("Fetch", scale=1)
144
 
145
  output = gr.HTML(label="Output")
146
 
147
- # Mode changes dropdown + symbol placeholder
148
  mode_input.change(
149
  update_on_mode,
150
- inputs=mode_input,
151
- outputs=[req_type, symbol]
 
 
 
 
 
152
  )
153
 
154
  # Fetch button
155
- btn.click(fetch_data, inputs=[mode_input, req_type, symbol], outputs=output)
156
 
157
 
158
  # ======================================================
 
40
 
41
 
42
  # ======================================================
43
+ # Update Dropdown + Symbol + Date Visibility Based on Mode/Req
44
  # ======================================================
45
+ def update_on_mode(mode, req_type_val):
46
+ # Default: hide date field
47
+ date_field_update = gr.update(visible=False, value="")
48
+
49
  if mode == "stock":
50
  return (
51
  gr.update(choices=STOCK_REQ, value="info", visible=True),
52
+ gr.update(value="ITC", placeholder="Enter stock symbol"),
53
+ date_field_update
54
  )
55
+
56
  elif mode == "index":
57
+ # Dropdown choices
58
+ dropdown_update = gr.update(choices=INDEX_REQ, value="nse_indices", visible=True)
59
+ symbol_update = gr.update(value="NIFTY 50", placeholder="Enter index name")
60
+
61
+ # If bhavcopy is selected, show date field
62
+ if req_type_val == "nse_bhav":
63
+ date_field_update = gr.update(visible=True, placeholder="Enter date (DD-MM-YYYY)")
64
+
65
+ return dropdown_update, symbol_update, date_field_update
66
+
67
+ return gr.update(visible=False), gr.update(value=""), date_field_update
68
 
69
 
70
  # ======================================================
71
  # Data Fetcher
72
  # ======================================================
73
+ def fetch_data(mode, req_type, symbol, date_str):
74
  req_type = req_type.lower()
75
+ symbol = symbol.strip()
76
 
77
  if mode == "index":
78
  if req_type == "nse_indices":
 
86
  elif req_type == "nse_future":
87
  return wrap(nse_future(symbol))
88
  elif req_type == "nse_bhav":
89
+ # Use date_str if provided, else today
90
+ date_input = date_str.strip() or pd.Timestamp.today().strftime("%d-%m-%Y")
91
  return wrap(nse_bhav(date_input))
92
  elif req_type == "nse_highlow":
93
  return wrap(nse_highlow())
 
151
  scale=2
152
  )
153
 
154
+ date_field = gr.Textbox(
155
+ label="Date",
156
+ value="",
157
+ placeholder="DD-MM-YYYY",
158
+ visible=False,
159
+ scale=1
160
+ )
161
+
162
  btn = gr.Button("Fetch", scale=1)
163
 
164
  output = gr.HTML(label="Output")
165
 
166
+ # Update dropdown, symbol, date field when mode OR request type changes
167
  mode_input.change(
168
  update_on_mode,
169
+ inputs=[mode_input, req_type],
170
+ outputs=[req_type, symbol, date_field]
171
+ )
172
+ req_type.change(
173
+ update_on_mode,
174
+ inputs=[mode_input, req_type],
175
+ outputs=[req_type, symbol, date_field]
176
  )
177
 
178
  # Fetch button
179
+ btn.click(fetch_data, inputs=[mode_input, req_type, symbol, date_field], outputs=output)
180
 
181
 
182
  # ======================================================