Introduction
You've built an amazing Streamlit app locally, and now it's time to share it with the world. But how do you go from running your app on your computer to making it accessible online? In this guide, we'll explore different methods for deploying Streamlit apps on the web, from free and easy options to more advanced setups.
1. Streamlit Sharing: The Easiest Option
Streamlit offers a free hosting service called Streamlit Sharing, which is perfect for small projects and quick deployments.
How to use Streamlit Sharing:
- Push your Streamlit app to a public GitHub repository.
- Sign up for Streamlit Sharing at share.streamlit.io.
- Connect your GitHub account and select the repository containing your app.
- Choose the main file (usually
app.py
) and click "Deploy".
Your app will be live in minutes! It's that simple.
# Example app.py import streamlit as st st.title("My First Deployed Streamlit App") st.write("Hello, World!")
2. Heroku: Free Tier with More Control
Heroku is a popular platform-as-a-service (PaaS) that offers a free tier suitable for deploying Streamlit apps.
Steps to deploy on Heroku:
- Create a
requirements.txt
file listing your app's dependencies:
streamlit
pandas
numpy
- Create a
Procfile
(no extension) with the following content:
web: sh setup.sh && streamlit run app.py
- Create a
setup.sh
file:
mkdir -p ~/.streamlit/ echo "\ [server]\n\ headless = true\n\ port = $PORT\n\ enableCORS = false\n\ \n\ " > ~/.streamlit/config.toml
- Install the Heroku CLI and deploy:
heroku create your-app-name git push heroku main
Your app will be live at https://your-app-name.herokuapp.com
.
3. AWS Elastic Beanstalk: Scalable and Robust
For more complex apps or those requiring additional services, AWS Elastic Beanstalk is a great option.
Deploying to AWS Elastic Beanstalk:
- Create a
Dockerfile
:
FROM python:3.8-slim WORKDIR /app COPY requirements.txt ./requirements.txt RUN pip3 install -r requirements.txt COPY . . EXPOSE 8501 ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
-
Create an
Elastic Beanstalk
application and environment using the AWS Management Console. -
Deploy your app using the AWS CLI:
eb init -p docker your-app-name eb create your-environment-name eb deploy
4. Google Cloud Run: Serverless and Cost-Effective
Google Cloud Run is a serverless platform that's great for Streamlit apps with varying traffic.
Deploying to Google Cloud Run:
- Build your Docker image:
docker build -t gcr.io/your-project-id/streamlit-app:v1 .
- Push the image to Google Container Registry:
docker push gcr.io/your-project-id/streamlit-app:v1
- Deploy to Cloud Run:
gcloud run deploy --image gcr.io/your-project-id/streamlit-app:v1 --platform managed
Best Practices for Deploying Streamlit Apps
- Use environment variables: Store sensitive information like API keys in environment variables instead of hard-coding them in your app.
import os import streamlit as st api_key = os.environ.get("API_KEY") st.write(f"Using API key: {api_key}")
-
Optimize for performance: Minimize the use of
st.cache
for frequently changing data, and consider using async operations for I/O-bound tasks. -
Handle errors gracefully: Use try-except blocks to catch and display errors in a user-friendly manner.
try: result = potentially_error_prone_function() st.success("Operation completed successfully!") except Exception as e: st.error(f"An error occurred: {str(e)}")
-
Version your dependencies: Use a
requirements.txt
file with specific versions to ensure consistency across different environments. -
Test thoroughly: Before deploying, test your app with different inputs and edge cases to ensure it behaves as expected.
By following these deployment methods and best practices, you'll be well on your way to sharing your Streamlit apps with the world. Each deployment option has its pros and cons, so choose the one that best fits your project's needs and your technical expertise. Happy deploying!