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.
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
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.
STEP 2:
Using VSCode, add two files, "streamlit.sh" and ".deployment", to the root directory of your project.
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.
STEP 4:
On Azure, find the Python app and modify the startup command as follows, then restart the app.
bash /home/site/wwwroot/streamlit.sh
STEP 5:
Check if your project is running correctly.
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.
Here's an example:
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