Streamlit

Deploy a Streamlit App

This quickstart 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.

You will need:

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

Deploy to Koyeb (opens in a new tab)

Consult the repository on GitHub (opens in a new tab) to view this example application.

Create the Streamlit app

Get started by creating a basic Streamlit app to deploy on Koyeb.

Alternatively, you can fork the repository on GitHub (opens in a new tab) to get a complete copy of the code. If you fork the repository, then you can skip to section on git-driven deployment on Koyeb.

Create a new directory for your project files and navigate to it:

mkdir example-streamlit
cd example-streamlit

In the example-streamlit directory, create a virtual environment for the new project using the following command:

python3 -m venv venv

For some operating systems, you might need to use the command with python3:

python3 -m venv venv

Virtual environments (opens in a new tab) provide isolation from the system environment allowing each virtual environment to have its own installation directories, dependencies, etc.

Activate and load your virtual environment:

. venv/bin/activate

Install Streamlit using pip:

pip install streamlit

Save your project dependencies to a requirements.txt file:

pip freeze > requirements.txt

Write 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 website and produce some visual effects.

Test the application using the streamlit run command:

streamlit run app.py

Navigate to http://localhost:8501 to see the application's web page.

To run the same application on a different port, use the --server.port option. You can use shell parameter expansion (opens in a new tab) to conditionally add this option if the PORT environment variable is set. For example, the following 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}

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.

This repository is used to version the application code and push changes to a GitHub repository. Run the following commands to commit and push changes to your GitHub repository, replacing the GitHub username and repository name with values from your account and the GitHub repo name:

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

Replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

Deploy to Koyeb using git-driven deployment

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

  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: https://github.com/koyeb/example-streamlit.
  4. Select Buildpack as the Builder for this repository.
  5. Click the Override toggle associated with the Run command field and enter the following:
    streamlit run app.py --server.port=$PORT
  6. Choose a name for your Service. For example example-streamlit.
  7. Click the Deploy button.

This creates a Koyeb App and Service which builds and deploys your application on Koyeb. You can access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Deploy to Koyeb using a pre-built container

As an alternative to git-driven deployment, you can deploy a pre-built Docker container from any public or private registry. This can be useful if your application needs specific system dependencies or you need more control over how the build is performed.

To dockerize the Streamlit application, create a new file called Dockerfile at the project root directory and add the following:

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.

To build and push the Docker image to a registry and deploy it on Koyeb, refer to the page on deploying pre-built container images.