Skip to content

How to Easily Deploy Streamlit App and Host on Cloud

Streamlit has emerged as a game-changer in the realm of data applications. It empowers developers to transform data scripts into interactive web apps swiftly and effortlessly. However, the journey doesn't end at creating an app. The next crucial step is deployment, which makes your Streamlit app accessible to users worldwide, paving the way for interaction, feedback, and real-world application.

In this comprehensive guide, we'll delve into various platforms for deploying your Streamlit app, including Streamlit Community Cloud, Heroku, AWS, Google Cloud, and more. We'll walk you through each step, discuss common pitfalls, and provide solutions to ensure a smooth deployment process. So, let's get started!

Have you heard of this awesome Data Analysis & Data Visualisation tool, that turns your Streamlit App into Tableau?

PyGWalker (opens in a new tab) is a Python Library that helps you easily embed a tableau-alternative UI into your own Streamlit app effortlessly. Check out this amazing video how to explore data with pygwalker (opens in a new tab) demonstrating the detailed steps for empowering your Streamlit app with this powerful Data Visualization Python Library!


Part 1: Deploying on DigitalOcean with Docker

DigitalOcean, a popular cloud infrastructure provider, offers a platform to deploy and scale applications that run concurrently on multiple computers. Here, we'll use Docker, a platform that simplifies the creation, deployment, and running of applications using containers.

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications. It encapsulates applications into containers, which are standalone executable packages containing everything needed to run an application.

Here's a step-by-step guide on deploying your Streamlit app on DigitalOcean using Docker:

  1. Create a Dockerfile: A Dockerfile is a text document containing all the commands to assemble an image. Here's a basic example of a Dockerfile for a Streamlit app:
## Use an official Python runtime as a parent image
FROM python:3.7-slim

## Set the working directory in the container to /app
WORKDIR /app

## Add the current directory contents into the container at /app
ADD . /app

## Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

## Make port 80 available to the world outside this container
EXPOSE 80

## Run app.py when the container launches
CMD streamlit run --server.port 80 app.py
  1. Set up a droplet on DigitalOcean: A droplet is a scalable compute platform with add-on storage, security, and monitoring capabilities. You can customize the size, location, and initial software of your droplet.

  2. Deploy your app: Once your Dockerfile is ready and your droplet is running, you can deploy your Streamlit app. Here's a basic command to build your Docker image and run it:

docker build -t my-streamlit-app .
docker run -p 80:80 my-streamlit-app

Part 2: Streamlit Sharing

Streamlit Sharing is a platform that allows developers to deploy their Streamlit apps instantly with zero configuration. It's an excellent option for those who want to share their apps quickly and easily.

How to Use Streamlit Sharing

Using Streamlit Sharing is straightforward. Here's a step-by-step guide:

  1. Push your Streamlit app to a public GitHub repository: Streamlit Sharing deploys apps directly from GitHub, so your app needs to be in a public repository.

  2. Sign up for Streamlit Sharing:

Go to the Streamlit Sharing website and click on "Sign in with GitHub" to sign up.

  1. Deploy your app: Once you're signed in, click on "New app", select your GitHub repository, branch, and file path, and click on "Deploy". Your app will be live in just a few minutes!

Here's a sample of what your Streamlit Sharing dashboard might look like:

My Apps
- App 1 (Deployed)
- App 2 (Deployed)
- App 3 (In progress)
 
New app
- Repository: my-github-username/my-streamlit-app
- Branch: main
- File path: app.py

Part 3: Deploying as an Executable

While Streamlit does not officially support deploying apps as executable files for different operating systems, it's possible to do so with some workarounds. Here's a general guide on how you might approach this:

  1. Package your app: You can use tools like PyInstaller or cx_Freeze to package your Streamlit app into an executable file. These tools collect all the necessary files and libraries your app needs to run and bundle them into a single file.

  2. Create an installer: If you want to distribute your app to other users, you might want to create an installer. Tools like Inno Setup or NSIS can help you with this.

  3. Test your executable: Before distributing your app, make sure to test the executable on the target operating system to ensure it works correctly.

Here's a sample command to create an executable using PyInstaller:

pyinstaller --onefile --add-data='myapp:myapp' app.py

Part 4: Deploying on Heroku

Heroku is a cloud platform that lets you deploy, run, and manage applications written in various programming languages. Here's a step-by-step guide on how to deploy your Streamlit app on Heroku:

  1. Create a Heroku account: Sign up for a free Heroku account if you don't have one already.

  2. Install the Heroku CLI: The Heroku Command Line Interface (CLI) is a tool that allows you to create and manage your Heroku apps directly from the terminal.

  3. Prepare your app for Heroku: Heroku needs two additional files to deploy your Streamlit app: a Procfile and a setup.sh file. The Procfile tells Heroku what command to use to start your app, and the setup.sh file sets up your environment.

Here's what your Procfile and setup.sh might look like:

web: sh setup.sh && streamlit run app.py
mkdir -p ~/.streamlit/

echo "\
[general]\n\
email = \"your-email@domain.com\"\n\
" > ~/.streamlit/credentials.toml

echo "\
[server]\n\
headless = true\n\
enableCORS=false\n\
port = $PORT\n\
" > ~/.streamlit/config.toml
  1. Deploy your app: Once your app is prepared, you can deploy it to Heroku using the Heroku CLI. Here's a sample command sequence to deploy your app:
git init
heroku create
git add .
git commit -m "Initial commit"
git push heroku master

After these steps, your Streamlit app should be live on Heroku!

Part 5: Deploying on Google Cloud Platform

Google Cloud Platform (GCP) is a suite of cloud computing services that runs on the same infrastructure that Google uses internally for its end-user products. It provides a robust and scalable environment for deploying your Streamlit apps.

Deploying and Securing a Streamlit App on GCP

Here's a step-by-step guide on how to deploy your Streamlit app on GCP:

  1. Create a GCP account: If you don't have one already, sign up for a GCP account.

  2. Create a new project: Navigate to the GCP Console and create a new project.

  3. Enable the App Engine: Navigate to the "App Engine" section and enable it for your project.

  4. Prepare your app for GCP: Similar to Heroku, GCP requires a app.yaml file to configure your app. Here's a sample app.yaml file for a Streamlit app:

runtime: python39
instance_class: F2
automatic_scaling:
  target_cpu_utilization: 0.65
entrypoint: streamlit run --server.port $PORT --server.enableCORS false app.py
  1. Deploy your app: Once your app is prepared, you can deploy it to GCP using the Google Cloud SDK. Here's a sample command sequence to deploy your app:
gcloud app deploy

After these steps, your Streamlit app should be live on GCP!

Part 6: Building Streamlit Apps in Amazon SageMaker Studio

Amazon SageMaker Studio is a fully integrated development environment (IDE) for machine learning. It provides all the tools needed to build, train, and deploy machine learning models quickly.

Benefits of Using Amazon SageMaker Studio

  • Fully managed service: Amazon SageMaker Studio takes care of all the underlying infrastructure, allowing you to focus on building and deploying your Streamlit apps.

  • Integrated Jupyter notebooks: You can easily create Jupyter notebooks to explore and visualize your data.

  • Model building and training: SageMaker Studio provides built-in algorithms and support for custom algorithms, making it easy to build and train your machine learning models.

  • Model deployment: Once your model is trained, you can deploy it with a single click from the SageMaker Studio console.

Part 7: Deploying on Azure App Services

Azure App Services is a fully managed platform for building, deploying, and scaling web apps. It supports a wide range of programming languages, including Python, making it a great choice for deploying your Streamlit apps.

Deploying a Streamlit App on Azure App Services

Here's a step-by-step guide on how to deploy your Streamlit app on Azure App Services:

  1. Create an Azure account: If you don't have one already, sign up for an Azure account.

  2. Create a new App Service: Navigate to the Azure portal and create a new App Service.

  3. Configure your App Service: Choose your subscription, resource group, name, publish method (Code), runtime stack (Python), and operating system.

  4. Deploy your app: Once your App Service is set up, you can deploy your Streamlit app using the Azure CLI or Git. Here's a sample command sequence to deploy your app using the Azure CLI:

az webapp up --sku F1 --name my-streamlit-app

After these steps, your Streamlit app should be live on Azure App Services!

Part 8: Deploying Machine Learning Pipelines on AWS Fargate

AWS Fargate is a serverless compute engine for containers. It works with both Amazon Elastic Container Service (ECS) and Amazon

Elastic Kubernetes Service (EKS), making it easier to deploy containerized applications.

Deploying a PyCaret and Streamlit App on AWS Fargate

Here's a step-by-step guide on how to deploy a machine learning pipeline developed with PyCaret and a Streamlit app on AWS Fargate:

  1. Create a Dockerfile: Your Dockerfile should include the necessary instructions to install PyCaret, Streamlit, and any other required libraries. Here's a basic example:
## Use an official Python runtime as a parent image
FROM python:3.7-slim

## Set the working directory in the container to /app
WORKDIR /app

## Add the current directory contents into the container at /app
ADD . /app

## Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

## Make port 80 available to the world outside this container
EXPOSE 80

## Run app.py when the container launches
CMD streamlit run --server.port 80 app.py
  1. Build and push your Docker image: You can use the Docker CLI to build your Docker image and push it to a container registry like Amazon Elastic Container Registry (ECR).

  2. Create a new task definition in AWS Fargate: A task definition is a text file in JSON format that describes one or more containers that form your application. It includes the container image to use, the required CPU and memory, the network configuration, and more.

  3. Run your task: Once your task definition is set up, you can run your task in AWS Fargate. Your Streamlit app should now be accessible at the public IP address of your task.

Here's a sample command sequence to build your Docker image and push it to Amazon ECR:

$(aws ecr get-login --no-include-email --region region)
docker build -t my-streamlit-app .
docker tag my-streamlit-app:latest my-ecr-repo/my-streamlit-app:latest
docker push my-ecr-repo/my-streamlit-app:latest

After these steps, your PyCaret and Streamlit app should be live on AWS Fargate!

Part 9: Troubleshooting Deployment Errors

Deploying a Streamlit app can sometimes be a challenging task, especially when you encounter errors that are hard to decipher. However, understanding these common errors can help you troubleshoot and resolve them effectively.

Common Deployment Errors and Their Solutions

  1. ModuleNotFoundError: This error occurs when Python cannot find a module that your app needs to run. To resolve this, ensure that all required modules are listed in your requirements.txt file.
ModuleNotFoundError: No module named 'module-name'
  1. StreamlitAPIException: This error occurs when there's an issue with your Streamlit code. Check the error message for details and adjust your code accordingly.
StreamlitAPIException: 'function-name' is not a valid Streamlit command.
  1. Docker Build Failures: If your Docker build fails, it's often due to issues in the Dockerfile. Check your Dockerfile for any syntax errors or missing instructions.
ERROR: Service 'service-name' failed to build
  1. Deployment Timeouts: If your deployment is taking too long, it might time out. This can be due to a slow internet connection, a large Docker image, or a complex build process. Try optimizing your Dockerfile or increasing the timeout limit.
Error: Deployment timed out.

Remember, the key to troubleshooting is understanding the error message. Don't be intimidated by the technical jargon. Break it down, understand what it's saying, and you'll be able to find a solution.

Conclusion

Deploying a Streamlit app is a crucial step in the data application development process. It allows your app to be accessible to the world, enabling user interaction and real-world application. In this guide, we've explored various platforms for deploying your Streamlit app, including Streamlit Community Cloud, Heroku, AWS, Google Cloud, Azure, and more.

Each platform has its own strengths and weaknesses, and the best one for you depends on your specific needs and circumstances. We encourage you to explore these options, try them out, and find the one that best fits your needs. Happy deploying!

Have you heard of this awesome Data Analysis & Data Visualisation tool, that turns your Streamlit App into Tableau?

PyGWalker (opens in a new tab) is a Python Library that helps you easily embed a tableau-alternative UI into your own Streamlit app effortlessly.

PyGWalker for Data visualization in Streamlit (opens in a new tab)

FAQs

  1. How do I deploy my Streamlit app?

    Deploying a Streamlit app involves packaging your app and its dependencies, and then uploading it to a hosting platform. The exact steps depend on the platform you're using. This guide provides detailed instructions for several popular platforms.

  2. How do I host a Streamlit app locally?

    To host a Streamlit app locally, you simply need to run the command streamlit run your-app.py in your terminal. Make sure you're in the correct directory and have all the necessary dependencies installed.

  3. How long does it take to deploy a Streamlit app?

    The deployment time for a Streamlit app can vary depending on the size of your app, the speed of your internet connection, and the platform you're using. However, with a good setup, it can often be done in a matter of minutes.

  4. Is Streamlit better than Flask?

    Streamlit and Flask are both excellent tools, but they serve different purposes. Streamlit is designed for building data applications quickly and easily, while Flask is a more general web framework that gives you more control over your application. The best tool depends on your specific needs.