Deploy Streamlit on Azure Web App

This post has been republished via RSS; it originally appeared at: Microsoft Tech Community - Latest Blogs - .

Streamlit is an open-source app framework that enables the creation of data-driven web applications with minimal coding. It's particularly popular for machine learning and AI-related projects, as it allows developers to easily create interactive dashboards directly from Python scripts.

 

This tutorial will guide you through the process of deploying a Streamlit application locally and on an Azure Web App, including considerations for memory usage during deployment.

 

TOC:

  • Hosting in Development Environment
  • Hosting in Azure Web App
  • Module and Memory Usage
  • Referencs

Hosting in Development Environment

To run a Streamlit application in your local development environment, follow these steps:

STEP 1:

Use VSCode to open an empty folder. Start a terminal and input the following commands to create a Python virtual environment and switch the current session to this environment.

theringe_0-1729576369858.png

Windows

 

python -m venv .venv
.\.venv\Scripts\Activate.ps1

 

Linux

 

python -m venv .venv
source .venv/Scripts/activate

 

 

 

STEP 2:

Enter the pip command to install streamlit and create a file named "app.py". Run another command to start the local server hosting the project. You will then be able to visit the project page in your browser at http://127.0.0.1:8501.

 

# Install Streamlit
pip install streamlit
# Launch server after app.py has been created
python -m streamlit run app.py

 

theringe_1-1729576675213.png

 

Hosting in Azure Web App

To deploy your Streamlit application on Azure Web App, follow these general steps:

STEP 1:

Create a Linux Python Web App on Azure.

theringe_2-1729576741593.png

 

STEP 2:

Using VSCode, add two files, "streamlit.sh" and ".deployment", to the root directory of your project.

theringe_3-1729576884470.png

 

streamlit.sh

 

pip install streamlit
python -m streamlit run app.py --server.port 8000 --server.address 0.0.0.0

 

.deployment

 

[config]
SCM_DO_BUILD_DURING_DEPLOYMENT=false

 

 

STEP 3:

Deploy the root directory of the project to the Python app you just created using VSCode.

theringe_4-1729576910432.png

 

STEP 4:

On Azure, find the Python app and modify the startup command as follows, then restart the app.

theringe_5-1729576975091.png

 

bash /home/site/wwwroot/streamlit.sh

 

theringe_6-1729577014659.png

 

STEP 5:

Check if your project is running correctly.

theringe_7-1729577027613.png

 

Module and Memory Usage

Although Streamlit itself can be installed via a simple pip command (pip install streamlit), it has several dependencies related to AI and machine learning libraries. These dependencies may consume a significant amount of memory, especially during the deployment process and when running your Streamlit application.

 

If your application process is unexpectedly terminated, or you encounter an Exit Code 137, this usually indicates that your Web App has run out of available memory. In such cases, consider upgrading to a higher SKU with more memory to ensure smooth deployment and operation.

theringe_10-1729577178248.png

 

Here's an example:

theringe_8-1729577102189.png

theringe_9-1729577124744.png

 

References

Azure Linux Web App and http server - Microsoft Community Hub

Deploy Mkdocs page on Azure Web App - Microsoft Community Hub

Install Streamlit using command line - Streamlit Docs

Troubleshoot Python function apps in Azure Functions | Microsoft Learn

python - Azure Function Exit code: 137 | Please review your requirements.txt - Stack Overflow

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.