2 1 Deploying to Heroku Explained
Key Concepts
- Heroku: A cloud platform that lets you deploy, manage, and scale applications.
- Procfile: A file that specifies the commands that are executed by the app on startup.
- Heroku CLI: A command-line interface for managing Heroku applications.
- Git: A version control system used to deploy applications to Heroku.
- Environment Variables: Securely store sensitive information like API keys and passwords.
Heroku
Heroku is a cloud platform that allows you to deploy, manage, and scale applications. It supports multiple programming languages and provides a simple interface for deploying applications.
Procfile
A Procfile is a file that specifies the commands that are executed by the app on startup. For a Streamlit app, the Procfile typically contains a single line to start the Streamlit server.
web: streamlit run app.py
Heroku CLI
The Heroku CLI is a command-line interface for managing Heroku applications. It allows you to create, deploy, and manage your applications directly from the terminal.
heroku login heroku create git push heroku main
Git
Git is a version control system used to deploy applications to Heroku. You need to initialize a Git repository in your project directory and commit your code before deploying it to Heroku.
git init git add . git commit -m "Initial commit"
Environment Variables
Environment variables are used to securely store sensitive information like API keys and passwords. You can set environment variables using the Heroku CLI.
heroku config:set API_KEY=your_api_key
Examples
Deploying a Streamlit App to Heroku
# Step 1: Create a Procfile echo "web: streamlit run app.py" > Procfile # Step 2: Initialize a Git repository git init git add . git commit -m "Initial commit" # Step 3: Create a Heroku app heroku create # Step 4: Deploy the app git push heroku main # Step 5: Set environment variables heroku config:set API_KEY=your_api_key
Analogies
Think of Heroku as a hosting service that provides a space for your application to run. The Procfile is like a set of instructions that tell Heroku how to start your app. The Heroku CLI is like a remote control that allows you to manage your app from a distance. Git is like a time machine that helps you track changes and deploy your app to Heroku. Environment variables are like secret codes that keep your sensitive information safe.
By mastering the deployment of Streamlit apps to Heroku, you can make your applications accessible to users, ensure they run smoothly, and handle increased traffic efficiently.