Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,19 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def main():
|
| 10 |
-
st.title("Stock
|
| 11 |
|
| 12 |
# User input for stock symbol
|
| 13 |
symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")
|
|
@@ -16,12 +22,26 @@ def main():
|
|
| 16 |
st.warning("Please enter a valid stock symbol.")
|
| 17 |
st.stop()
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
st.write(stock_data)
|
| 25 |
|
| 26 |
if __name__ == "__main__":
|
| 27 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
import pandas as pd
|
| 4 |
+
from datetime import datetime
|
| 5 |
|
| 6 |
+
# Hard-coded API key for demonstration purposes
|
| 7 |
+
API_KEY = "QR8F9B7T6R2SWTAT"
|
| 8 |
+
|
| 9 |
+
def fetch_alpha_vantage_data(api_key, symbol):
|
| 10 |
+
url = f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={api_key}'
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
alpha_vantage_data = response.json()
|
| 13 |
+
return alpha_vantage_data
|
| 14 |
|
| 15 |
def main():
|
| 16 |
+
st.title("Real-Time Stock Data")
|
| 17 |
|
| 18 |
# User input for stock symbol
|
| 19 |
symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")
|
|
|
|
| 22 |
st.warning("Please enter a valid stock symbol.")
|
| 23 |
st.stop()
|
| 24 |
|
| 25 |
+
# Use the hard-coded API key
|
| 26 |
+
api_key = API_KEY
|
| 27 |
+
|
| 28 |
+
# Continuously fetch and display real-time data
|
| 29 |
+
while True:
|
| 30 |
+
# Fetch Alpha Vantage data
|
| 31 |
+
alpha_vantage_data = fetch_alpha_vantage_data(api_key, symbol)
|
| 32 |
+
|
| 33 |
+
# Extract relevant data from Alpha Vantage response
|
| 34 |
+
alpha_vantage_quote = alpha_vantage_data.get('Global Quote', {})
|
| 35 |
+
df = pd.DataFrame([alpha_vantage_quote])
|
| 36 |
+
df.index = [datetime.now()] # Use the current timestamp as the index
|
| 37 |
+
df = df.dropna(axis=0)
|
| 38 |
+
|
| 39 |
+
# Display the real-time data
|
| 40 |
+
st.subheader("Real-Time Data:")
|
| 41 |
+
st.write(df)
|
| 42 |
|
| 43 |
+
# Add a delay to avoid exceeding API rate limits
|
| 44 |
+
st.experimental_sleep(60) # Sleep for 60 seconds (adjust as needed)
|
|
|
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
main()
|