Streamlit

Deploy a Streamlit App

This guide explains how to deploy a basic Streamlit (opens in a new tab) application to Koyeb using:

  1. Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
  2. Pre-built containers you can deploy from any public or private registry.

To successfully follow this documentation, you will need to have a Koyeb account (opens in a new tab). You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.

You can deploy and preview the Streamlit application from this guide by clicking the Deploy to Koyeb button below.

Deploy to Koyeb (opens in a new tab)

You can consult the repository on GitHub (opens in a new tab) to find out more about the example application that this guide uses.

Create the Streamlit app

We will start by creating a basic Streamlit application. You will need a recent version of Python (opens in a new tab) as well as pip, the Python packaging tool, installed on your local machine. Additionally, you may wish to install Docker (opens in a new tab) if you are planning on building and testing a container image for the application locally.

Create a new project

To get started, create a new directory for your project files and navigate into it:

mkdir example-streamlit
cd example-streamlit

Inside the new directory, create a virtual environment for the new project and activate it by typing:

python3 -m venv venv
source venv/bin/activate

Your prompt will likely change to reflect that the virtual environment is now active.

Install the package

Now we can install Streamlit by typing:

pip install streamlit

Save your project dependencies to a requirements.txt file by typing:

pip freeze > requirements.txt

Create a basic Streamlit application

Now that the dependencies are installed, you can create a basic Streamlit application.

Create a file called app.py with the following contents:

app.py
import streamlit as st
import time
 
 
st.title("Streamlit, Deployed on Koyeb")
st.subheader("Let's celebrate!")
 
progress_text = "Inflating the balloons..."
my_progress = st.progress(0, text=progress_text)
 
for percent in range(100):
    time.sleep(0.05)
    my_progress.progress(percent + 1, text=progress_text)
 
my_progress.empty()
st.balloons()
st.button("Re-run")

Test the application

This application uses Streamlit to render a basic web site and produce some visual effects after a few moments.

Test the application by typing:

streamlit run app.py

If your browser does not automatically open a new page, navigate to http://localhost:8501 to see the application's web page.

To run the same application on a different port, we can use the --server.port option. You can use shell parameter expansion (opens in a new tab) to conditionally add this if the PORT environment variable is set. For example, this will run the application on port 8888, but fall back to port 8501 if the PORT variable is unset:

export PORT=8888
streamlit run app.py ${PORT:+--server.port $PORT}

Create a Dockerfile for the project (Optional)

We can build and run our Streamlit project on Koyeb using the native Python buildpack, but we can also optionally build from a Dockerfile for more control. To make it possible to build a container image for our application, we just need to create the Dockerfile. We'll also define a .dockerignore file to tell the builder what files to skip when creating the image.

First, define a .dockerignore file in your main project directory. Inside, paste the following contents:

.dockerignore
.git
.gitignore
Dockerfile
.dockerignore
venv

This file tells Docker to not include Git files, the Docker files themselves, or the Python virtual environment. This helps ensure that the image we build is not bloated and that the build completes faster.

Next, create a new file called Dockerfile within the main project directory. Inside, paste the following contents:

Dockerfile
FROM python:slim
 
WORKDIR /app
COPY . .
 
RUN pip install -r requirements.txt
 
CMD ["sh", "-c", "streamlit run app.py ${PORT:+--server.port=$PORT} --server.address=0.0.0.0"]

This Dockerfile is based on the slim version of the python image (opens in a new tab). The Dockerfile copies all of the project files to the image's filesystem and then installs the dependencies with pip. It is configured to allow optionally passing PORT as an environment variable.

If you have Docker installed locally, you can build and test the image on your computer and optionally upload it to a registry. You can deploy container images from any container registry to Koyeb.

We can also build the Dockerfile directly from the repository when we deploy, which is useful as a way of automatically deploying when changes occur. We will demonstrate this method as one of the options in this guide.

Push the project to GitHub

In the project directory, initialize a new git repository by running the following command:

git init

Create a new GitHub repository (opens in a new tab) for your Streamlit project if you haven't already done so.

Pull down GitHub's default .gitignore file for Python to help avoid accidentally committing unnecessary and unwanted files:

curl -L https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore -o .gitignore

When you are ready, run the following commands to commit and push changes to the repository. Remember to replace the values of <YOUR_GITHUB_USERNAME> and <YOUR_REPOSITORY_NAME> with your own information:

git add :/
git commit -m "Initial commit"
git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git
git branch -M main
git push -u origin main

Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

Deploy to Koyeb using git-driven deployment

Once the repository is pushed to GitHub, you can deploy the Streamlit application to Koyeb. Any changes in the deployed branch of your codebase will automatically trigger a redeploy on Koyeb, ensuring that your application is always up-to-date.

To deploy the Streamlit app on Koyeb using the control panel (opens in a new tab), follow the steps below:

  1. Click Create Service on the Overview tab of the Koyeb control panel and choose Web service.
  2. Select GitHub as the deployment option.
  3. Choose the GitHub repository containing your application code. Alternatively, you can enter our public Streamlit example repository (opens in a new tab) into the Public GitHub repository at the bottom of the page: https://github.com/koyeb/example-streamlit.
  4. Select your preferred Instance size and region.
  5. Choose the Builder for your project. We can use either a Dockerfile or buildpack for this repository.
  6. If you are using the Buildpack builder, click the Override toggle associated with the Run command field and enter streamlit run app.py --server.port=$PORT.
  7. Choose a name for your Service, for example example-streamlit.
  8. Click the Deploy button.

A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Deploy to Koyeb from a container registry

If you chose to build a container image for the Streamlit application, you can optionally deploy the application from a container registry instead of from GitHub.

To deploy a pre-built Streamlit container image on Koyeb using the control panel (opens in a new tab), follow the steps below:

  1. Click Create Service on the Overview tab of the Koyeb control panel and then select Web service.
  2. Select Docker as the deployment option.
  3. Choose the container image and tag from your registry and click Next to continue.
  4. Choose a name for your Service, for example example-streamlit.
  5. Click the Deploy button.

A Koyeb App and Service will be created. The container image will be pulled from the container registry and a container will be deployed from it. Once the initialization is complete, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.