Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,18 @@
|
|
| 1 |
-
from common import *
|
| 2 |
import gradio as gr
|
| 3 |
-
from stock import *
|
| 4 |
-
from indices_html import *
|
| 5 |
-
from index_live_html import *
|
| 6 |
-
from preopen_html import *
|
| 7 |
-
from eq_html import *
|
| 8 |
import pandas as pd
|
| 9 |
-
from bhavcopy_html import *
|
| 10 |
-
from nsepython import *
|
| 11 |
-
from yahooinfo import fetch_info
|
| 12 |
-
from build_nse_fno import nse_fno_html
|
| 13 |
import datetime
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# ======================================================
|
| 17 |
# Request Type Options
|
|
@@ -29,6 +30,7 @@ INDEX_REQ = [
|
|
| 29 |
"index_pe_pb_div", "index_total_returns"
|
| 30 |
]
|
| 31 |
|
|
|
|
| 32 |
# ======================================================
|
| 33 |
# Update UI based on mode
|
| 34 |
# ======================================================
|
|
@@ -37,137 +39,101 @@ def update_on_mode(mode):
|
|
| 37 |
return (
|
| 38 |
gr.update(choices=STOCK_REQ, value="info"),
|
| 39 |
gr.update(value="ITC"),
|
| 40 |
-
gr.update(value=yesterday_str())
|
| 41 |
)
|
| 42 |
-
|
| 43 |
elif mode == "index":
|
| 44 |
return (
|
| 45 |
gr.update(choices=INDEX_REQ, value="indices"),
|
| 46 |
gr.update(value="NIFTY 50"),
|
| 47 |
-
gr.update(value=yesterday_str())
|
| 48 |
)
|
| 49 |
-
|
| 50 |
return (
|
| 51 |
gr.update(choices=[], value=""),
|
| 52 |
gr.update(value=""),
|
| 53 |
gr.update(value="")
|
| 54 |
)
|
| 55 |
|
|
|
|
| 56 |
# ======================================================
|
| 57 |
-
# Data Fetcher
|
| 58 |
# ======================================================
|
| 59 |
def fetch_data(mode, req_type, name, date_str):
|
| 60 |
req_type = req_type.lower()
|
| 61 |
name = name.strip()
|
| 62 |
-
to_date = date_str.strip()
|
| 63 |
-
|
| 64 |
-
# ✅ Frontend may send empty date → auto yesterday
|
| 65 |
-
if not date_str:
|
| 66 |
-
to_date = today_str()
|
| 67 |
-
|
| 68 |
-
from_date = last_year_str(to_date)
|
| 69 |
|
| 70 |
if mode == "index":
|
| 71 |
-
|
| 72 |
if req_type == "indices":
|
| 73 |
-
return build_indices_html()
|
| 74 |
-
|
| 75 |
elif req_type == "nse_open":
|
| 76 |
-
return build_index_live_html()
|
| 77 |
-
|
| 78 |
elif req_type == "nse_preopen":
|
| 79 |
-
return build_preopen_html()
|
| 80 |
-
|
| 81 |
elif req_type == "nse_fno":
|
| 82 |
-
return nse_fno_html(to_date,name)
|
| 83 |
-
|
| 84 |
elif req_type == "nse_events":
|
| 85 |
-
return nse_events().to_html()
|
| 86 |
-
|
| 87 |
elif req_type == "nse_fiidii":
|
| 88 |
-
return nse_fiidii().to_html()
|
| 89 |
-
|
| 90 |
elif req_type == "nse_future":
|
| 91 |
-
return wrap(nse_future(name))
|
| 92 |
-
|
| 93 |
elif req_type == "nse_highlow":
|
| 94 |
-
return nse_highlow(to_date).to_html()
|
| 95 |
-
|
| 96 |
elif req_type == "nse_bhav":
|
| 97 |
-
return build_bhavcopy_html(to_date)
|
| 98 |
-
|
| 99 |
elif req_type == "nse_largedeals":
|
| 100 |
-
return nse_largedeals().to_html()
|
| 101 |
-
|
| 102 |
elif req_type == "nse_bulkdeals":
|
| 103 |
-
return nse_bulkdeals().to_html()
|
| 104 |
-
|
| 105 |
elif req_type == "nse_blockdeals":
|
| 106 |
-
return nse_blockdeals().to_html()
|
| 107 |
-
|
| 108 |
elif req_type == "nse_most_active":
|
| 109 |
-
return nse_most_active().to_html()
|
| 110 |
-
|
| 111 |
elif req_type == "index_history":
|
| 112 |
-
return index_history("NIFTY 50", from_date, to_date).to_html()
|
| 113 |
-
|
| 114 |
elif req_type == "largedeals_historical":
|
| 115 |
-
return nse_largedeals_historical(from_date, to_date).to_html()
|
| 116 |
-
|
| 117 |
elif req_type == "index_pe_pb_div":
|
| 118 |
-
return index_pe_pb_div("NIFTY 50", from_date, to_date).to_html()
|
| 119 |
-
|
| 120 |
elif req_type == "index_total_returns":
|
| 121 |
-
return index_total_returns("NIFTY 50", from_date, to_date).to_html()
|
| 122 |
-
|
| 123 |
else:
|
| 124 |
-
return wrap(f"<h3>No handler for {req_type}</h3>")
|
| 125 |
|
| 126 |
elif mode == "stock":
|
| 127 |
-
|
| 128 |
if req_type == "daily":
|
| 129 |
-
return wrap(fetch_daily(name))
|
| 130 |
-
|
| 131 |
elif req_type == "nse_eq":
|
| 132 |
-
return build_eq_html(name)
|
| 133 |
-
|
| 134 |
elif req_type == "intraday":
|
| 135 |
-
return wrap(fetch_intraday(name))
|
| 136 |
-
|
| 137 |
elif req_type == "info":
|
| 138 |
-
return wrap(fetch_info(name))
|
| 139 |
-
|
| 140 |
elif req_type == "qresult":
|
| 141 |
-
return wrap(fetch_qresult(name))
|
| 142 |
-
|
| 143 |
elif req_type == "result":
|
| 144 |
-
return wrap(fetch_result(name))
|
| 145 |
-
|
| 146 |
elif req_type == "balance":
|
| 147 |
-
return wrap(fetch_balance(name))
|
| 148 |
-
|
| 149 |
elif req_type == "cashflow":
|
| 150 |
-
return wrap(fetch_cashflow(name))
|
| 151 |
-
|
| 152 |
elif req_type == "dividend":
|
| 153 |
-
return wrap(fetch_dividend(name))
|
| 154 |
-
|
| 155 |
elif req_type == "split":
|
| 156 |
-
return wrap(fetch_split(name))
|
| 157 |
-
|
| 158 |
elif req_type == "other":
|
| 159 |
-
return wrap(fetch_other(name))
|
| 160 |
-
|
| 161 |
elif req_type == "stock_hist":
|
| 162 |
-
return nse_stock_hist(from_date, to_date, name).to_html()
|
| 163 |
-
|
| 164 |
else:
|
| 165 |
-
return wrap(f"<h3>No handler for {req_type}</h3>")
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
return wrap(f"<h3>No valid mode: {mode}</h3>")
|
| 168 |
|
| 169 |
# ======================================================
|
| 170 |
-
# UI
|
| 171 |
# ======================================================
|
| 172 |
with gr.Blocks(title="Stock / Index App") as iface:
|
| 173 |
|
|
@@ -180,24 +146,9 @@ with gr.Blocks(title="Stock / Index App") as iface:
|
|
| 180 |
value="stock",
|
| 181 |
scale=1
|
| 182 |
)
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
scale=2
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
req_type_input = gr.Dropdown(
|
| 190 |
-
label="Request Type",
|
| 191 |
-
allow_custom_value=True,
|
| 192 |
-
scale=2
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
date_input = gr.Textbox(
|
| 196 |
-
label="Date (DD-MM-YYYY)",
|
| 197 |
-
placeholder="Leave empty = yesterday",
|
| 198 |
-
scale=1
|
| 199 |
-
)
|
| 200 |
-
|
| 201 |
fetch_btn = gr.Button("Fetch", scale=1)
|
| 202 |
|
| 203 |
output = gr.HTML(label="Output")
|
|
@@ -223,8 +174,9 @@ with gr.Blocks(title="Stock / Index App") as iface:
|
|
| 223 |
outputs=output
|
| 224 |
)
|
| 225 |
|
|
|
|
| 226 |
# ======================================================
|
| 227 |
# Launch
|
| 228 |
# ======================================================
|
| 229 |
if __name__ == "__main__":
|
| 230 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import datetime
|
| 4 |
|
| 5 |
+
import common
|
| 6 |
+
import stock
|
| 7 |
+
import indices_html
|
| 8 |
+
import index_live_html
|
| 9 |
+
import preopen_html
|
| 10 |
+
import eq_html
|
| 11 |
+
import bhavcopy_html
|
| 12 |
+
import nsepython
|
| 13 |
+
import yahooinfo
|
| 14 |
+
import build_nse_fno
|
| 15 |
+
|
| 16 |
|
| 17 |
# ======================================================
|
| 18 |
# Request Type Options
|
|
|
|
| 30 |
"index_pe_pb_div", "index_total_returns"
|
| 31 |
]
|
| 32 |
|
| 33 |
+
|
| 34 |
# ======================================================
|
| 35 |
# Update UI based on mode
|
| 36 |
# ======================================================
|
|
|
|
| 39 |
return (
|
| 40 |
gr.update(choices=STOCK_REQ, value="info"),
|
| 41 |
gr.update(value="ITC"),
|
| 42 |
+
gr.update(value=common.yesterday_str())
|
| 43 |
)
|
|
|
|
| 44 |
elif mode == "index":
|
| 45 |
return (
|
| 46 |
gr.update(choices=INDEX_REQ, value="indices"),
|
| 47 |
gr.update(value="NIFTY 50"),
|
| 48 |
+
gr.update(value=common.yesterday_str())
|
| 49 |
)
|
|
|
|
| 50 |
return (
|
| 51 |
gr.update(choices=[], value=""),
|
| 52 |
gr.update(value=""),
|
| 53 |
gr.update(value="")
|
| 54 |
)
|
| 55 |
|
| 56 |
+
|
| 57 |
# ======================================================
|
| 58 |
+
# Data Fetcher
|
| 59 |
# ======================================================
|
| 60 |
def fetch_data(mode, req_type, name, date_str):
|
| 61 |
req_type = req_type.lower()
|
| 62 |
name = name.strip()
|
| 63 |
+
to_date = date_str.strip() or common.today_str()
|
| 64 |
+
from_date = common.last_year_str(to_date)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
if mode == "index":
|
|
|
|
| 67 |
if req_type == "indices":
|
| 68 |
+
return indices_html.build_indices_html()
|
|
|
|
| 69 |
elif req_type == "nse_open":
|
| 70 |
+
return index_live_html.build_index_live_html()
|
|
|
|
| 71 |
elif req_type == "nse_preopen":
|
| 72 |
+
return preopen_html.build_preopen_html()
|
|
|
|
| 73 |
elif req_type == "nse_fno":
|
| 74 |
+
return build_nse_fno.nse_fno_html(to_date, name)
|
|
|
|
| 75 |
elif req_type == "nse_events":
|
| 76 |
+
return nsepython.nse_events().to_html()
|
|
|
|
| 77 |
elif req_type == "nse_fiidii":
|
| 78 |
+
return nsepython.nse_fiidii().to_html()
|
|
|
|
| 79 |
elif req_type == "nse_future":
|
| 80 |
+
return common.wrap(nsepython.nse_future(name))
|
|
|
|
| 81 |
elif req_type == "nse_highlow":
|
| 82 |
+
return nsepython.nse_highlow(to_date).to_html()
|
|
|
|
| 83 |
elif req_type == "nse_bhav":
|
| 84 |
+
return bhavcopy_html.build_bhavcopy_html(to_date)
|
|
|
|
| 85 |
elif req_type == "nse_largedeals":
|
| 86 |
+
return nsepython.nse_largedeals().to_html()
|
|
|
|
| 87 |
elif req_type == "nse_bulkdeals":
|
| 88 |
+
return nsepython.nse_bulkdeals().to_html()
|
|
|
|
| 89 |
elif req_type == "nse_blockdeals":
|
| 90 |
+
return nsepython.nse_blockdeals().to_html()
|
|
|
|
| 91 |
elif req_type == "nse_most_active":
|
| 92 |
+
return nsepython.nse_most_active().to_html()
|
|
|
|
| 93 |
elif req_type == "index_history":
|
| 94 |
+
return nsepython.index_history("NIFTY 50", from_date, to_date).to_html()
|
|
|
|
| 95 |
elif req_type == "largedeals_historical":
|
| 96 |
+
return nsepython.nse_largedeals_historical(from_date, to_date).to_html()
|
|
|
|
| 97 |
elif req_type == "index_pe_pb_div":
|
| 98 |
+
return nsepython.index_pe_pb_div("NIFTY 50", from_date, to_date).to_html()
|
|
|
|
| 99 |
elif req_type == "index_total_returns":
|
| 100 |
+
return nsepython.index_total_returns("NIFTY 50", from_date, to_date).to_html()
|
|
|
|
| 101 |
else:
|
| 102 |
+
return common.wrap(f"<h3>No handler for {req_type}</h3>")
|
| 103 |
|
| 104 |
elif mode == "stock":
|
|
|
|
| 105 |
if req_type == "daily":
|
| 106 |
+
return common.wrap(stock.fetch_daily(name))
|
|
|
|
| 107 |
elif req_type == "nse_eq":
|
| 108 |
+
return eq_html.build_eq_html(name)
|
|
|
|
| 109 |
elif req_type == "intraday":
|
| 110 |
+
return common.wrap(stock.fetch_intraday(name))
|
|
|
|
| 111 |
elif req_type == "info":
|
| 112 |
+
return common.wrap(yahooinfo.fetch_info(name))
|
|
|
|
| 113 |
elif req_type == "qresult":
|
| 114 |
+
return common.wrap(stock.fetch_qresult(name))
|
|
|
|
| 115 |
elif req_type == "result":
|
| 116 |
+
return common.wrap(stock.fetch_result(name))
|
|
|
|
| 117 |
elif req_type == "balance":
|
| 118 |
+
return common.wrap(stock.fetch_balance(name))
|
|
|
|
| 119 |
elif req_type == "cashflow":
|
| 120 |
+
return common.wrap(stock.fetch_cashflow(name))
|
|
|
|
| 121 |
elif req_type == "dividend":
|
| 122 |
+
return common.wrap(stock.fetch_dividend(name))
|
|
|
|
| 123 |
elif req_type == "split":
|
| 124 |
+
return common.wrap(stock.fetch_split(name))
|
|
|
|
| 125 |
elif req_type == "other":
|
| 126 |
+
return common.wrap(stock.fetch_other(name))
|
|
|
|
| 127 |
elif req_type == "stock_hist":
|
| 128 |
+
return stock.nse_stock_hist(from_date, to_date, name).to_html()
|
|
|
|
| 129 |
else:
|
| 130 |
+
return common.wrap(f"<h3>No handler for {req_type}</h3>")
|
| 131 |
+
|
| 132 |
+
return common.wrap(f"<h3>No valid mode: {mode}</h3>")
|
| 133 |
|
|
|
|
| 134 |
|
| 135 |
# ======================================================
|
| 136 |
+
# Gradio UI
|
| 137 |
# ======================================================
|
| 138 |
with gr.Blocks(title="Stock / Index App") as iface:
|
| 139 |
|
|
|
|
| 146 |
value="stock",
|
| 147 |
scale=1
|
| 148 |
)
|
| 149 |
+
name_input = gr.Textbox(label="Symbol / Index Name", scale=2)
|
| 150 |
+
req_type_input = gr.Dropdown(label="Request Type", allow_custom_value=True, scale=2)
|
| 151 |
+
date_input = gr.Textbox(label="Date (DD-MM-YYYY)", placeholder="Leave empty = yesterday", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
fetch_btn = gr.Button("Fetch", scale=1)
|
| 153 |
|
| 154 |
output = gr.HTML(label="Output")
|
|
|
|
| 174 |
outputs=output
|
| 175 |
)
|
| 176 |
|
| 177 |
+
|
| 178 |
# ======================================================
|
| 179 |
# Launch
|
| 180 |
# ======================================================
|
| 181 |
if __name__ == "__main__":
|
| 182 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|