Use yahoo_fin to display the current stock price. The app should have a text input field where users can enter a stock ticker, a submit button to fetch the real-time price, and a display area showing the stock symbol and its latest price. The app should handle invalid tickers gracefully and update dynamically when a new ticker is entered.
Drop files here
or click to upload
import streamlit as st
from yahoo_fin import stock_info
import time
st.title("Real-Time Stock Price Checker")
# Add a text input for the stock ticker
ticker = st.text_input("Enter Stock Ticker Symbol:", value="", help="Example: AAPL for Apple Inc.")
if ticker:
ticker = ticker.upper()
try:
# Add a spinner while fetching the data
with st.spinner(f'Fetching price for {ticker}...'):
# Get the current price
price = stock_info.get_live_price(ticker)
# Create two columns for better layout
col1, col2 = st.columns(2)
with col1:
st.metric(
label="Stock Symbol",
value=ticker
)
with col2:
st.metric(
label="Current Price",
value=f"${price:.2f}"
)
# Add a small note about price refresh
st.caption("Price updates when you enter a new ticker or refresh the page.")
except Exception as e:
st.error(f"Error: Could not fetch price for ticker '{ticker}'. Please make sure you entered a valid stock symbol.")
else:
st.info("👆 Enter a stock ticker symbol above to get started!")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?