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"