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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -32
app.py CHANGED
@@ -4,7 +4,7 @@ from stock import *
4
  import pandas as pd
5
 
6
  # ======================================================
7
- # Scrollable HTML wrapper for table output
8
  # ======================================================
9
  SCROLL_WRAP = """
10
  <div style="
@@ -40,31 +40,20 @@ INDEX_REQ = [
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
  # ======================================================
@@ -73,6 +62,7 @@ def update_on_mode(mode, req_type_val):
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,8 +76,8 @@ def fetch_data(mode, req_type, symbol, date_str):
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())
@@ -155,7 +145,6 @@ with gr.Blocks(title="Stock / Index App") as iface:
155
  label="Date",
156
  value="",
157
  placeholder="DD-MM-YYYY",
158
- visible=False,
159
  scale=1
160
  )
161
 
@@ -163,18 +152,14 @@ with gr.Blocks(title="Stock / Index App") as iface:
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
 
 
4
  import pandas as pd
5
 
6
  # ======================================================
7
+ # Scrollable HTML wrapper
8
  # ======================================================
9
  SCROLL_WRAP = """
10
  <div style="
 
40
 
41
 
42
  # ======================================================
43
+ # Update Dropdown + Symbol
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="nse_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
  # ======================================================
 
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 == "nse_indices":
 
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())
 
145
  label="Date",
146
  value="",
147
  placeholder="DD-MM-YYYY",
 
148
  scale=1
149
  )
150
 
 
152
 
153
  output = gr.HTML(label="Output")
154
 
155
+ # Mode change updates dropdown + symbol
156
  mode_input.change(
157
  update_on_mode,
158
+ inputs=mode_input,
159
+ outputs=[req_type, symbol]
 
 
 
 
 
160
  )
161
 
162
+ # Request type change does not hide date anymore (always visible)
163
  # Fetch button
164
  btn.click(fetch_data, inputs=[mode_input, req_type, symbol, date_field], outputs=output)
165