eshan6704 commited on
Commit
b1449d5
·
verified ·
1 Parent(s): 671ff92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -2
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
2
  import yfinance as yf
 
 
3
 
4
  def fetch_data(symbol, req_type):
5
  try:
@@ -7,7 +9,6 @@ def fetch_data(symbol, req_type):
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()
@@ -23,8 +24,111 @@ def fetch_data(symbol, req_type):
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>
@@ -36,6 +140,7 @@ def fetch_data(symbol, req_type):
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
 
 
1
  import gradio as gr
2
  import yfinance as yf
3
+ import plotly.graph_objs as go
4
+ import pandas as pd
5
 
6
  def fetch_data(symbol, req_type):
7
  try:
 
9
 
10
  if req_type.lower() == "info":
11
  info = ticker.info
 
12
  rows = "".join(
13
  f"<tr><td><b>{key}</b></td><td>{value}</td></tr>"
14
  for key, value in info.items()
 
24
  </body>
25
  </html>
26
  """
27
+
28
+ elif req_type.lower() == "daily":
29
+ df = yf.download(symbol, period="1y", interval="1d")
30
+ if df.empty:
31
+ return f"<html><body><h1>No daily data for {symbol}</h1></body></html>"
32
+
33
+ fig = go.Figure()
34
+
35
+ # Candlestick
36
+ fig.add_trace(go.Candlestick(
37
+ x=df.index,
38
+ open=df['Open'],
39
+ high=df['High'],
40
+ low=df['Low'],
41
+ close=df['Close'],
42
+ name="Price"
43
+ ))
44
+
45
+ # Volume as bar chart
46
+ fig.add_trace(go.Bar(
47
+ x=df.index,
48
+ y=df['Volume'],
49
+ name="Volume",
50
+ marker_color='lightblue',
51
+ yaxis="y2"
52
+ ))
53
+
54
+ # Layout with secondary y-axis
55
+ fig.update_layout(
56
+ title=f"Daily Candlestick Chart for {symbol}",
57
+ xaxis_title="Date",
58
+ yaxis_title="Price",
59
+ yaxis2=dict(title="Volume", overlaying="y", side="right", showgrid=False),
60
+ xaxis_rangeslider_visible=False,
61
+ height=600
62
+ )
63
+
64
+ chart_html = fig.to_html(full_html=False)
65
+
66
+ # Data table
67
+ table_html = df.tail(30).to_html(classes="dataframe", border=1)
68
+
69
+ html_response = f"""
70
+ <html>
71
+ <head><title>Daily Data for {symbol}</title></head>
72
+ <body>
73
+ <h1>Daily Data for {symbol}</h1>
74
+ {chart_html}
75
+ <h2>Recent Daily Data (last 30 rows)</h2>
76
+ {table_html}
77
+ </body>
78
+ </html>
79
+ """
80
+
81
+ elif req_type.lower() == "intraday":
82
+ df = yf.download(symbol, period="1d", interval="5m")
83
+ if df.empty:
84
+ return f"<html><body><h1>No intraday data for {symbol}</h1></body></html>"
85
+
86
+ fig = go.Figure()
87
+
88
+ # Candlestick
89
+ fig.add_trace(go.Candlestick(
90
+ x=df.index,
91
+ open=df['Open'],
92
+ high=df['High'],
93
+ low=df['Low'],
94
+ close=df['Close'],
95
+ name="Price"
96
+ ))
97
+
98
+ # Volume
99
+ fig.add_trace(go.Bar(
100
+ x=df.index,
101
+ y=df['Volume'],
102
+ name="Volume",
103
+ marker_color='orange',
104
+ yaxis="y2"
105
+ ))
106
+
107
+ fig.update_layout(
108
+ title=f"Intraday Candlestick Chart for {symbol}",
109
+ xaxis_title="Time",
110
+ yaxis_title="Price",
111
+ yaxis2=dict(title="Volume", overlaying="y", side="right", showgrid=False),
112
+ xaxis_rangeslider_visible=False,
113
+ height=600
114
+ )
115
+
116
+ chart_html = fig.to_html(full_html=False)
117
+ table_html = df.tail(50).to_html(classes="dataframe", border=1)
118
+
119
+ html_response = f"""
120
+ <html>
121
+ <head><title>Intraday Data for {symbol}</title></head>
122
+ <body>
123
+ <h1>Intraday Data for {symbol}</h1>
124
+ {chart_html}
125
+ <h2>Recent Intraday Data (last 50 rows)</h2>
126
+ {table_html}
127
+ </body>
128
+ </html>
129
+ """
130
+
131
  else:
 
132
  html_response = f"""
133
  <html>
134
  <head><title>Stock Data for {symbol}</title></head>
 
140
  </body>
141
  </html>
142
  """
143
+
144
  except Exception as e:
145
  html_response = f"<html><body><h1>Error</h1><p>{str(e)}</p></body></html>"
146