Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,52 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
def fetch_data(symbol, req_type):
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return html_response
|
| 17 |
|
|
|
|
| 18 |
iface = gr.Interface(
|
| 19 |
fn=fetch_data,
|
| 20 |
inputs=[
|
| 21 |
-
gr.Textbox(label="Stock Symbol", value=
|
| 22 |
-
gr.Textbox(label="Request Type", value=
|
| 23 |
],
|
| 24 |
outputs=gr.HTML(label="Collected Stock Data"),
|
| 25 |
title="Stock Data API (Full)",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import yfinance as yf
|
| 3 |
|
| 4 |
def fetch_data(symbol, req_type):
|
| 5 |
+
try:
|
| 6 |
+
ticker = yf.Ticker(symbol)
|
| 7 |
+
|
| 8 |
+
if req_type.lower() == "info":
|
| 9 |
+
info = ticker.info
|
| 10 |
+
# Build HTML table from info dict
|
| 11 |
+
rows = "".join(
|
| 12 |
+
f"<tr><td><b>{key}</b></td><td>{value}</td></tr>"
|
| 13 |
+
for key, value in info.items()
|
| 14 |
+
)
|
| 15 |
+
html_response = f"""
|
| 16 |
+
<html>
|
| 17 |
+
<head><title>Stock Data for {symbol}</title></head>
|
| 18 |
+
<body>
|
| 19 |
+
<h1>Ticker Info for {symbol}</h1>
|
| 20 |
+
<table border="1" cellpadding="5" cellspacing="0">
|
| 21 |
+
{rows}
|
| 22 |
+
</table>
|
| 23 |
+
</body>
|
| 24 |
+
</html>
|
| 25 |
+
"""
|
| 26 |
+
else:
|
| 27 |
+
# Default static response
|
| 28 |
+
html_response = f"""
|
| 29 |
+
<html>
|
| 30 |
+
<head><title>Stock Data for {symbol}</title></head>
|
| 31 |
+
<body>
|
| 32 |
+
<h1>Data Request</h1>
|
| 33 |
+
<p>Symbol: {symbol}</p>
|
| 34 |
+
<p>Request Type: {req_type}</p>
|
| 35 |
+
<p>No special handler for this request type.</p>
|
| 36 |
+
</body>
|
| 37 |
+
</html>
|
| 38 |
+
"""
|
| 39 |
+
except Exception as e:
|
| 40 |
+
html_response = f"<html><body><h1>Error</h1><p>{str(e)}</p></body></html>"
|
| 41 |
+
|
| 42 |
return html_response
|
| 43 |
|
| 44 |
+
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=fetch_data,
|
| 47 |
inputs=[
|
| 48 |
+
gr.Textbox(label="Stock Symbol", value="PNB"),
|
| 49 |
+
gr.Textbox(label="Request Type", value="info")
|
| 50 |
],
|
| 51 |
outputs=gr.HTML(label="Collected Stock Data"),
|
| 52 |
title="Stock Data API (Full)",
|