File size: 5,073 Bytes
47f6a53
04b86ea
856772e
04b86ea
6a3c696
856772e
71a405b
c6732fb
71a405b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a3c696
5a966e3
 
 
 
 
 
 
27aed0a
5a966e3
 
 
 
 
c6732fb
5a966e3
c6732fb
5a966e3
 
c6732fb
 
5a966e3
 
c6732fb
33a96eb
c6732fb
 
 
5a966e3
 
 
6a3c696
71a405b
ad45030
47f6a53
ad45030
c6732fb
71a405b
 
27aed0a
1ded4f4
b6bc8f2
6a3c696
b6bc8f2
6a3c696
b6bc8f2
6a3c696
5a966e3
6a3c696
5a966e3
c6732fb
 
6a3c696
5a966e3
6a3c696
71a405b
 
 
 
70a4c71
71a405b
70a4c71
71a405b
70a4c71
71a405b
70a4c71
71a405b
70a4c71
71a405b
70a4c71
71a405b
70a4c71
71a405b
70a4c71
71a405b
70a4c71
71a405b
70a4c71
71a405b
 
 
47f6a53
71a405b
bc71b9f
71a405b
 
bc71b9f
71a405b
bc71b9f
 
 
 
 
71a405b
0bb4ac0
5a966e3
bc71b9f
 
 
 
 
 
 
0bb4ac0
6a3c696
bc71b9f
 
a8fcd03
b233a95
bc71b9f
5a966e3
bc71b9f
 
b233a95
b2ed551
ad45030
 
 
 
 
 
 
bc71b9f
 
 
b2ed551
c6732fb
5a966e3
 
c6732fb
 
5a966e3
 
c6732fb
5a966e3
ad45030
47f6a53
bc71b9f
71a405b
bc71b9f
71a405b
47f6a53
1bbb697
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
import gradio as gr

from stock import *
from nsepython import *
import pandas as pd

# ======================================================
# Scrollable HTML wrapper
# ======================================================
SCROLL_WRAP = """
<div style="
    max-height: 80vh;
    overflow-y: auto;
    overflow-x: auto;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 6px;
">
{{HTML}}
</div>
"""

def wrap(html):
    if html is None:
        return "<h3>No Data</h3>"
    return SCROLL_WRAP.replace("{{HTML}}", html)


# ======================================================
# Request Type Options
# ======================================================
STOCK_REQ = [
    "info", "intraday", "daily", "qresult", "result", "balance",
    "cashflow", "dividend", "split", "other"
]

INDEX_REQ = [
    "indices", "nse_open", "nse_preopen", "nse_fno",
    "nse_future", "nse_bhav", "nse_highlow"
]


# ======================================================
# Update Dropdown + Symbol
# ======================================================
def update_on_mode(mode):
    if mode == "stock":
        return (
            gr.update(choices=STOCK_REQ, value="info"),
            gr.update(value="ITC", placeholder="Enter stock symbol")
        )
    elif mode == "index":
        return (
            gr.update(choices=INDEX_REQ, value="indices"),
            gr.update(value="NIFTY 50", placeholder="Enter index name or leave blank for today bhavcopy")
        )
    return gr.update(visible=False), gr.update(value="")


# ======================================================
# Data Fetcher
# ======================================================
def fetch_data(mode, req_type, symbol, date_str):
    req_type = req_type.lower()
    symbol = symbol.strip()
    date_str = date_str.strip()

    if mode == "index":
        if req_type == "indices":
            return build_indices_html()
        elif req_type == "nse_open":
            return wrap(nse_open(symbol))
        elif req_type == "nse_preopen":
            return wrap(nse_preopen(symbol))
        elif req_type == "nse_fno":
            return wrap(nse_fno(symbol))
        elif req_type == "nse_future":
            return wrap(nse_future(symbol))
        elif req_type == "nse_bhav":
            # Use date if provided, else today
            date_input = date_str or pd.Timestamp.today().strftime("%d-%m-%Y")
            return wrap(nse_bhav(date_input))
        elif req_type == "nse_highlow":
            return wrap(nse_highlow())
        else:
            return wrap(f"<h3>No handler for {req_type}</h3>")

    elif mode == "stock":
        if req_type == "daily":
            return wrap(fetch_daily(symbol))
        elif req_type == "intraday":
            return wrap(fetch_intraday(symbol))
        elif req_type == "info":
            return wrap(fetch_info(symbol))
        elif req_type == "qresult":
            return wrap(fetch_qresult(symbol))
        elif req_type == "result":
            return wrap(fetch_result(symbol))
        elif req_type == "balance":
            return wrap(fetch_balance(symbol))
        elif req_type == "cashflow":
            return wrap(fetch_cashflow(symbol))
        elif req_type == "dividend":
            return wrap(fetch_dividend(symbol))
        elif req_type == "split":
            return wrap(fetch_split(symbol))
        elif req_type == "other":
            return wrap(fetch_other(symbol))
        else:
            return wrap(f"<h3>No handler for {req_type}</h3>")

    return wrap(f"<h3>No valid mode: {mode}</h3>")


# ======================================================
# 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
        )

        symbol = gr.Textbox(
            label="Symbol / Index Name",
            value="ITC",
            placeholder="Enter stock symbol",
            scale=2
        )

        req_type = gr.Dropdown(
            label="Request Type",
            choices=STOCK_REQ,
            value="info",
            scale=2
        )

        date_field = gr.Textbox(
            label="Date",
            value="",
            placeholder="DD-MM-YYYY",
            scale=1
        )

        btn = gr.Button("Fetch", scale=1)

    output = gr.HTML(label="Output")

    # Mode change updates dropdown + symbol
    mode_input.change(
        update_on_mode,
        inputs=mode_input,
        outputs=[req_type, symbol]
    )

    # Request type change does not hide date anymore (always visible)
    # Fetch button
    btn.click(fetch_data, inputs=[mode_input, req_type, symbol, date_field], outputs=output)


# ======================================================
# Launch
# ======================================================
if __name__ == "__main__":
    iface.launch(server_name="0.0.0.0", server_port=7860)