A Heroku Python Flask website application

We have been talking about Heroku applications using Python, where we can set up Python based web hosting without the need to work around Apache to get a python based web site running. In our python website example we use the flask library instead of Apache for the server.

The simplest 'Hello World' flask application is easy to find. It is on the Heroku website as well as in documentation that can be found for the Flask modules. Nonetheless, here it is again:

1
2
3
4
5
6
7
8
from flask import Flask
 
app = Flask(__name__)
@app.route('/')
def hello():
 return 'Hello World!'
if __name__ == '__main__':
 app.run('')

To simplify using flask, lets just run this application on our Ubuntu computer to 127.0.0.1

To do this, we need to have the Flask library available to applications.

    pip install flask

Then, all we need to do to see our 'Hello World' application is this:

    python hello.py

Holy strawberries Batman! We've got a website! Does Heroku work this simply? No, the Heroku version of Hello World contains two extra lines of code, and uses the OS module for Python, so to conclude today's discussion about simple python web site applications, here is the Heroku version of Hello World.

1
2
3
4
5
6
7
8
9
10
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
 return 'Hello World!'
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
 port = int(os.environ.get('PORT', 5000))
 app.run(host='0.0.0.0', port=port)

If you don't believe me, go to the www.heroku.com website, they really deserve the credit, because I cut and pasted this example from them. Two lines of code are added to the main execution body of the example, first the variable 'port' is set with the results of an operating system call and that is provided for the app.run command.

Can you think of a way to turn this into a single line of code? If you can, you have started to read and understand Python, and hopefully look forward to our next discussion on Python Flask Heroku applications. See you later, alligator! 



Heroku, a web hosting company good for Python 2.7

Heroku is a web hosting service at www.heroku.com where Python web applications can be implemented at a low cost, as low as 45 EU per month. They also offer (for now) a free plan. They do this by selling CPU time as 'Dynos' where the more dynos that you purchase, the more web traffic you support.

There are two types of Dyno, Web and Worker. In order to implement your Python web application you will need at least one Web Dyno. We have used the examples on the Heroku website to implement both Flask and Django applications with their system. Many of us are accustomed with implementing a Flask or Bottle WSGI web application on Python to 127.0.0.1 on our notebook Ubuntu server and may be looking to publish our clever application to the internet without running it with our personal computer. As of the end of 2012 here, Heroku is a way to do this, for free or very low cost, but it does require we learn how Heroku works.

Heroku requires that we grab our libraries in a virtual environment. If you are not familiar with the Python virtualenv library / application, that is a vital foundation for taking advantage of Heroku hosting.

For experienced Ubuntu bash folks, the older posts here condense the process a bit from the Heroku website. For people that have spent years in bash, the Heroku instructions tend to be lengthy. In my case, while learning about and implementing a Heroku Python web application, I started several Virtual Machines of an Ubuntu 11.04 image and started with no libraries, building up the operating system and libraries including Virtualenv, and the 'Heroku Toolbelt'.

The Heroku Toolbelt is a required component, like virtualenv, of your Heroku endeavor. I used the Virtual Machine format to build my Heroku development platform because corporate policies do not allow installation of the Heroku Toolbelt on many Ubuntu platforms, but you should not have any problem doing this on your personal computer. This component adds a set of commands that begin with 'heroku ...' that allow server management of your Heroku instance. Heroku Toolbelt is similar to git, in that it is a set of commands that begin with a command.

Speaking of git, you will be helped a lot if you are familiar with git, but you don't need to know a lot of git. My usage of git with Heroku is limited to git add ., git commit and git push. Git is used instead of ftp to send your Python code to the server. You can also get your Python code with git clone, and you should also note that Heroku has PostgreSql for database. We even tried to use sqlite3 and Heroku had somehow turned that built in library off. It may be that our Heroku Python application is not allowed to write to the Heroku server, per se, as your application space at Heroku is really a git repository, and not a general use ftp accessed directory.

So, for Python Flask and Python Django applications Heroku is a really powerful and scaleable platform. And right now (2012), it's free!



Creating your first application and deploy to Heroku

cd source # cd $HOME/MyHerokuProject/source
pip freeze > requirements.txt
cat >Procfile
web: python hello_main_module.py
cat >hello_main_module.py
import os
from flask import Flask
# import BeautifulSoup
import urllib2
import re
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World!'
@app.route('/gb')
def goodbye():
return 'Good Bye'
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
git init 
git add . 
git commit -m 'init' 
heroku create 
git push heroku master 


Warming up an Ubuntu Server 7, like a micro-instance that you can get free for one year from AWS, or Amazon Web Services EC2, plus Flask.py and Heroku toolbelt

Many thanks to Heroku.com for much of this

sudo apt-get install python-setuptools
sudo apt-get install python-pip
sudo apt-get install python-dev
sudo apt-get install python-virtualenv
ssh-keygen -t rsa
wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh
mkdir MyHerokuPyProject
mkdir MyHerokuPyProject/source
 sudo su ubuntu
heroku login
heroku keys:add
cd MyHerokuPyProject 
virtualenv vedir --distribute
source vedir/bin/activate
pip install Flask
pip install pil



Copyright(c) 2017 - PythonBlogs.com
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved