DEV Community

Cover image for Deploy Flask to HEROKU - With Samples
Sm0ke
Sm0ke

Posted on

Deploy Flask to HEROKU - With Samples

Hello Coders,

This article explains how to deploy Flask to HEROKU, a popular Platform-as-a-Service provider (PaaS) which makes it easy for developers to deploy apps in different technologies and frameworks. The platform supports all major languages like Python, Ruby, Java, PHP, and popular frameworks (Flask included).
To provide something useful, the article comes with a few deployment-ready samples, developed by AppSeed and released under the MIT License on Github.


Thank you for reading! TL;DR; - Links & Resources


Deploy Flask to HEROKU - Argon Dashboard a free sample project.


What is Flask

Short-note for beginners - Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. Classified as a microframework, Flask is written in Python and it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.


Set up PC for Flask

Being a Python framework, Flask requires Python to run and expose his magic. Install Python is fairly simple via the official installers available on the official download page.

After installer execution, just open a terminal window and type python --version. On my PC, this command return:

$ python --version
Python 3.8.5
Enter fullscreen mode Exit fullscreen mode

Once Python is installed, we can use PIP, the official package manager for Python, to install Flask:

$ pip install Flask
Enter fullscreen mode Exit fullscreen mode

What is HEROKU

HEROKU is a popular platform that automates the deployment for apps coded on many languages like Python, GO, Ruby, JAVA, Php, and frameworks (Flask included).

To get started the first thing is to create an account on the HEROKU platform and install the command-line interface that matches our OS.


HEROKU - Sign Up page

HEROKU - Sign Up page.


HEROKU - Instal CLI

HEROKU - Instal CLI.


We can check the installation by typing heroku -v in the console. Any output that's not an error, means we are on the good track. On my PC, the output looks as below:

$ heroku -v
heroku/7.42.13 win32-x64 node-v12.16.2
Enter fullscreen mode Exit fullscreen mode

From this point, we can move forward with our sample apps. The source code is published on Github and the recommended way to download the samples is to use GIT command-line tool. If your workstation doesn't have GIT installed, the apps can be downloaded as ZIP archives.


Flask HEROKU Sample #1 - Black Dashboard

Flask Black Dashboard is an open-source, production/deployment-ready starter released under the MIT License on Github. I will iterate below the relevant files that make the HEROKU deployment possible with just a few lines written in the terminal.


#1 runtime.txt — specify the Python version to be used

python-3.6.10
Enter fullscreen mode Exit fullscreen mode

#2 Procfile — the HEROKU app bootstrapper

web: gunicorn run:app --log-file=-
Enter fullscreen mode Exit fullscreen mode

The above line instructs HEROKU to use the Gunicorn WSGI server to execute the WSGI app object, returned by run.py, located at the root of the project.

# Contents of run.py
from flask_migrate import Migrate
from os import environ
... 
app = create_app( app_config )
Migrate(app, db) 
# At this point, app is the WSGI object that Gunicorn expects.
Enter fullscreen mode Exit fullscreen mode

The gunicorn module must be also present in the requirements.txt file, along with other modules required by the app.

flask
flask_login
...
python-decouple
gunicorn          # <--- The magic line
Enter fullscreen mode Exit fullscreen mode

With all configuration in place, we can start the deployment by typing a few lines in the terminal.


#1 Clone the source code

$ git clone https://github.com/app-generator/flask-black-dashboard.git
$ cd flask-black-dashboard
Enter fullscreen mode Exit fullscreen mode

#2 HEROKU Login - this will trigger a new browser window

$ heroku login
Enter fullscreen mode Exit fullscreen mode

#3 Create the app in HEROKU world

$ # Create the app with a random name
$ heroku create 
$
$ # Create app using a name
$ heroku create you-name-here
Enter fullscreen mode Exit fullscreen mode

#4 Compile the app in HEROKU environment. This step might take a while.

$ git push heroku master
Enter fullscreen mode Exit fullscreen mode

#5 Open the app in the browser

$ heroku open
Enter fullscreen mode Exit fullscreen mode

At this point, the sample app should be visible in the browser.

Flask Dashboard - Black Design, free starter coded in Flask by AppSeed.


Flask HEROKU Sample #2 - Webpixels

This Flask starer is provided with database, ORM, a simple codebase, and deployment scripts (HEROKU included).
The project README contains all the necessary information to compile the app in a local environment and also to deploy the app on HEROKU with a few lines of code:


$ # Clone the source code:
$ git clone https://github.com/app-generator/flask-illustrations-webpixels.git
$ cd flask-illustrations-webpixels
$
$ # Check Heroku CLI is installed
$ heroku -v
heroku/7.25.0 win32-x64 node-v12.13.0 # <-- All good
$
$ # Check Heroku CLI is installed
$ heroku login
$ # this command will open a browser window - click the login button (in browser)
$
$ # Create the Heroku project
$ heroku create
$
$ # Trigger the LIVE deploy
$ git push heroku master
$
$ # Open the LIVE app in browser
$ heroku open
Enter fullscreen mode Exit fullscreen mode

Once the browser is triggered to open a new page, we should see Flask Webpixels running.


Flask Webpixels - simple Flask application, deployed on HEROKU.


Flask HEROKU Sample #3 - DattaAble

This open-source admin dashboard coded in Flask with a basic set of features can be deployed instantly on HEROKU just by typing the same commands as for the previous samples:


$ # Clone the source code:
$ git clone https://github.com/app-generator/flask-dashboard-dattaable.git
$ cd flask-dashboard-dattaable
$
$ # Check Heroku CLI is installed
$ heroku -v
heroku/7.25.0 win32-x64 node-v12.13.0 # <-- All good
$
$ # Check Heroku CLI is installed
$ heroku login
$ # this command will open a browser window - click the login button (in browser)
$
$ # Create the Heroku project
$ heroku create
$
$ # Trigger the LIVE deploy
$ git push heroku master
$
$ # Open the LIVE app in the browser
$ heroku open
Enter fullscreen mode Exit fullscreen mode

By visiting the browser, our sample Flask starter should be usable. Just create a new user and authenticate it.


Flask HEROKU deployment - DattAble Dashboard, a sample project provided by AppSeed.


Using HEROKU for deployment might be a good solution for fast deployment without touching the server environment not only for Flask apps. As mention in the first paragraph, HEROKU can be used for Django, Nodejs, or JAVA web applications. More information can be found by accessing the official docs:


print('Thank you!')
Enter fullscreen mode Exit fullscreen mode

Top comments (9)

Collapse
 
anzhari profile image
Anzhari Purnomo

Have you tried uvicorn before? Did you notice any significant performance boost compared to gunicorn?

Collapse
 
uithemes profile image
ui-themes

uvicorn is super ...

Collapse
 
sm0ke profile image
Sm0ke

Hello Anzhari!
I will take a look at uvicorn, thanks for your suggestion.
I'm using uWSGI in production.

Collapse
 
siddheshshankar profile image
Siddhesh Shankar

Hi, I deployed a simple web app on heroku platform.
Please have a look at my blog
dev.to/siddheshshankar/deploying-a...

Please share your thoughts.

Collapse
 
sm0ke profile image
Sm0ke

I will take a look. Thanks for sharing!

Collapse
 
uithemes profile image
ui-themes

Nice. Thanks for writing!

Collapse
 
sm0ke profile image
Sm0ke

Yw!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Good to see Flask getting more attention on here.

Collapse
 
sm0ke profile image
Sm0ke

Flask is amazing ..