Update app.py
Browse files
app.py
CHANGED
|
@@ -22,14 +22,49 @@ SCROLL_WRAP = """
|
|
| 22 |
|
| 23 |
|
| 24 |
def wrap(html):
|
| 25 |
-
"""Wrap returned HTML inside scrollable container"""
|
| 26 |
if html is None:
|
| 27 |
return "<h3>No Data</h3>"
|
| 28 |
return SCROLL_WRAP.replace("{{HTML}}", html)
|
| 29 |
|
| 30 |
|
| 31 |
# ======================================================
|
| 32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# ======================================================
|
| 34 |
def fetch_data(mode, req_type, name):
|
| 35 |
req_type = req_type.lower()
|
|
@@ -39,16 +74,18 @@ def fetch_data(mode, req_type, name):
|
|
| 39 |
|
| 40 |
if req_type == "nse_indices":
|
| 41 |
return wrap(nse_indices())
|
| 42 |
-
|
| 43 |
elif req_type == "nse_open":
|
| 44 |
return wrap(nse_open(name))
|
| 45 |
-
|
| 46 |
elif req_type == "nse_preopen":
|
| 47 |
return wrap(nse_preopen(name))
|
| 48 |
-
|
| 49 |
elif req_type == "nse_fno":
|
| 50 |
return wrap(nse_fno(name))
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
return wrap(f"<h3>No handler for {req_type}</h3>")
|
| 54 |
|
|
@@ -56,34 +93,24 @@ def fetch_data(mode, req_type, name):
|
|
| 56 |
|
| 57 |
if req_type == "daily":
|
| 58 |
return wrap(fetch_daily(symbol))
|
| 59 |
-
|
| 60 |
elif req_type == "intraday":
|
| 61 |
return wrap(fetch_intraday(symbol))
|
| 62 |
-
|
| 63 |
elif req_type == "info":
|
| 64 |
return wrap(fetch_info(symbol))
|
| 65 |
-
|
| 66 |
elif req_type == "qresult":
|
| 67 |
return wrap(fetch_qresult(symbol))
|
| 68 |
-
|
| 69 |
elif req_type == "result":
|
| 70 |
return wrap(fetch_result(symbol))
|
| 71 |
-
|
| 72 |
elif req_type == "balance":
|
| 73 |
return wrap(fetch_balance(symbol))
|
| 74 |
-
|
| 75 |
elif req_type == "cashflow":
|
| 76 |
return wrap(fetch_cashflow(symbol))
|
| 77 |
-
|
| 78 |
elif req_type == "dividend":
|
| 79 |
return wrap(fetch_dividend(symbol))
|
| 80 |
-
|
| 81 |
elif req_type == "split":
|
| 82 |
return wrap(fetch_split(symbol))
|
| 83 |
-
|
| 84 |
elif req_type == "other":
|
| 85 |
return wrap(fetch_other(symbol))
|
| 86 |
-
|
| 87 |
else:
|
| 88 |
return wrap(f"<h3>No handler for {req_type}</h3>")
|
| 89 |
|
|
@@ -99,35 +126,39 @@ with gr.Blocks(title="Stock / Index App") as iface:
|
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
|
| 102 |
-
mode_input = gr.
|
|
|
|
| 103 |
label="Mode",
|
| 104 |
value="stock",
|
| 105 |
-
placeholder="stock / index",
|
| 106 |
scale=1
|
| 107 |
)
|
| 108 |
|
| 109 |
symbol = gr.Textbox(
|
| 110 |
label="Symbol / Index Name",
|
| 111 |
-
value="
|
| 112 |
placeholder="Enter symbol",
|
| 113 |
scale=2
|
| 114 |
)
|
| 115 |
|
| 116 |
req_type = gr.Dropdown(
|
| 117 |
label="Request Type",
|
| 118 |
-
choices=
|
| 119 |
-
"info","intraday","daily","qresult","result","balance","cashflow",
|
| 120 |
-
"dividend","split","nse_indices","nse_open","nse_preopen","nse_fno","nse_future","nse_bhav","nse_highlow"
|
| 121 |
-
],
|
| 122 |
value="info",
|
| 123 |
scale=2
|
| 124 |
)
|
| 125 |
|
| 126 |
btn = gr.Button("Fetch", scale=1)
|
| 127 |
|
| 128 |
-
# Output box (scroll handled by wrapper)
|
| 129 |
output = gr.HTML(label="Output")
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
btn.click(fetch_data, inputs=[mode_input, req_type, symbol], outputs=output)
|
| 132 |
|
| 133 |
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
def wrap(html):
|
|
|
|
| 25 |
if html is None:
|
| 26 |
return "<h3>No Data</h3>"
|
| 27 |
return SCROLL_WRAP.replace("{{HTML}}", html)
|
| 28 |
|
| 29 |
|
| 30 |
# ======================================================
|
| 31 |
+
# REQUEST TYPE OPTIONS
|
| 32 |
+
# ======================================================
|
| 33 |
+
STOCK_REQ = [
|
| 34 |
+
"info", "intraday", "daily", "qresult", "result", "balance",
|
| 35 |
+
"cashflow", "dividend", "split", "other"
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
INDEX_REQ = [
|
| 39 |
+
"nse_indices", "nse_open", "nse_preopen", "nse_fno",
|
| 40 |
+
"nse_future", "nse_bhav", "nse_highlow"
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# ======================================================
|
| 45 |
+
# UPDATE DROPDOWN + SYMBOL BASED ON MODE
|
| 46 |
+
# ======================================================
|
| 47 |
+
def update_on_mode(mode):
|
| 48 |
+
if mode == "stock":
|
| 49 |
+
return (
|
| 50 |
+
gr.update(choices=STOCK_REQ, value="info", visible=True),
|
| 51 |
+
gr.update(value="ITC") # default stock symbol
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
elif mode == "index":
|
| 55 |
+
return (
|
| 56 |
+
gr.update(choices=INDEX_REQ, value="nse_indices", visible=True),
|
| 57 |
+
gr.update(value="NIFTY 50") # default index name
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
return (
|
| 61 |
+
gr.update(visible=False),
|
| 62 |
+
gr.update(value="")
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# ======================================================
|
| 67 |
+
# DATA FETCHER
|
| 68 |
# ======================================================
|
| 69 |
def fetch_data(mode, req_type, name):
|
| 70 |
req_type = req_type.lower()
|
|
|
|
| 74 |
|
| 75 |
if req_type == "nse_indices":
|
| 76 |
return wrap(nse_indices())
|
|
|
|
| 77 |
elif req_type == "nse_open":
|
| 78 |
return wrap(nse_open(name))
|
|
|
|
| 79 |
elif req_type == "nse_preopen":
|
| 80 |
return wrap(nse_preopen(name))
|
|
|
|
| 81 |
elif req_type == "nse_fno":
|
| 82 |
return wrap(nse_fno(name))
|
| 83 |
+
elif req_type == "nse_future":
|
| 84 |
+
return wrap(nse_future(name))
|
| 85 |
+
elif req_type == "nse_bhav":
|
| 86 |
+
return wrap(nse_bhav(name))
|
| 87 |
+
elif req_type == "nse_highlow":
|
| 88 |
+
return wrap(nse_highlow(name))
|
| 89 |
else:
|
| 90 |
return wrap(f"<h3>No handler for {req_type}</h3>")
|
| 91 |
|
|
|
|
| 93 |
|
| 94 |
if req_type == "daily":
|
| 95 |
return wrap(fetch_daily(symbol))
|
|
|
|
| 96 |
elif req_type == "intraday":
|
| 97 |
return wrap(fetch_intraday(symbol))
|
|
|
|
| 98 |
elif req_type == "info":
|
| 99 |
return wrap(fetch_info(symbol))
|
|
|
|
| 100 |
elif req_type == "qresult":
|
| 101 |
return wrap(fetch_qresult(symbol))
|
|
|
|
| 102 |
elif req_type == "result":
|
| 103 |
return wrap(fetch_result(symbol))
|
|
|
|
| 104 |
elif req_type == "balance":
|
| 105 |
return wrap(fetch_balance(symbol))
|
|
|
|
| 106 |
elif req_type == "cashflow":
|
| 107 |
return wrap(fetch_cashflow(symbol))
|
|
|
|
| 108 |
elif req_type == "dividend":
|
| 109 |
return wrap(fetch_dividend(symbol))
|
|
|
|
| 110 |
elif req_type == "split":
|
| 111 |
return wrap(fetch_split(symbol))
|
|
|
|
| 112 |
elif req_type == "other":
|
| 113 |
return wrap(fetch_other(symbol))
|
|
|
|
| 114 |
else:
|
| 115 |
return wrap(f"<h3>No handler for {req_type}</h3>")
|
| 116 |
|
|
|
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
|
| 129 |
+
mode_input = gr.SegmentedButton(
|
| 130 |
+
["stock", "index"],
|
| 131 |
label="Mode",
|
| 132 |
value="stock",
|
|
|
|
| 133 |
scale=1
|
| 134 |
)
|
| 135 |
|
| 136 |
symbol = gr.Textbox(
|
| 137 |
label="Symbol / Index Name",
|
| 138 |
+
value="ITC", # default for stock
|
| 139 |
placeholder="Enter symbol",
|
| 140 |
scale=2
|
| 141 |
)
|
| 142 |
|
| 143 |
req_type = gr.Dropdown(
|
| 144 |
label="Request Type",
|
| 145 |
+
choices=STOCK_REQ,
|
|
|
|
|
|
|
|
|
|
| 146 |
value="info",
|
| 147 |
scale=2
|
| 148 |
)
|
| 149 |
|
| 150 |
btn = gr.Button("Fetch", scale=1)
|
| 151 |
|
|
|
|
| 152 |
output = gr.HTML(label="Output")
|
| 153 |
|
| 154 |
+
# Mode changes dropdown + symbol
|
| 155 |
+
mode_input.change(
|
| 156 |
+
update_on_mode,
|
| 157 |
+
inputs=mode_input,
|
| 158 |
+
outputs=[req_type, symbol]
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
# Fetch button
|
| 162 |
btn.click(fetch_data, inputs=[mode_input, req_type, symbol], outputs=output)
|
| 163 |
|
| 164 |
|