Heroku Office Entrance
Pulled out the image from here.
This is yet another article in this series that shows a practical hands-on using another Python Cloud Computing solution there existed in our planet: Heroku. We will going to try to publish our Openshift's Flask Biography application in Heroku, and along the way --it's unavoidable-- we will compare the process with Openshift, which due to Openshift's generosity in offering 1 GB free plan (including database and user uploaded files!), I still think Openshift is the best Python Cloud Computing platform for developers out there. I got to remind you though, I am not talking in term of its performance comparison in handling multitude number of visitors. We are just going to find out how to create, deploy and manage application installed in Heroku. The branch that we are going to use, is the Blueprints branch in our public pythonthusiast/bio Github repository.
Great, lets get started!
Heroku Usage at a Glance
Prepare Local Workstation
First of all, signup to Heroku and install its heroku toolbelt according to your Operating System. Open your command prompt/terminal and type:
heroku login. Follow its login process obediently and upon successful, you will be able to issue
heroku command to create or manage your deployed application.
PS: If this is your first time working for a PaaS/cloud computing environment, or if you have never use Git before, chance are that you don't have an SSH key. Don't worry about that.
heroku login command will detect your current working environment, and depending on that, it will offer to create a pair of private/public SSH key. But, it seems like it will not upload an existing SSH key. For that, you have to manually upload your SSH key located at
~/.ssh/id_rsa.pub into your Heroku Dashboard.
Prepare Simple Flask Application
Before we begin on hosting our previous Biography Flask application in Heroku, we are going to test it with a small Flask application. First, let's make sure we have Flask package installed first:
1 2 3
$ pip install Flask #you can skip this step if you have already installed Flask in your working Python environment, or use virtualenv instead $ mkdir flaskmyheroku $ cd flaskmyheroku/
Create a Python module, name it
hello.py, with this content:
1 2 3 4 5 6 7 8 9 10 11
import os from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': app.run('0.0.0.0', 8888)
$ python hello.py * Running on http://0.0.0.0:8888/
Great! Although the application is simple enough, it is already a WSGI aware application and can run easily from the command line or hosted in WSGI enabled web server.
Another point to note is, have a look at this Flask quick start application explained in Heroku dev center. If you have already read it and then compare it with the above simple flask application, you'll notice that I don't use
foreman to start the application. I have a very good reason of avoiding
foreman: I simply can't use it to start the application. Several solution which basically told to install Heroku toolbelt to directory not contains spaces and use
foreman version 0.61, is not working for me. Thereby, I simply skip of using
foreman to start the application.
Make Application Runnable in Heroku
But, how do we make this application runnable in Heroku? Differ with Openshift that use uWSGI as its WSGI server, Heroku use gunicorn as its WSGI server for Python application. gunicorn needs to have a regular text file named
Procfile file to be existed in your application root directory that contains definition of your startup Python module. Here is the content of a valid
web: gunicorn hello:app
It told gunicorn that we have a web application from
hello module (a.k.a
hello.py Python file), with a WSGI application named
app. Please remember from our previous discussion, that the above
if __name__ == '__main__': code will only got executed if
hello module was run from the command line. As this
hello module will be run by gunicorn, it won't bother with this line.
Now that Heroku know what is your startup module, how do we tell Heroku what Python packages that our application need? Simple. We need another regular text file named
requirements.txt that also reside in our application root directory. This file will be use by Heroku to run
pip (Python package management system), and prepare all dependent packages needed by your application into local working Python environment.
requirements.txt contains the same semantic entry (but different format) to what it is in Openshift's
setup.py needed by
setuptools, which is also another Python package management system. Here is the content of
requirements.txt needed by our simple Flask application above:
And those are the steps needed to make every Python web application runnable in Heroku.
Create Heroku Application
This subsection is interesting: it will be quick. Don't believe me? Here goes the commands needed to host the above simple Flask application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
$ git init Initialized empty Git repository in c:/Users/Eko Wibowo/__sandbox__/herokuflask/.git/ $ git add . -A $ git commit -m'initial commit' [master (root-commit) 0766188] initial commit create mode 100644 .gitignore create mode 100644 Procfile create mode 100644 hello.py create mode 100644 requirements.txt $ heroku create herokuflask Creating herokuflask... done, stack is cedar http://herokuflask.herokuapp.com/ | email@example.com:herokuflask.git Git remote heroku added $ git push heroku master Initializing repository, done. Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 2.14 KiB | 0 bytes/s, done. Total 6 (delta 0), reused 0 (delta 0) -----> Python app detected -----> No runtime.txt provided; assuming python-2.7.6. -----> Preparing Python runtime (python-2.7.6) -----> Installing Setuptools (2.1) -----> Installing Pip (1.5.4) -----> Installing dependencies using Pip (1.5.4) -----> Discovering process types Procfile declares types -> web -----> Compressing... done, 30.4MB -----> Launching... done, v3 http://herokuflask.herokuapp.com/ deployed to Heroku To firstname.lastname@example.org:herokuflask.git * [new branch] master -> master $
Now you can navigate to http://herokuflask.herokuapp.com and see its live in action!
To sum up the above process, first we initialized an empty git repository in the current directory where our simple Flask application reside. Next we create a Heroku application named
herokuflask, which will be automatically hosted in the domain http://herokuflask.herokuapp.com (when you have git push-ed your application of course). The process will also add remote git repository named
heroku, which then you use it to push your application to heroku: making it live in the internet!
Straightforward, isn't it? The only obstacle that I can think of, is if you haven't uploaded (or even created!) your public SSH key to your Heroku account. I think I will specifically address this matter in another article. It's an introductory material that I seems to be skipped throughout articles in this site. Pardon for that...
Another point to note is, differ with Openshift, the application created in Heroku will not using specific user domain. So, if you create Heroku application by simply copy pasting the above code:
heroku create herokuflask, you will be in trouble. I have already created an application with that name! In Openshift, the similar action will result in an application that will be hosted in http://herokuflask-ekowibowo.herokuapp.com. And it will have different URL based on your user domain. Simple, yet useful
At first, I woud like to use a single article that talk about how to use Heroku as your Python Cloud Computing solution. I feels like going to treat is as my first article in this series : PythonAnywhere. But the truth is, Heroku is not as easy and as straightforward to use as PythonAnywhere, where you don't have to prepare any local workstation. In this regard, Heroku is much like Openshift (or it must be the other way round, as Openshift was born after Heroku).
Therefore, I split the discussion of hosting our Flask Biography application to Heroku in the next subsequent article in this series.