Skip to content

Build A Stock Prediction Web App In Python

In this tutorial we build a stock prediction web app in Python using streamlit, Yahoo finance, and Facebook Prophet.

In this tutorial we build a stock prediction web app in Python using streamlit, Yahoo finance, and Facebook Prophet.




We need to install streamlit, Facebook prophet, yfinance, and plotly. Run this in your terminal:

$ pip install streamlit fbprophet yfinance plotly

The Code

Thanks to streamlit it does not require a lot of code to implement a nice looking web app. This is the whole code:

import streamlit as st
from datetime import date

import yfinance as yf
from fbprophet import Prophet
from fbprophet.plot import plot_plotly
from plotly import graph_objs as go

START = "2015-01-01"
TODAY ="%Y-%m-%d")

st.title('Stock Forecast App')

stocks = ('GOOG', 'AAPL', 'MSFT', 'GME')
selected_stock = st.selectbox('Select dataset for prediction', stocks)

n_years = st.slider('Years of prediction:', 1, 4)
period = n_years * 365

def load_data(ticker):
    data =, START, TODAY)
    return data

data_load_state = st.text('Loading data...')
data = load_data(selected_stock)
data_load_state.text('Loading data... done!')

st.subheader('Raw data')

# Plot raw data
def plot_raw_data():
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="stock_open"))
    fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="stock_close"))
    fig.layout.update(title_text='Time Series data with Rangeslider', xaxis_rangeslider_visible=True)


# Predict forecast with Prophet.
df_train = data[['Date','Close']]
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})

m = Prophet()
future = m.make_future_dataframe(periods=period)
forecast = m.predict(future)

# Show and plot forecast
st.subheader('Forecast data')

st.write(f'Forecast plot for {n_years} years')
fig1 = plot_plotly(m, forecast)

st.write("Forecast components")
fig2 = m.plot_components(forecast)

Run the application

To start the app, run

$ streamlit run

where should be the name of your file in this case.

FREE VS Code / PyCharm Extensions I Use

鉁 Write cleaner code with Sourcery, instant refactoring suggestions: Link*

PySaaS: The Pure Python SaaS Starter Kit

馃殌 Build a software business faster with pure Python: Link*

* These are affiliate link. By clicking on it you will not have any additional costs. Instead, you will support my project. Thank you! 馃檹