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.
Streamlit offers a free hosting service called Streamlit Sharing, which is perfect for small projects and quick deployments.
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!")
Heroku is a popular platform-as-a-service (PaaS) that offers a free tier suitable for deploying Streamlit apps.
requirements.txt
file listing your app's dependencies:streamlit
pandas
numpy
Procfile
(no extension) with the following content:web: sh setup.sh && streamlit run app.py
setup.sh
file:mkdir -p ~/.streamlit/ echo "\ [server]\n\ headless = true\n\ port = $PORT\n\ enableCORS = false\n\ \n\ " > ~/.streamlit/config.toml
heroku create your-app-name git push heroku main
Your app will be live at https://your-app-name.herokuapp.com
.
For more complex apps or those requiring additional services, AWS Elastic Beanstalk is a great option.
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
Google Cloud Run is a serverless platform that's great for Streamlit apps with varying traffic.
docker build -t gcr.io/your-project-id/streamlit-app:v1 .
docker push gcr.io/your-project-id/streamlit-app:v1
gcloud run deploy --image gcr.io/your-project-id/streamlit-app:v1 --platform managed
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!
25/09/2024 | Python
22/11/2024 | Python
14/11/2024 | Python
06/12/2024 | Python
08/11/2024 | Python
22/11/2024 | Python
25/09/2024 | Python
15/11/2024 | Python
15/11/2024 | Python
22/11/2024 | Python
15/11/2024 | Python
05/10/2024 | Python