Spaces:
Running
Running
Delete app2.py
Browse files
app2.py
DELETED
|
@@ -1,211 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from stock import *
|
| 3 |
-
from indices_html import *
|
| 4 |
-
from index_live_html import *
|
| 5 |
-
from preopen_html import *
|
| 6 |
-
from eq_html import *
|
| 7 |
-
import pandas as pd
|
| 8 |
-
from bhavcopy_html import *
|
| 9 |
-
from nsepython import *
|
| 10 |
-
from yahooinfo import fetch_info
|
| 11 |
-
|
| 12 |
-
# ======================================================
|
| 13 |
-
# Scrollable HTML wrapper
|
| 14 |
-
# ======================================================
|
| 15 |
-
SCROLL_WRAP = """
|
| 16 |
-
<div style="
|
| 17 |
-
max-height: 80vh;
|
| 18 |
-
overflow-y: auto;
|
| 19 |
-
overflow-x: auto;
|
| 20 |
-
padding: 10px;
|
| 21 |
-
border: 1px solid #ccc;
|
| 22 |
-
border-radius: 6px;
|
| 23 |
-
">
|
| 24 |
-
{{HTML}}
|
| 25 |
-
</div>
|
| 26 |
-
"""
|
| 27 |
-
|
| 28 |
-
import datetime
|
| 29 |
-
|
| 30 |
-
def last_year_date(d):
|
| 31 |
-
# d = "09-12-2025"
|
| 32 |
-
dt = datetime.datetime.strptime(d, "%d-%m-%Y")
|
| 33 |
-
new_dt = dt.replace(year=dt.year - 1)
|
| 34 |
-
return new_dt.strftime("%d-%m-%Y")
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def wrap(html):
|
| 39 |
-
if html is None:
|
| 40 |
-
return "<h3>No Data</h3>"
|
| 41 |
-
return SCROLL_WRAP.replace("{{HTML}}", html)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# ======================================================
|
| 45 |
-
# Request Type Options
|
| 46 |
-
# ======================================================
|
| 47 |
-
STOCK_REQ = [
|
| 48 |
-
"info", "intraday", "daily","nse_eq", "qresult", "result", "balance",
|
| 49 |
-
"cashflow", "dividend", "split", "other","stock_hist"
|
| 50 |
-
]
|
| 51 |
-
|
| 52 |
-
INDEX_REQ = [
|
| 53 |
-
"indices", "nse_open", "nse_preopen", "nse_fno","nse_fiidii","nse_events"
|
| 54 |
-
"nse_future", "nse_bhav", "nse_highlow","index_history","nse_largedeals","nse_most_active","largedeals_historical","nse_bulkdeals",
|
| 55 |
-
"nse_blockdeals","index_pe_pb_div","index_total_returns"
|
| 56 |
-
]
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# ======================================================
|
| 60 |
-
# Update Dropdown based on mode
|
| 61 |
-
# ======================================================
|
| 62 |
-
def update_on_mode(mode):
|
| 63 |
-
if mode == "stock":
|
| 64 |
-
return gr.update(choices=STOCK_REQ, value="info")
|
| 65 |
-
elif mode == "index":
|
| 66 |
-
return gr.update(choices=INDEX_REQ, value="indices")
|
| 67 |
-
return gr.update(choices=[], value="")
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# ======================================================
|
| 71 |
-
# Data Fetcher (no defaults, use exactly frontend input)
|
| 72 |
-
# ======================================================
|
| 73 |
-
def fetch_data(mode, req_type, name, date_str):
|
| 74 |
-
req_type = req_type.lower()
|
| 75 |
-
name = name.strip()
|
| 76 |
-
date_str = date_str.strip()
|
| 77 |
-
date_start= last_year_date(date_str)
|
| 78 |
-
if mode == "index":
|
| 79 |
-
if req_type == "indices":
|
| 80 |
-
return build_indices_html()
|
| 81 |
-
|
| 82 |
-
elif req_type == "nse_open":
|
| 83 |
-
return build_index_live_html()
|
| 84 |
-
elif req_type == "nse_preopen":
|
| 85 |
-
return build_preopen_html()
|
| 86 |
-
elif req_type == "nse_fno":
|
| 87 |
-
return wrap(nse_fno(name))
|
| 88 |
-
elif req_type == "nse_events":
|
| 89 |
-
return nse_events().to_html()
|
| 90 |
-
elif req_type == "nse_fiidii":
|
| 91 |
-
return nse_fiidii().to_html()
|
| 92 |
-
elif req_type == "nse_future":
|
| 93 |
-
return wrap(nse_future(name))
|
| 94 |
-
elif req_type == "nse_highlow":
|
| 95 |
-
return wrap(nse_highlow())
|
| 96 |
-
elif req_type == "nse_bhav":
|
| 97 |
-
return build_bhavcopy_html(date_str) # no default
|
| 98 |
-
elif req_type == "nse_largedeals":
|
| 99 |
-
return nse_largedeals().to_html()
|
| 100 |
-
elif req_type == "nse_bulkdeals":
|
| 101 |
-
return nse_bulkdeals().to_html()
|
| 102 |
-
|
| 103 |
-
elif req_type == "nse_blockdeals":
|
| 104 |
-
return nse_blockdeals().to_html()
|
| 105 |
-
elif req_type == "nse_most_active":
|
| 106 |
-
return nse_most_active().to_html()
|
| 107 |
-
elif req_type == "index_history":
|
| 108 |
-
return index_history("NIFTY 50",date_start,date_str).to_html() # no default
|
| 109 |
-
elif req_type == "largedeals_historical":
|
| 110 |
-
return nse_largedeals_historical(date_start,date_str).to_html() # no default
|
| 111 |
-
elif req_type == "index_pe_pb_div":
|
| 112 |
-
return index_pe_pb_div("NIFY 50",date_start,date_str).to_html() # no default
|
| 113 |
-
elif req_type == "index_total_returns":
|
| 114 |
-
return index_total_returns("NIFY 50",date_start,date_str).to_html() # no default
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
else:
|
| 118 |
-
return wrap(f"<h3>No handler for {req_type}</h3>")
|
| 119 |
-
|
| 120 |
-
elif mode == "stock":
|
| 121 |
-
if req_type == "daily":
|
| 122 |
-
return wrap(fetch_daily(name))
|
| 123 |
-
elif req_type == "nse_eq":
|
| 124 |
-
return build_eq_html(name)
|
| 125 |
-
elif req_type == "intraday":
|
| 126 |
-
return wrap(fetch_intraday(name))
|
| 127 |
-
elif req_type == "info":
|
| 128 |
-
return wrap(fetch_info(name))
|
| 129 |
-
elif req_type == "qresult":
|
| 130 |
-
return wrap(fetch_qresult(name))
|
| 131 |
-
elif req_type == "result":
|
| 132 |
-
return wrap(fetch_result(name))
|
| 133 |
-
elif req_type == "balance":
|
| 134 |
-
return wrap(fetch_balance(name))
|
| 135 |
-
elif req_type == "cashflow":
|
| 136 |
-
return wrap(fetch_cashflow(name))
|
| 137 |
-
elif req_type == "dividend":
|
| 138 |
-
return wrap(fetch_dividend(name))
|
| 139 |
-
elif req_type == "split":
|
| 140 |
-
return wrap(fetch_split(name))
|
| 141 |
-
elif req_type == "other":
|
| 142 |
-
return wrap(fetch_other(name))
|
| 143 |
-
elif req_type == "stock_hist":
|
| 144 |
-
return nse_stock_hist(date_start,date_str,name).to_html()
|
| 145 |
-
else:
|
| 146 |
-
return wrap(f"<h3>No handler for {req_type}</h3>")
|
| 147 |
-
|
| 148 |
-
return wrap(f"<h3>No valid mode: {mode}</h3>")
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
# ======================================================
|
| 152 |
-
# UI
|
| 153 |
-
# ======================================================
|
| 154 |
-
with gr.Blocks(title="Stock / Index App") as iface:
|
| 155 |
-
|
| 156 |
-
gr.Markdown("### **Stock / Index Data Fetcher**")
|
| 157 |
-
|
| 158 |
-
with gr.Row():
|
| 159 |
-
mode_input = gr.Radio(
|
| 160 |
-
["stock", "index"],
|
| 161 |
-
label="Mode",
|
| 162 |
-
value="stock",
|
| 163 |
-
scale=1
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
name_input = gr.Textbox(
|
| 167 |
-
label="Symbol / Index Name",
|
| 168 |
-
value="ITC",
|
| 169 |
-
placeholder="Enter stock symbol or index",
|
| 170 |
-
scale=2
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
req_type_input = gr.Dropdown(
|
| 174 |
-
label="Request Type",
|
| 175 |
-
choices=STOCK_REQ + INDEX_REQ,
|
| 176 |
-
value="info",
|
| 177 |
-
allow_custom_value=True,
|
| 178 |
-
scale=2
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
date_input = gr.Textbox(
|
| 182 |
-
label="Date",
|
| 183 |
-
value="",
|
| 184 |
-
placeholder="DD-MM-YYYY",
|
| 185 |
-
scale=1
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
fetch_btn = gr.Button("Fetch", scale=1)
|
| 189 |
-
|
| 190 |
-
output = gr.HTML(label="Output")
|
| 191 |
-
|
| 192 |
-
# Update dropdown choices when mode changes
|
| 193 |
-
mode_input.change(
|
| 194 |
-
update_on_mode,
|
| 195 |
-
inputs=mode_input,
|
| 196 |
-
outputs=req_type_input
|
| 197 |
-
)
|
| 198 |
-
|
| 199 |
-
# Fetch button
|
| 200 |
-
fetch_btn.click(
|
| 201 |
-
fetch_data,
|
| 202 |
-
inputs=[mode_input, req_type_input, name_input, date_input],
|
| 203 |
-
outputs=output
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
# ======================================================
|
| 208 |
-
# Launch
|
| 209 |
-
# ======================================================
|
| 210 |
-
if __name__ == "__main__":
|
| 211 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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