Deploy a Streamlit Web App with Azure App Service | by Richard Peterson | Towards Data Science

Following the official Microsoft docs.
Streamlit is an open-source python library that is excellent for displaying charts and using widgets to interact with visual dashboards. With a simple language and intuitive controls, Streamlit helps you to quickly create web apps that can display text, dataframes, charts, maps, and more. The first step in deploying a Streamlit web app to the actual internet is to write your app.py script and launch locally to make sure it looks as expected.
You can deploy this docker container by cloning from my GitHub repo!
Launch your streamlit app by running streamlit run app.pyin a shell from the folder where the app.py file is saved. Whenever the app.py file is saved, the local web app will update automatically. Streamlit has an extensive graphing API that allows Seaborn/Matplotlib, Plotly and Altair charts. Combined with Streamlit’s checkboxes and other widgets, you can build an interactive plotting dashboard with zero knowledge of front end or back end web development. This make it an excellent tool for researchers and data scientists that want to quickly publish findings to the internet or create presentations that live in an accessible web app on the internet.
Streamlit’s Getting Started tutorial is the best way to learn the API, so I will jump straight to deployment of the Streamlit web app. For this example, I will publish an exploratory analysis of American 5-year survey data collected by the US Census Bureau. See below for my streamlit app file. The data is queried using the CensusData pip package.
When you have a web app that you are ready to publish, you will create a docker container hosted on the Azure Container Registry. This container will have everything the web app needs to function.
First, sign in to https://portal.azure.com/. Also, install the Azure CLI.
On your computer, open a shell in the folder where your app is saved. Log in to Azure by typing
az login
Next, create a Resource Group to house the Container Registry and the Web App Service that we will create in the following steps.
az group create -l eastus -n CensusApp
Create a Container Registry under the CensusApp resource group.
az acr create --name CensusAppRegistry --resource-group CensusApp --sku basic --admin-enabled true
For this app, there are 6 files that are necessary for this app to function. The census_app.py file is the streamlit app file. Censusimg.jpg is the file for the website top image. You will need to create a Dockerfile which gives instructions for the container build, a config.toml file for app settings, a credentials.toml file which prevents the Docker container from crashing on startup, and a requirements.txt file for python packages necessary for the app.py file.
You can visit my GitHub to view the full project.
FROM python:3.7-slimCOPY . /appWORKDIR /appRUN pip install -r requirements.txtEXPOSE 80RUN mkdir ~/.streamlitRUN cp config.toml ~/.streamlit/config.tomlRUN cp credentials.toml ~/.streamlit/credentials.tomlWORKDIR /appENTRYPOINT ["streamlit", "run"]CMD ["census_app.py"]
I recommend copying MarcSkovMadsen’s config.toml file from his awesome-streamlit repository.
requirements.txt
streamlitpandasnumpyseabornmatplotlibpsycopg2-binary
From the shell at the folder where your files are saved, run:
az acr build --registry CensusAppRegistry --resource-group CensusApp --image census-app .
If the build was successful, you should see the image description with the sha256 and output similar to: Run ID: ca1 was successful after 2m36s. Double check to make sure the image is in your Container Registry by logging into the Azure portal and navigating to your resource group, click on your container registry, under the Services Blade, click on Repositories.
Create an App Service Plan for your web app. This will determine the size of the virtual machine that will run your docker image. Here I have selected a sku of B1 — the first tier above free. At the time this article was written, a B1 service plan is 12.41/month. Create the app service plan from within the portal or by running:
az appservice plan create -g CensusApp -n CensusAppServicePlan -l eastus --is-linux --sku B1
Create the Azure web app from the docker container in the Container Registry by running the below command, or from the portal.
az webapp create -g CensusApp -p CensusAppServicePlan -n census-web-app -i censusappregistry.azurecr.io/census-app:latest
To view your new website, go to your new web app resource and select Browse. The first time you view it, there will be a cold-start delay and will likely become unresponsive after about 5 minutes. Go back to the resource and click browse again, and your web app should appear in your browser!
You should now see 3 resources in your Resource Group.
The beauty of this method of web app delivery is that you can redeploy your app by re-building your container.
Simply make changes to your streamlit app.py file and rebuild your container by running the command we ran earlier to build the initial image in the Container Registry.
az acr build --registry CensusAppRegistry --resource-group CensusApp --image census-app .
The link to your new web app will show in the browser address bar. Just look at that beautiful app! Now that you have the deployment down pat, you can revisit the app and make changes and add functionality. Great job!
Please visit my GitHub for the files used to build this containerized web app!
Thank you for reading!
** EDIT: Changed the SQL request to an actual CensusData API request!