The power of Python is now available to web based projects via frameworks such as Flask and Streamlit. These projects allows the developer to bring the data wrangling and analytics capabilities of Python to the web. When it comes to the deployment and sharing of these projects, development and testing can be hindered by problems with setting up dependencies, requirements, network ports, and execution environments. To solve this, Docker was introduced. Docker is an open source product that offers hardware virtualization at the Operating System (OS) level. This guide will demonstrate how to use Docker to package and ship a Python-based Streamlit web app.
The guide assumes you have at least intermediate knowledge in Python and have a base level of understanding of Streamlit and Docker.
Consider the scenario where you would like to build a growth calculator that determines the worth of an investment given the growth rate, initial investment, and time in years.
This app would then be shared as part of your developer portfolio. It is paramount that interested parties will be able to download and run your project with ease. The web being the most common interface, you decide the project will be web based. Since it involves data analysis, it will be in Python. To give your recipients an easy time in setting up and running your app, you decide to package it in docker.
Copy the code below into a Python file and name it app.py
1import streamlit as st
2
3st.title("Hello Streamlit")
4st.header("Calculate % Growth")
5initial = st.number_input("Initial investment in USD")
6yr = st.number_input("Growth Period in years")
7growth = st.number_input("Growth Rate in %")
8terminal_value = 0
9current_val = initial
10for year in range(int(yr)):
11 current_val += growth * current_val
12 terminal_value = current_val
13
14# perform cashflow projections for the next 5 years
15st.write(f'Terminal value of {initial} after {yr} years at a growth rate of {growth} is {terminal_value}')
This project requires Python libraries to be installed for it to run.
The libraries will be recorded in a requirements.txt
file.
Copy the contents below into your requirements.txt
file.
1streamlit
For ease of handling during the Docker building, place both the Python and requirements files in a folder and name it src/
.
The dockerfile required for this project mainly has to achieve the following logical steps:
Copy the Docker commands below in a file and name it Dockerfile
1FROM python:3.8
2
3ENV MICRO_SERVICE=/home/app/webapp
4# set work directory
5RUN mkdir -p $MICRO_SERVICE
6# where your code lives
7WORKDIR $MICRO_SERVICE
8
9# set environment variables
10ENV PYTHONDONTWRITEBYTECODE 1
11ENV PYTHONUNBUFFERED 1
12
13# install dependencies
14RUN pip install --upgrade pip
15# copy project
16COPY src/ $MICRO_SERVICE
17RUN pip install -r requirements.txt
18EXPOSE 8501
19CMD streamlit run app.py
With both files set up, you are ready to build and run your image. To build your image, run the command
1docker build -t myimage .
After the docker image is built, it is time to run your Docker image.
1docker run -p 8501:8501 myimage
The app is now running at the localhost address provided by the Docker interface.
Stremlit page
Packaging and shipping a Python web app using Docker are vital skills for developers holding Python developer and Devops related positions. To further build on these skills, do further research on how to run multi-container apps on Docker using docker-compose.