File size: 1,486 Bytes
63495b1
96f9491
 
160fe8a
f2ba8f4
96f9491
 
160fe8a
96f9491
 
 
 
 
 
160fe8a
 
96f9491
160fe8a
 
 
 
 
 
 
 
 
 
 
 
 
f2ba8f4
160fe8a
63495b1
 
96f9491
160fe8a
 
 
 
 
 
 
 
63495b1
 
 
 
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
import gradio as gr
import requests
import pandas as pd
import matplotlib.pyplot as plt

API_KEY = "PJRAUD6KHJ2O097X"

def get_stock_data(symbol, start_date, end_date):
    url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={API_KEY}"
    response = requests.get(url)
    data = response.json()

    if "Time Series (Daily)" in data:
        df = pd.DataFrame(data["Time Series (Daily)"]).T
        df.index = pd.to_datetime(df.index)
        df = df.loc[start_date:end_date]
        df.columns = ["Open", "High", "Low", "Close", "Volume"]
        
        # Plotting
        plt.figure(figsize=(10, 5))
        plt.plot(df['Close'], label='Close Price')
        plt.title(f'Closing Price of {symbol}')
        plt.xlabel('Date')
        plt.ylabel('Price')
        plt.legend()
        plt.grid(True)
        plt.xticks(rotation=45)
        plt.tight_layout()

        return df.to_html(), gr.Image.from_matplotlib(plt)
    else:
        return "Data not found or invalid stock symbol.", None

iface = gr.Interface(
    fn=get_stock_data,
    inputs=[
        gr.Textbox(label="Stock Symbol", placeholder="Enter a stock symbol (like AAPL, MSFT)"),
        gr.Date(label="Start Date"),
        gr.Date(label="End Date")
    ],
    outputs=["html", "plot"],
    title="Personalized Stock Market Data App",
    description="Enter a stock symbol and date range to fetch its daily time series data."
)

if __name__ == "__main__":
    iface.launch()