Deploy Flask to HEROKU - Atlantis Dark
Hello,
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 an open-source HEROKU deployment-ready sample - Flask Atlantis Dark.
Thank you for reading! TL;DR; Links (for fast-runners)
- Flask Atlantis Dark - product page
- HEROKU - the official website
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.
Getting started with Flask is quite easy, once we have Python installed. In case you're not sure about Python presence, just open a terminal and type python --version
. If this command returns something else than an error, you can move further and follow up the rest of this article.
On my workstation I have:
$ python --version
Python 3.8.5
The recommended way to install Flask is to use PIP, the official package manager for Python.
$ pip install Flask
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 - Instal CLI
We can check the installation by typing heroku -v
in the console. If HEROKU is properly istalled, the -v
argument should return the CLI version:
$ heroku -v
heroku/7.42.13 win32-x64 node-v12.16.2
From this point, we can move forward with our sample Flask Dashboard. The source code is published on Github and the recommended way to download the sample is to use GIT
command-line tool. If your workstation doesn't have GIT installed, the app can be downloaded as a ZIP archive.
Flask HEROKU Sample - Atlantis Dark
Flask Atlantis Dark 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
#2 Procfile — the HEROKU app bootstrapper
web: gunicorn run:app --log-file=-
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.
from app import app, db
if __name__ == "__main__":
app.run()
# At this point, app is the WSGI object that Gunicorn expects.
The gunicorn
module must be also present in the requirements.txt file, along with other modules required by the app.
flask
flask_login
...
gunicorn # <--- The magic line
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-dashboard-atlantis-dark.git
$ cd flask-dashboard-atlantis-dark
#2 HEROKU Login - this will trigger a new browser window
$ heroku login
#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
#4 Compile the app in HEROKU environment. This step might take a while.
$ git push heroku master
#5 Open the app in the browser
$ heroku open
At this point, the sample app should be visible in the browser.
Flask Atlantis DARK - Timeline page
HEROKU, FLASK resources
- Flask - the official website
- HEROKU - the official website
- More deployment-ready Flask Dashboards starters
Thank you for reading!
Feel free to AMA in the comments section.