Build a Streamlit app using 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. Keep the UI clean and responsive.
Drop files here
or click to upload
import streamlit as st
from yahoo_fin.stock_info import get_live_price
import time
# Set page title and config
st.set_page_config(page_title="Stock Price Viewer", layout="centered")
# Add custom CSS for styling
st.markdown("""
<style>
.big-font {
font-size:30px !important;
font-weight: bold;
}
</style>
""", unsafe_allow_html=True)
# App title
st.title("Real-Time Stock Price Viewer")
# Input field for stock ticker
ticker = st.text_input("Enter Stock Ticker Symbol:", value="").upper()
if st.button("Get Price") or ticker:
if ticker:
try:
# Show spinner while fetching data
with st.spinner(f'Fetching price for {ticker}...'):
# Get the live price
price = get_live_price(ticker)
# Display results
st.markdown("---")
st.markdown(f"<p class='big-font'>{ticker}</p>", unsafe_allow_html=True)
st.markdown(f"<p class='big-font'>${price:.2f}</p>", unsafe_allow_html=True)
# Add timestamp
st.text(f"Last updated: {time.strftime('%Y-%m-%d %H:%M:%S')}")
except Exception as e:
st.error(f"Error: Could not fetch price for '{ticker}'. Please check if the ticker symbol is correct.")
else:
st.warning("Please enter a ticker symbol")
# Add usage instructions
with st.expander("How to use"):
st.write("""
1. Enter a valid stock ticker symbol (e.g., AAPL for Apple, GOOGL for Google)
2. Click 'Get Price' or press Enter
3. The current stock price will be displayed below
Note: Prices are pulled in real-time from Yahoo Finance
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?