File size: 7,445 Bytes
7eee327 7f8f8cc 936b995 7f8f8cc 8dfb875 5534b57 8dfb875 5534b57 8dfb875 7eee327 18f1b43 7eee327 18f1b43 7eee327 dd973d7 7eee327 7f8f8cc 18f1b43 467d84a a7cf44e 467d84a a7cf44e 467d84a 18f1b43 7f8f8cc 18f1b43 7f8f8cc 18f1b43 7eee327 4d62e6d 5534b57 8dfb875 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 dd973d7 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 22653ea 7eee327 7f8f8cc 7eee327 22653ea 7eee327 22653ea 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 7f8f8cc 7eee327 ae3c78e 18f1b43 7f8f8cc 7eee327 18f1b43 7f8f8cc 18f1b43 7eee327 4d62e6d 7eee327 18f1b43 7f8f8cc 7eee327 4d62e6d 18f1b43 467d84a 4d62e6d 18f1b43 4d62e6d 18f1b43 7eee327 7f8f8cc 18f1b43 7eee327 18f1b43 7eee327 7f8f8cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
import gradio as gr
import pandas as pd
import datetime
import common
import stock
import indices_html
import index_live_html
import preopen_html
import eq_html
import bhavcopy_html
import nsepython
import yahooinfo
import build_nse_fno
# ======================================================
# Date helpers
# ======================================================
def today_str():
return datetime.date.today().strftime("%d-%m-%Y")
import datetime
def sd_ed(d: str) -> tuple[str, str]:
base_date = datetime.datetime.strptime(d, "%d-%m-%Y").date()
def is_working_day(x):
return x.weekday() < 5 # Mon–Fri
def prev_working(x):
while not is_working_day(x):
x -= datetime.timedelta(days=1)
return x
# -------- Last working day (before given date) --------
last_working = prev_working(base_date - datetime.timedelta(days=1))
# -------- 364-day back, fallback to 363, 362, ... --------
past_working = prev_working(base_date - datetime.timedelta(days=364))
return (
past_working.strftime("%d-%m-%Y"),
last_working.strftime("%d-%m-%Y")
)
# ======================================================
# Request Type Options
# ======================================================
STOCK_REQ = [
"info", "intraday", "daily", "nse_eq", "qresult", "result",
"balance", "cashflow", "dividend", "split", "other", "stock_hist"
]
INDEX_REQ = [
"indices", "nse_open", "nse_preopen", "nse_fno", "nse_fiidii",
"nse_events", "nse_future", "nse_bhav", "nse_highlow","stock_highlow",
"index_history", "nse_largedeals", "nse_most_active",
"largedeals_historical", "nse_bulkdeals", "nse_blockdeals",
"index_pe_pb_div", "index_total_returns"
]
# ======================================================
# Update UI based on mode
# ======================================================
def update_on_mode(mode):
if mode == "stock":
return (
gr.update(choices=STOCK_REQ, value="info"),
gr.update(value="ITC"),
gr.update(value=today_str())
)
elif mode == "index":
return (
gr.update(choices=INDEX_REQ, value="indices"),
gr.update(value="NIFTY 50"),
gr.update(value=today_str())
)
return (
gr.update(choices=[], value=""),
gr.update(value=""),
gr.update(value="")
)
# ======================================================
# Data Fetcher
# ======================================================
def fetch_data(mode, req_type, name, date_str):
req_type = req_type.lower()
name = name.strip()
from_date,to_date = sd_ed(date_str.strip())
if mode == "index":
if req_type == "indices":
return indices_html.build_indices_html()
elif req_type == "nse_open":
return index_live_html.build_index_live_html()
elif req_type == "nse_preopen":
return preopen_html.build_preopen_html()
elif req_type == "nse_fno":
return build_nse_fno.nse_fno_html(to_date, name)
elif req_type == "nse_events":
return nsepython.nse_events().to_html()
elif req_type == "nse_fiidii":
return nsepython.nse_fiidii().to_html()
elif req_type == "nse_future":
return common.wrap(nsepython.nse_future(name))
elif req_type == "nse_highlow":
return nsepython.nse_highlow(to_date).to_html()
elif req_type == "stock_highlow":
return nsepython.stock_highlow(to_date).to_html()
elif req_type == "nse_bhav":
return bhavcopy_html.build_bhavcopy_html(to_date)
elif req_type == "nse_largedeals":
return nsepython.nse_largedeals().to_html()
elif req_type == "nse_bulkdeals":
return nsepython.nse_bulkdeals().to_html()
elif req_type == "nse_blockdeals":
return nsepython.nse_blockdeals().to_html()
elif req_type == "nse_most_active":
return nsepython.nse_most_active().to_html()
elif req_type == "index_history":
return nsepython.index_history("NIFTY", from_date, to_date).to_html()
elif req_type == "largedeals_historical":
return nsepython.nse_largedeals_historical(from_date, to_date).to_html()
elif req_type == "index_pe_pb_div":
return nsepython.index_pe_pb_div("NIFTY", from_date, to_date).to_html()
elif req_type == "index_total_returns":
return nsepython.index_total_returns("NIFTY", from_date, to_date).to_html()
else:
return common.wrap(f"<h3>No handler for {req_type}</h3>")
elif mode == "stock":
if req_type == "daily":
return common.wrap(stock.fetch_daily(name))
elif req_type == "nse_eq":
return eq_html.build_eq_html(name)
elif req_type == "intraday":
return common.wrap(stock.fetch_intraday(name))
elif req_type == "info":
return common.wrap(yahooinfo.fetch_info(name))
elif req_type == "qresult":
return common.wrap(stock.fetch_qresult(name))
elif req_type == "result":
return common.wrap(stock.fetch_result(name))
elif req_type == "balance":
return common.wrap(stock.fetch_balance(name))
elif req_type == "cashflow":
return common.wrap(stock.fetch_cashflow(name))
elif req_type == "dividend":
return common.wrap(stock.fetch_dividend(name))
elif req_type == "split":
return common.wrap(stock.fetch_split(name))
elif req_type == "other":
return common.wrap(stock.fetch_other(name))
elif req_type == "stock_hist":
return nsepython.nse_stock_hist(from_date, to_date, name).to_html()
else:
return common.wrap(f"<h3>No handler for {req_type}</h3>")
return common.wrap(f"<h3>No valid mode: {mode}</h3>")
# ======================================================
# Gradio UI
# ======================================================
with gr.Blocks(title="Stock / Index App") as iface:
gr.Markdown("### **Stock / Index Data Fetcher**")
with gr.Row():
mode_input = gr.Radio(
["stock", "index"],
label="Mode",
value="stock",
scale=1
)
name_input = gr.Textbox(label="Symbol / Index Name", scale=2)
req_type_input = gr.Dropdown(label="Request Type", allow_custom_value=True, scale=2)
date_input = gr.Textbox(label="Date (DD-MM-YYYY)", placeholder="Leave empty = yesterday", scale=1)
fetch_btn = gr.Button("Fetch", scale=1)
output = gr.HTML(label="Output")
# Mode change → auto defaults
mode_input.change(
update_on_mode,
inputs=mode_input,
outputs=[req_type_input, name_input, date_input]
)
# Initial load defaults
iface.load(
update_on_mode,
inputs=mode_input,
outputs=[req_type_input, name_input, date_input]
)
# Fetch
fetch_btn.click(
fetch_data,
inputs=[mode_input, req_type_input, name_input, date_input],
outputs=output
)
# ======================================================
# Launch
# ======================================================
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860)
|