6 min read

Flask App Deployment on Heroku: A Beginner’s Guide with Free Tier Setup

Flask App Heroku Deployment

When you’re ready to launch your Flask app, you may question where to host it. With so many hosting options available, why should you select Heroku? 

Heroku is a very user-friendly platform for web projects. It’s perfect for developers who want to deploy Flask quickly. Heroku makes it easy to deploy your Flask project, which is very useful for new developers.

One benefit of using Heroku is its free tier. This is perfect for small projects and experimentation. Flask app Heroku free hosting is a great option. It’s ideal for personal apps, learning Flask, or side projects. Heroku’s free tier simplifies setups. You can focus on coding and app development.

This article will help you deploy your Flask project to Heroku for free.

What You Need Before Deploying Flask App Heroku

Before starting with Flask deployment on Heroku, you should do a few things first. Ensure you have the following:

  • A Working Flask App: You require a simple Flask app ready for deployment. If you don’t already have one, you can easily make one by following these instructions.
  • Basic Understanding of Python and Flask: This article is for those who know the basics of Flask. If you’re new to Flask, start with some beginning lessons.
  • Git Installed: Git is required for version control, and you will use it to deploy your work to Heroku. If you haven’t installed Git yet, go to Git’s website and download it.
  • A Heroku Account: Sign up for a free Heroku account. To create one, go to the Heroku website.

Other tools you’ll need:

  • Python 3: Flask is based on Python; therefore, make sure Python 3 is installed on your machine.
  • Heroku CLI: You may manage your Heroku apps using the Heroku Command Line Interface (CLI). Download it from Heroku’s website.

Set Up Your Flask Project for Deployment

Now that you’ve gathered everything, let’s prepare your Flask app for deployment. Here’s what you’ll need to do:

  1. Create a Folder for Your Flask App: First, create a new folder for your project. Name it something like flask-deployment or any other name you prefer. This folder will contain all your Flask files.

Install Flask: If you haven’t installed Flask, install it using the Python package manager pip. Open your terminal and type the following:

bash

pip install flask
  1. Create the Flask App: Next, create a new Python file inside your folder and name it app.py. Here’s a simple Flask app that returns “Hello, World!” when accessed:
python

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run(debug=True)

Create the requirements.txt File: Heroku needs a list of your app’s dependencies. Run the following command to generate the requirements.txt file:

bash

pip freeze > requirements.txt
  1. This file will contain Flask as a dependency.

Create the Procfile: The Procfile tells Heroku how to run your app. Create a file named Procfile (without an extension) in your project folder. Add the following line:

makefile

web: gunicorn app:app
  1. This tells Heroku to use Gunicorn to run your Flask app in production.

Initialize Git and Prepare for Heroku

Once your Flask app is set up, it’s time to push it to Heroku using Git. Here’s how to do it:

Initialize Git: Open your terminal in the project folder and run:

bash

git init
  1. This initializes a new Git repository for your app.

Add Your Files to Git: Now, add all of your files to Git by typing:

bash

git add.
  1. Make Your First Commit: Commit your files to Git with the following command:
bash

git commit -m "Initial commit"
  1. Create a Heroku App: To create a new app on Heroku, run this command in your terminal:
heroku create your-app-name
  1. Replace your-app-name with a unique name for your app. This command will set up your Heroku app and connect it to your Git repository.

Deploy Flask App to Heroku

With everything set up, it’s time to deploy your Flask app on Heroku. Follow these steps:

Push Your Code to Heroku: In the terminal, run this command to push your code to Heroku:

git push heroku master
  1. Heroku will automatically detect that you have a Flask app and will install the necessary dependencies.
  2. Wait for Heroku to Build Your App: Heroku will take a few minutes to build your app. You’ll see a lot of logs as Heroku installs dependencies and sets up the server. Once it’s done, you’ll see a success message.

Open Your App: After the deployment is complete, you can open your app in a browser using:

heroku open
  1. Your Flask app is now live on Heroku! This is a quick and simple way to deploy Flask on Heroku.

Tip: If you encounter issues, check the logs with:

heroku logs --tail

Free Tier Limits and What to Expect

While the free tier on Heroku is great for small projects, there are some limitations:

  • Your app will sleep after 30 minutes of inactivity: If it isn’t accessed for 30 minutes, it will sleep. The first user to visit the app after it sleeps will experience a delay.
  • Limited usage: Heroku’s free tier offers 550 hours per month. If you exceed this limit, your app will be paused until the next month, unless you upgrade.
  • No persistent storage: Any data stored within your app will be lost if the app is restarted.

These limitations are fine for small personal projects, but if your app grows, you might need to upgrade to a paid tier.

Bonus: Add a Static Homepage or HTML Template

You can easily add a static homepage if you want to make your Flask app more interactive. Here’s how:

  1. Create the templates Folder: Inside your project folder, create a template folder.

Add an HTML Template: In the templates folder, create an index.html file with this code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask App</title>
</head>
<body>
    <h1>Welcome to my Flask app!</h1>
    <p>This app is deployed on Heroku.</p>
</body>
</html>
  1. Update app.py to Render the Template: Modify your app.py to render the template instead of returning “Hello, World!”:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
    return render_template("index.html")
if __name__ == "__main__":
    app.run(debug=True)
  1. Deploy Changes to Heroku: Add, commit, and push the changes to Heroku:
git add .
git commit -m "Added HTML template"
git push heroku master
  1. When you visit your app, you’ll see the new static homepage!

Conclusion: You’re Live on Heroku!

Congratulations! You’ve successfully deployed your Flask app on Heroku using the Heroku free tier. This process makes deploying a Flask app on Heroku simple and efficient, allowing you to get your app online with minimal hassle. Whether experimenting, learning, or building a personal project, Flask app Heroku’s free hosting offers the perfect environment.

If you want more advanced Flask deployment tutorials, try scaling your app and adding more features.

Previous Post
From Lists to Trees: Demystifying Data Structures in Python
Next Post
NodeJS App Deployment on DigitalOcean with PM2 and Nginx

Deploy Django, Flask, and FastAPI in seconds

Seenode simplifies the deployment of your apps with a single, scalable, easy-to-use platform.

Share

Flask App Deployment on Heroku: A Beginner’s Guide with Free Tier Setup

Flask App Heroku Deployment

When you’re ready to launch your Flask app, you may question where to host it. With so many hosting options available, why should you select Heroku? 

Heroku is a very user-friendly platform for web projects. It’s perfect for developers who want to deploy Flask quickly. Heroku makes it easy to deploy your Flask project, which is very useful for new developers.

One benefit of using Heroku is its free tier. This is perfect for small projects and experimentation. Flask app Heroku free hosting is a great option. It’s ideal for personal apps, learning Flask, or side projects. Heroku’s free tier simplifies setups. You can focus on coding and app development.

This article will help you deploy your Flask project to Heroku for free.

What You Need Before Deploying Flask App Heroku

Before starting with Flask deployment on Heroku, you should do a few things first. Ensure you have the following:

  • A Working Flask App: You require a simple Flask app ready for deployment. If you don’t already have one, you can easily make one by following these instructions.
  • Basic Understanding of Python and Flask: This article is for those who know the basics of Flask. If you’re new to Flask, start with some beginning lessons.
  • Git Installed: Git is required for version control, and you will use it to deploy your work to Heroku. If you haven’t installed Git yet, go to Git’s website and download it.
  • A Heroku Account: Sign up for a free Heroku account. To create one, go to the Heroku website.

Other tools you’ll need:

  • Python 3: Flask is based on Python; therefore, make sure Python 3 is installed on your machine.
  • Heroku CLI: You may manage your Heroku apps using the Heroku Command Line Interface (CLI). Download it from Heroku’s website.

Set Up Your Flask Project for Deployment

Now that you’ve gathered everything, let’s prepare your Flask app for deployment. Here’s what you’ll need to do:

  1. Create a Folder for Your Flask App: First, create a new folder for your project. Name it something like flask-deployment or any other name you prefer. This folder will contain all your Flask files.

Install Flask: If you haven’t installed Flask, install it using the Python package manager pip. Open your terminal and type the following:

bash

pip install flask
  1. Create the Flask App: Next, create a new Python file inside your folder and name it app.py. Here’s a simple Flask app that returns “Hello, World!” when accessed:
python

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run(debug=True)

Create the requirements.txt File: Heroku needs a list of your app’s dependencies. Run the following command to generate the requirements.txt file:

bash

pip freeze > requirements.txt
  1. This file will contain Flask as a dependency.

Create the Procfile: The Procfile tells Heroku how to run your app. Create a file named Procfile (without an extension) in your project folder. Add the following line:

makefile

web: gunicorn app:app
  1. This tells Heroku to use Gunicorn to run your Flask app in production.

Initialize Git and Prepare for Heroku

Once your Flask app is set up, it’s time to push it to Heroku using Git. Here’s how to do it:

Initialize Git: Open your terminal in the project folder and run:

bash

git init
  1. This initializes a new Git repository for your app.

Add Your Files to Git: Now, add all of your files to Git by typing:

bash

git add.
  1. Make Your First Commit: Commit your files to Git with the following command:
bash

git commit -m "Initial commit"
  1. Create a Heroku App: To create a new app on Heroku, run this command in your terminal:
heroku create your-app-name
  1. Replace your-app-name with a unique name for your app. This command will set up your Heroku app and connect it to your Git repository.

Deploy Flask App to Heroku

With everything set up, it’s time to deploy your Flask app on Heroku. Follow these steps:

Push Your Code to Heroku: In the terminal, run this command to push your code to Heroku:

git push heroku master
  1. Heroku will automatically detect that you have a Flask app and will install the necessary dependencies.
  2. Wait for Heroku to Build Your App: Heroku will take a few minutes to build your app. You’ll see a lot of logs as Heroku installs dependencies and sets up the server. Once it’s done, you’ll see a success message.

Open Your App: After the deployment is complete, you can open your app in a browser using:

heroku open
  1. Your Flask app is now live on Heroku! This is a quick and simple way to deploy Flask on Heroku.

Tip: If you encounter issues, check the logs with:

heroku logs --tail

Free Tier Limits and What to Expect

While the free tier on Heroku is great for small projects, there are some limitations:

  • Your app will sleep after 30 minutes of inactivity: If it isn’t accessed for 30 minutes, it will sleep. The first user to visit the app after it sleeps will experience a delay.
  • Limited usage: Heroku’s free tier offers 550 hours per month. If you exceed this limit, your app will be paused until the next month, unless you upgrade.
  • No persistent storage: Any data stored within your app will be lost if the app is restarted.

These limitations are fine for small personal projects, but if your app grows, you might need to upgrade to a paid tier.

Bonus: Add a Static Homepage or HTML Template

You can easily add a static homepage if you want to make your Flask app more interactive. Here’s how:

  1. Create the templates Folder: Inside your project folder, create a template folder.

Add an HTML Template: In the templates folder, create an index.html file with this code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask App</title>
</head>
<body>
    <h1>Welcome to my Flask app!</h1>
    <p>This app is deployed on Heroku.</p>
</body>
</html>
  1. Update app.py to Render the Template: Modify your app.py to render the template instead of returning “Hello, World!”:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
    return render_template("index.html")
if __name__ == "__main__":
    app.run(debug=True)
  1. Deploy Changes to Heroku: Add, commit, and push the changes to Heroku:
git add .
git commit -m "Added HTML template"
git push heroku master
  1. When you visit your app, you’ll see the new static homepage!

Conclusion: You’re Live on Heroku!

Congratulations! You’ve successfully deployed your Flask app on Heroku using the Heroku free tier. This process makes deploying a Flask app on Heroku simple and efficient, allowing you to get your app online with minimal hassle. Whether experimenting, learning, or building a personal project, Flask app Heroku’s free hosting offers the perfect environment.

If you want more advanced Flask deployment tutorials, try scaling your app and adding more features.

Previous Post
From Lists to Trees: Demystifying Data Structures in Python
Next Post
NodeJS App Deployment on DigitalOcean with PM2 and Nginx

Share