Deploy a Streamlit app to Azure

Tracy Chen
4 min readJun 24, 2020

--

Image Credit

Looking ahead
What is Streamlit?
How to deploy your Streamlit app to Azure App Service?
How to redeploy your app?

What is Streamlit?

For the past few weeks, I’ve been playing around with the Streamlit framework and I am so HOOKED!

For those who are unfamiliar with the framework — Streamlit is an open source Python framework that is geared towards data scientists and ML engineers. Although I don’t fall into these buckets, I found the framework to be incredibly helpful as my past experience was largely in backend development.

With the framework, you can build web apps with beautifully rendered interactive charts using very few lines of Python code. The framework abstracts away all the difficulties of getting started with building your own web application. There is now no need to learn Django/Flask or any of the popular Python web frameworks. It is incredibly easy to get started really quickly.

For my first application, I used Streamlit to build a dashboard to examine the StackOverflow 2019 Developer survey results segmenting by student status.

As this article will mainly be focused on how I deployed my web app, I won’t be going through the specifics of how I built this application. However, here is a link to my Github repo. As you can see we are able to create a simple web app with only a few lines of code!

How to deploy to Azure App Service?

There are multiple ways to be go about this but I will share the experience that I found to be the easiest. We will be deploying to Azure App Service using a Docker linux container and Azure Container Registry (ACR).

Prerequisites

Step 1 — ACR

  • Create a container on ACR via Azure portal
  • Sign into Azure portal
  • From Azure Marketplace select Containers > Container Registry
  • Enter the required information
  • Select “Review + Create”
  • Once created, you should be redirected to a landing page with information pertaining to your container
  • On the “Overview” tab, there should be something called “Login Server” (e.g. specificregistryname.azurecr.io). Note down this value as you will need again when you push and pull images with Docker.
  • Login to registry
    - Sign in to Azure CLI by running the command ‘az login’ in your terminal ** Note that this command will not work if you do not have Azure CLI installed
    - Log into Azure ACR by running ‘az acr login — name <registry-name>’. ** Note do not include azurecr.io for the registry name
  • If you get stuck at any of these steps – take a look at this tutorial

Step 2 — Create a Requirements.txt file

  • Run the command ‘pip freeze > requirements.txt’ in your application terminal to autogenerate the requirements.txt file
  • This file contains all of your application’s dependencies

Step 3 — Create a Dockerfile

  • The Dockerfile contains the instructions to build the image. Here is my Dockerfile for reference:
FROM python:3.8
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 8501
COPY . /app
ENTRYPOINT ["streamlit", "run"]
CMD ["first_app.py"]
  • If you choose to use this file just remember to change the Python version and your application .py file (in my case I named it “first_app.py”)

Step 4 — Build Docker container locally

  • To build Docker container:
docker build –t mystapp:latest .
  • To view all your Docker images:
docker images
  • To run locally:
docker run -p 8501:8501 mystapp:latest

Step 5 — Push local image to ACR

  • Type in the following command:
docker login streamlittest.azurecr.io — username streamlittest

{Enter in any one of the two password values for the password — I found that it was easiest to copy and paste}

  • Tag your image:
docker tag mystapp:latest streamlittest.azurecr.io/myimage:v1
  • Push your image:
docker push streamlittest.azurecr.io/myimage:v1

Step 6— Create an Azure App Service web app

  • Go to Azure portal
  • Search for App Services (or it can appear as Web App)
  • Click the “Add” button
  • Fill out all the necessary fields
  • For Publish -> select “Docker Container”
  • On the “Docker” tab -> under Image Source -> select “Azure Container Registry” -> select your registry (which should appear on the dropdown list)
  • Select “Review + Create”

Awesome! Now you can see your app running on Azure by browsing to http://<app-name>.azurewebsites.net

Note: you may not immediately see your code running — Azure App Service takes a bit of time to load if this is the first deployment. However, if you try refreshing a few minutes later you should see your application up and running!

To redeploy your app, all you have to do is rebuild your Docker container locally and then follow the commands listed in Step 5 to push the image to ACR.

--

--

Tracy Chen
Tracy Chen

Written by Tracy Chen

A product person who still dabbles in code.

Responses (2)