rioanggara commited on
Commit
160fe8a
·
1 Parent(s): 96f9491
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -1,30 +1,46 @@
1
  import gradio as gr
2
  import requests
3
  import pandas as pd
 
4
 
5
- # Alpha Vantage API key
6
  API_KEY = "PJRAUD6KHJ2O097X"
7
 
8
- # Function to fetch stock data
9
- def get_stock_data(symbol):
10
  url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={API_KEY}"
11
  response = requests.get(url)
12
  data = response.json()
13
 
14
  if "Time Series (Daily)" in data:
15
  df = pd.DataFrame(data["Time Series (Daily)"]).T
 
 
16
  df.columns = ["Open", "High", "Low", "Close", "Volume"]
17
- return df.head().to_html() # Display the latest 5 days of data
 
 
 
 
 
 
 
 
 
 
 
 
18
  else:
19
- return "Data not found or invalid stock symbol."
20
 
21
- # Gradio app interface
22
  iface = gr.Interface(
23
  fn=get_stock_data,
24
- inputs=gr.Textbox(label="Stock Symbol", placeholder="Enter a stock symbol (like AAPL, MSFT)"),
25
- outputs="html",
26
- title="Stock Market Data App",
27
- description="Enter a stock symbol to fetch its daily time series data."
 
 
 
 
28
  )
29
 
30
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import requests
3
  import pandas as pd
4
+ import matplotlib.pyplot as plt
5
 
 
6
  API_KEY = "PJRAUD6KHJ2O097X"
7
 
8
+ def get_stock_data(symbol, start_date, end_date):
 
9
  url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={API_KEY}"
10
  response = requests.get(url)
11
  data = response.json()
12
 
13
  if "Time Series (Daily)" in data:
14
  df = pd.DataFrame(data["Time Series (Daily)"]).T
15
+ df.index = pd.to_datetime(df.index)
16
+ df = df.loc[start_date:end_date]
17
  df.columns = ["Open", "High", "Low", "Close", "Volume"]
18
+
19
+ # Plotting
20
+ plt.figure(figsize=(10, 5))
21
+ plt.plot(df['Close'], label='Close Price')
22
+ plt.title(f'Closing Price of {symbol}')
23
+ plt.xlabel('Date')
24
+ plt.ylabel('Price')
25
+ plt.legend()
26
+ plt.grid(True)
27
+ plt.xticks(rotation=45)
28
+ plt.tight_layout()
29
+
30
+ return df.to_html(), gr.Image.from_matplotlib(plt)
31
  else:
32
+ return "Data not found or invalid stock symbol.", None
33
 
 
34
  iface = gr.Interface(
35
  fn=get_stock_data,
36
+ inputs=[
37
+ gr.Textbox(label="Stock Symbol", placeholder="Enter a stock symbol (like AAPL, MSFT)"),
38
+ gr.Date(label="Start Date"),
39
+ gr.Date(label="End Date")
40
+ ],
41
+ outputs=["html", "plot"],
42
+ title="Personalized Stock Market Data App",
43
+ description="Enter a stock symbol and date range to fetch its daily time series data."
44
  )
45
 
46
  if __name__ == "__main__":