St / app.py
Geek7's picture
Update app.py
83c311b verified
raw
history blame
1.61 kB
import streamlit as st
import requests
import pandas as pd
from datetime import datetime
import pytz
# 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
# Set the desired time zone (e.g., 'US/Eastern')
desired_time_zone = 'US/Eastern'
# Continuously fetch and display real-time data
while True:
# 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])
# Convert the timestamp to the desired time zone
df.index = [datetime.now(pytz.timezone(desired_time_zone))]
df = df.dropna(axis=0)
# Display the real-time data
st.subheader("Real-Time Data:")
st.write(df)
# Add a delay to avoid exceeding API rate limits
st.experimental_sleep(60) # Sleep for 60 seconds (adjust as needed)
if __name__ == "__main__":
main()