
To set up a Streamlit Azure Web App, you'll need to create a Streamlit app first. This can be done using the Streamlit CLI.
You can create a new Streamlit app by running the command `streamlit new my_app` in your terminal. This will create a new directory called `my_app` with a basic Streamlit app structure.
Next, you'll need to install the Streamlit Azure deployment package using pip. This can be done by running the command `pip install streamlit-azure-deployment`.
With the package installed, you can use the `streamlit azure` command to deploy your app to Azure. This command will create a new Azure Web App and deploy your Streamlit app to it.
Curious to learn more? Check out: Deploy to Azure Container Apps
Deploying to Azure
Deploying to Azure involves using a custom startup command, which can be provided post-deployment in the Azure Portal or as part of an Azure CLI command. This command is necessary because Azure App Services for Python is set up to use Gunicorn by default, but Streamlit applications use Tornado as their web server.
To configure the custom startup command, you'll need to create a requirements.txt file in the project root, which contains the dependencies required by your application. This file is essential for Azure to deploy your Streamlit app successfully.
You can include the custom startup command in your Azure Resource Manager (ARM) template, which is a JSON file used to deploy your application. The ARM template will also require you to specify parameters such as the Web App Name, Location, SKU, Runtime stack, Git Repo URL, and Git Repo branch.
Take a look at this: How to Deploy Flask App in Azure
Deploying to Azure with ARM
You can deploy your application to Azure using an Azure Resource Manager (ARM) template. This template is a JSON file that defines the infrastructure you need to deploy.
ARM templates are a form of infrastructure as code, which means you define the infrastructure that needs to be deployed. In our case, the ARM template contains a number of parameters, all of which have defaults defined. These include Web App Name, Location, SKU, Runtime stack, Git Repo URL, and Git Repo branch.
Related reading: Arm in Azure
The ARM template includes a Microsoft.Web/sites/config resource, with an appCommandLine property, which is our custom startup command. This command runs our Streamlit app and binds to 0.0.0.0 on port 8000.
To deploy the ARM template, you can use one-click deploy, which is directed to our ARM template. Alternatively, you can use the Azure CLI to deploy the ARM template.
Here's a summary of the parameters that can be defined in the ARM template:
Note that these parameters have defaults defined in the ARM template, so you can deploy the web app with the default values if you don't specify any custom values.
Generate SSL Certificate
Generating an SSL certificate is a crucial step in deploying your Streamlit app to Azure. You'll need to create a run.sh configuration file that will generate the SSL certificate and save it to your Azure File Share.
This configuration file should be run only once, and it will save your SSL certificate in your Azure File Share after generation.
If this caught your attention, see: Azure Application Configuration
Here's a breakdown of how the run.sh configuration works:
- Lines 2-3 start nginx.
- Line 4 starts your Streamlit app, which must be running on your FQDN to obtain a SSL certificate using certbot.
- Line 5 sleeps for 300 seconds, allowing Azure time to connect your Streamlit app to your FQDN.
- Line 6 uses certbot to create a SSL certificate for your domain name, replacing FQDN_NAME and AZURE_LOCATION placeholders.
- Line 7 zips up the SSL certificate and saves it on the volume mount.
- Line 8 runs an infinite loop, ensuring your Streamlit app continues running after the SSL certificate generation.
If you built the bash file from part one, you can simply enter the command and see a message from certbot saying the certificate has been successfully generated. If the certification fails, try a longer sleep time for line 5 of run.sh.
Deployment Steps
To deploy a Streamlit application to Azure, you'll need to use a custom startup command. This command should be provided post-deployment in the Azure Portal or as part of an Azure CLI command.
A requirements.txt file must be in the project root, so make sure it's in the root of your repository.
You can deploy your application using an Azure Resource Manager (ARM) template. This template is a JSON file that defines the infrastructure to be deployed.
To deploy your application directly from your public git repo, you'll need to include a Microsoft.Web/sites/config resource with an appCommandLine property in your ARM template. This property should contain the custom startup command to run your Streamlit app.
Suggestion: React Js Deploy Nginx in Azure Web App
Here are the parameters you'll need to define in your ARM template:
You can deploy your application using one-click deploy, which will direct you to a page where you can deploy your application and change the parameters to your needs. Alternatively, you can use the Azure CLI to deploy the ARM template.
Frequently Asked Questions
What webserver does Streamlit use?
Streamlit uses Tornado Web Server under the hood. This powerful web server enables Streamlit's interactive web applications to run smoothly and efficiently.
Is Streamlit a web app?
Streamlit is a framework for building web apps, specifically designed for machine learning and data science applications. It enables users to rapidly create and share interactive web apps with ease.
Sources
- https://waynegoosen.com/post/streamlit-deployment-guide-part-3-azure-infra/
- https://benalexkeen.com/deploying-streamlit-applications-with-azure-app-services/
- https://towardsdatascience.com/beginner-guide-to-streamlit-deployment-on-azure-part-2-cf14bb201b8e
- https://www.linkedin.com/posts/azure-daily_deploy-streamlit-on-azure-web-app-activity-7254381112028798977-48aO
- https://stackoverflow.com/questions/76701776/deploy-streamlit-app-with-machine-learning-model-on-azure-web-app/76703247
Featured Images: pexels.com