Open In App

Deploy Python Flask App on Heroku

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Flask is a web application framework written in Python. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects. This article revolves around how to deploy a flask app on Heroku. To demonstrate this, we are first going to create a sample application for a better understanding of the process. 

Prerequisites 

  • Python
  • pip
  • Heroku CLI
  • Git

 

Deploying Flask App on Heroku

Let’s create a simple flask application first and then it can be deployed to heroku. Create a folder named “eflask” and open the command line and cd inside the “eflask” directory. Follow the following steps to create the sample application for this tutorial.
STEP 1 : Create a virtual environment with pipenv and install Flask and Gunicorn .
 

$ pipenv install flask gunicorn 

STEP 2 : Create a “Procfile” and write the following code. 
 

$ touch Procfile 

 

STEP 3 : Create “runtime.txt” and write the following code.
 

$ touch runtime.txt 

 

STEP 4 : Create a folder named “app” and enter the folder. 
 

$ mkdir app
$ cd app

STEP 5 : Create a python file, “main.py” and enter the sample code.
 

 touch main.py 

 

Python3




from flask import Flask
 
app = Flask(__name__)
 
@app.route("/")
def home_view():
        return "<h1>Welcome to Geeks for Geeks</h1>"


STEP 6 :Get back to the previous directory “eflask”.Create a file“wsgi.py” and insert the following code.
 

$ cd ../
$ touch wsgi.py

 

Python3




from app.main import app
 
if __name__ == "__main__":
        app.run()


STEP 7 : Run the virtual environment.
 

$ pipenv shell 

STEP 8 : Initialize an empty repo, add the files in the repo and commit all the changes.
 

$ git init 
$ git add .
$ git commit -m "Initial Commit"

STEP 9 : Login to heroku CLI using 
 

heroku login

Now, Create a unique name for your Web app.
 

$ heroku create eflask-app

 

STEP 10 : Push your code from local to the heroku remote. 
 

$ git push heroku master

 

Finally, web app will be deployed on http://eflask-app.herokuapp.com
 

 



Previous Article
Next Article

Similar Reads

How to deploy Node.js app on Heroku from GitHub ?
In this article, we will be looking at how to deploy your Demo Node.js app to Heroku. At the end of this article, we will have a basic Hello World app running on a public domain that can be accessed by anyone. The Node must be installed on your machine. Refer to this article How to install Node on your machine.We are also going to use Github accoun
3 min read
Deploy your Machine Learning web app (Streamlit) on Heroku
In this article, We will go through some simple and easy steps to deploy Machine Learning web app, built using Streamlit on Heroku cloud. This article is easy to follow as this approach does not require you to have a knowledge of Heroku CLI. What is Heroku? Heroku is a Platform as a Service (PaaS). It is a cloud platform where one can build, operat
6 min read
How to deploy React app to Heroku?
React is a very popular and widely used library for building User Interfaces. So if you are thinking about deploying your React app to the cloud platform, there are various choices for doing that such as AWS EC2 or Heroku. But for testing your React app, Heroku will be the best option as it is free and very easy to get started with. Prerequisites:
3 min read
How to Deploy Django application on Heroku ?
Django is an MVT web framework used to build web applications. It is robust, simple, and helps web developers to write clean, efficient, and powerful code. In this article, we will learn how to deploy a Django project on Heroku in simple steps. For this, a Django project should be ready, visit the following link to prepare one:https://www.geeksforg
4 min read
How to Deploy a Basic Static HTML Website to Heroku?
Heroku is a simple and one-stop solution to host any website or server. This article revolves around how you can host your own Static HTML webpage on Heroku. To demonstrate this we are going to build a simple webpage and host it. PrerequisitesGitHeroku AccountHeroku CLI Let's create a directory named "portfolio" for our project. We will connect thi
3 min read
How to Deploy a Django Application to Heroku with Git CLI?
Django is a popular web Framework of Python. It is based on MVT(Model-View-Template). MVT is a software design pattern for developing a web application. For more details about Django visit the Django tutorial. Deploying Django project using Heroku here is two methods: Deploying Django project on Heroku using CLI Deploying Django project on Heroku u
3 min read
How to upload Laravel App to Heroku Cloud Application Platform
Prerequisites :Knowledge of PHP (Laravel)A Heroku user accountA basic knowledge of Git version control Setting up Heroku CLI: You can download Heroku CLI from here. We recommend you to view this article to install Heroku CLI. Creating a Laravel App: In order to create a laravel app goto your command line and write the following command. $ composer
3 min read
Deploying a Django App to Heroku Using Github Repository
Heroku is a free hosting cloud service provider. We can use our free dynos to deploy our applications on the cloud. The only disadvantage is that it loses all the data once the app sleeps and it cannot handle multiple requests at a time when hosted on free dynos. First of all, to proceed further you need to have these two things ready Django appHer
3 min read
Deploying Django App on Heroku with Postgres as Backend
Django is a high-level Python web framework used to create web applications without any hassle, whereas, PostgreSQL is a powerful, open-source object-relational database. Let us first create a Django application with PostgreSQL in the backend and deploy it in Heroku which is a container-based cloud Platform to deploy, manage, and scale the applicat
5 min read
How to use Flask-Session in Python Flask ?
Flask Session - Flask-Session is an extension for Flask that supports Server-side Session to your application.The Session is the time between the client logs in to the server and logs out of the server.The data that is required to be saved in the Session is stored in a temporary directory on the server.The data in the Session is stored on the top o
4 min read
Article Tags :
Practice Tags :