File size: 1,295 Bytes
ab50b27
d31c699
087ef0b
8a8357f
26d7e54
ab50b27
d31c699
 
 
8a8357f
 
d31c699
 
 
34c23a9
a261fc2
8a8357f
04f6c13
 
 
a261fc2
04f6c13
 
 
34c23a9
d31c699
 
 
357e80b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34c23a9
b073561
8b8721a
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
import streamlit as st
import requests
import pandas as pd
from datetime import datetime
import time

# Hard-coded API key for demonstration purposes
API_KEY = "QR8F9B7T6R2SWTAT"

def fetch_alpha_vantage_data(api_key, symbol):
    url = f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={api_key}'
    response = requests.get(url)
    alpha_vantage_data = response.json()
    return alpha_vantage_data

def main():
    st.title("Real-Time Stock Data")

    # User input for stock symbol
    symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")

    if not symbol:
        st.warning("Please enter a valid stock symbol.")
        st.stop()

    # Use the hard-coded API key
    api_key = API_KEY

    # Fetch Alpha Vantage data
    alpha_vantage_data = fetch_alpha_vantage_data(api_key, symbol)

    # Extract relevant data from Alpha Vantage response
    alpha_vantage_quote = alpha_vantage_data.get('Global Quote', {})
    df = pd.DataFrame([alpha_vantage_quote])
    df.index = [datetime.now()]  # Use the current timestamp as the index
    df = df.dropna(axis=0)

    # Display the real-time data
    st.subheader("Real-Time Data:")
    st.write(df)

    # Notify user and stop the app
    st.balloons()
    st.stop()

if __name__ == "__main__":
    main()