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