Deploying a Prophet Forecasting Model with Streamlit to Heroku | by Edward Krueger | Towards Data Science

By: Edward Krueger and Douglas Franklin.
Streamlit and Facebook Prophet give us the ability to create a clean forecasting dashboard with minimal effort and no cost. This makes for a solid proof of concept workflow.
In this project, we create a dashboard using Streamlit and Prophet to showcase data and make forecasts and then deploy our app to Heroku.
Here is a link to the GitHub repository with our code for the project. Note that we use Pipenv to manage our dependencies.
github.com
Streamlit is a free and open-source app framework that presents an easy way for data scientists and machine learning engineers to create great Python apps quickly. Streamlit’s opinioned nature makes building frontends easy. Lastly, Streamlit can make experimentation and production of data apps and machine learning analysis easier to do and to iterate on.
Check it out on GitHub.
github.com
Streamlit is excellent because it embraces Python scripting, treats widgets as variables, and caches data and computation for reuse. This allows us to have custom complex visualizations that only need to load once. Additionally, Streamlit can be used locally or run on the cloud.
Before you get started, you’re going to need Python 3.6 or later.
First, let’s install streamlit. If you have issues getting your machine set up, feel free to use our Pipfile from the project repository.
$ pip install streamlit
Now run the hello world app to make sure everything it’s working:
$ streamlit hello
This command opens a web server on your computer where you can play around with some demos! The first time you run this command, you may be prompted for an email that is added to ~/.streamlit/config.toml . It is not necessary to add your email to use to the software.
It’s worth noting that this email prompt will appear each time you run Streamlit without a complete config.toml file. To get around this, we use this bash script to input our email and the port we want to run on.
Once we run this setup.sh script, we have our email and port set.
Facebook Prophet or FB Prophet is, fittingly, a prediction tool. In lieu of predicting the end-times, we are going to forecast time series COVID data. FB Prophet uses pandas and takes in and outputs a Dataframe.
The Dataframes passed to FB prophet must have a “ds” and “y” column. We then feed a Dataframe containing only dates into
FB Prophet and the forecast fills the missing values.
We had some issues getting this set up locally on a windows 10 machine and within a docker container. However, we had no complications at all when using GCP AI Platform notebooks.
You will need to install pystanbefore installing fbprophet .
pip install pystan
Then you should be able to run:
pip install fbprophet
Note that you might get an error with FB Prophet that states it can’t be found. We ran into this error and the package was found. If you have issues with setup, this try a cloud VM or using our Pipfile!
Now that we have Streamlit and a basic understanding of what it does let’s get started with our forecasting app.
Pandas works well with Streamlit and is already a dependency for FB Prophet, so we are using it in the load_data.py file to make our lives a little easier.
All of our data is in the data.json file; we need to load it into Pandas and transform it into a Dataframe.
The rest of our app code is relatively simple and presents us with the dropdown options and select functions. This allows our app to get data and generate plots when a user selects an option from the dropdown.
Check out the app.py code here.
github.com
Streamlit lets us write apps like they are Python scripts. This means that when a new selection is picked in our dropdown, the entire app file runs again to generate the plot.
Let run our app and see the default view. Use the following line to run your streamlit app on local port 8000.
streamlit run app/app.py --server.port 8000
Our app is set up to load data and generate the ‘All — cumulative cases’ plot by default seen below.
Since our forecasting is live, it can take some time to generate plots after you select them from the dropdown. To reduce this loading time, we’ve added Streamlit cache to store results of the function, our plots.
Our app has a dropdown to select which plot you would like to view. When you choose an option that is not the default, FB prophet runs the forecast on demand. The resultant Dataframe is then passed to Streamlit, which displays our data.
What we cache with Streamlit doesn’t have to be data. We can cache anything, like our forecasts! Notice the alert in yellow “Running make_forecast(...)” appears only the first time an option is selected. Then when we reselect “Cumulative Recoveries,” there is no forecast; instead, our plot is pulled out of the cache.
This caching allows for smooth swapping between plots once they are forecasted.
To run our app on Heroku, we need to add a Procifle to our GitHub repository to serve our app on their platform. Here is our procfile.
web: sh setup.sh && streamlit run app/app.py
Notice that we are calling our setup.sh file to set our email and port on the Heroku machine. Then we tell streamlit to run our app. Just add this Procfile to your project root and you'll be able to run on Heroku.
Now let’s go to Heroku’s website and deploy our app from GitHub using the GUI.
To connect your repository, select GitHub, enter the name of the repository and hit search. Once your username and repository appear, click connect. Now select the desired branch and click deploy.
Build logs will begin to populate a console on the page. Notice that Heroku looks for a requirements.txt file first then installs dependencies from Pipenv’s Pipfile.lock. If you don’t use Pipenv, you will need to have a requiremnts.txt for this build to occur.
Once your environment has been built from the Pipfile.lock and the build is successful, you will see the below message.
The app is successfully deployed! Click to view button to see the deployed app on Heroku. Now we can view our app live on Heroku and run forecasts on their platform.
Check out our app here!
st-covid-forecast.herokuapp.com
We can enable automatic deployment to have changes to the Github master be displayed on Heroku as they are pushed. If you use this method, you’ll want to be sure that you always have a working master branch.
To review we discussed Streamlit and Facebook Prophet as tools that allow us to make forecasting dashboards with minimal effort and no cost. Streamlit is opinionated, easy to use, and allows us to have our front and backend code in a single file.
We used these tools to make a simple dashboard showing COVID data, what will you use them for?
Thank you for reading! Please let us know what tool you use for dashboards and what you are looking to learn more about.