In this short step-by-step guide, you will learn how to build a financial dashboard in Streamlit using real stock data from yfinance. We’ll use:
- Streamlit
st.container()for layoutst.metric()for financial KPIsyfinancefor live market data
By the end, you’ll have a working stock market dashboard app with price, daily change, and charts.
Step 1 — Install Required Libraries
Install the required packages:
pip install streamlit yfinance pandas
Step 2 — Import Libraries
Create a new file called app.py and add:
import streamlit as st
import yfinance as yf
import pandas as pd
Step 3 — Add App Title and Layout Container
Use st.container() to structure your dashboard layout.
st.set_page_config(page_title="Financial Dashboard", layout="wide")
st.title("Financial Dashboard")
with st.container():
st.write("Track real-time stock data using Streamlit and yfinance.")
st.container() allows you to group and organize UI elements cleanly.
Step 4 — Add Stock Input Selector
Let users choose a stock ticker:
ticker = st.text_input("Enter Stock Ticker", value="AAPL")
Step 5 — Download Stock Data with yfinance
Fetch recent market data:
stock = yf.Ticker(ticker)
df = stock.history(period="1mo")
Step 6 — Display Key Financial Metrics with st.metric()
st.metric() is perfect for KPIs like:
- Current Price
- Daily Change
- Percentage Change
current_price = df["Close"].iloc[-1]
previous_close = df["Close"].iloc[-2]
change = current_price - previous_close
percent_change = (change / previous_close) * 100
col1, col2, col3 = st.columns(3)
col1.metric("Current Price", f"${current_price:.2f}")
col2.metric("Daily Change", f"${change:.2f}")
col3.metric("Percent Change", f"{percent_change:.2f}%")
This creates a clean KPI row for your financial dashboard.
Step 7 — Add Price Chart
st.subheader("Closing Price - Last 30 Days")
st.line_chart(df["Close"])
Full Working Example — Financial Dashboard in Streamlit
Here is the complete code:
import streamlit as st
import yfinance as yf
import pandas as pd
st.set_page_config(page_title="Financial Dashboard", layout="wide")
st.title("Financial Dashboard")
st.write("Track real-time stock data using Streamlit and yfinance.")
ticker = st.text_input("Enter Stock Ticker", value="AAPL")
stock = yf.Ticker(ticker)
df = stock.history(period="1mo")
if not df.empty:
current_price = df["Close"].iloc[-1]
previous_close = df["Close"].iloc[-2]
change = current_price - previous_close
percent_change = (change / previous_close) * 100
col1, col2, col3 = st.columns(3)
col1.metric("Current Price", f"${current_price:.2f}")
col2.metric("Daily Change", f"${change:.2f}")
col3.metric("Percent Change", f"{percent_change:.2f}%")
st.subheader("Closing Price - Last 30 Days")
st.line_chart(df["Close"])
else:
st.warning("No data found for this ticker.")
Step 8 — Run the App
Run the dashboard:
streamlit run app.py
Your financial dashboard will open in the browser.
What We Built
You created a:
- Real-time stock dashboard
- KPI metrics using
st.metric() - Organized layout with
st.container() - Interactive ticker search
- Live financial chart
This approach works for:
- Portfolio tracking
- Crypto dashboards
- Company financial summaries
- Trading tools