Flask Python Heroku

 lets start building source code

cd source # MyHerokuProject/source 

We need to create two files for heroku to spin up our application;

pip freeze > requirements.txt
cat >Procfile
web: python hello.py

create your index.html equivalent

cat >hello.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)

and lets git it to heroku

git init 
git add .
git commit -m 'init'
heroku create
git push heroku master 

Heroku Setup for Python and Flask

Getting virtualenv – Setuptools, pip, easyinstall on Ububtu

  • Install python setuptools on Ubuntu
  • Install pip on Ubuntu
  • easy_install on Ubuntu
  • virtualenv on Ubuntu – yes, get used to it.
sudo apt-get install python-setuptools
sudo apt-get install python-pip
  • Install Python development libraries, could be optional but we will use it here.
sudo apt-get install python-dev
  •  Get virtual env
sudo apt-get install python-virtualenv

You might also need a public key from your Ubuntu workstation. If your workstation is secure, you can just hit enter for the passphrase.

ssh-keygen -t rsa

Install the heroku toolbelt.

  • Install Heroku toolbelt on Ubuntu
  • Install Heroku toolbelt on AWS running Ubuntu
wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

Now your 127.0.0.1 server is in sync with Heroku libraries.

Lets start a project.

mkdir MyHerokuPyProject
mkdir MyHerokuPyProject/source

Now, logout and login in to get heroku in the PATH, or do this;

 sudo su ubuntu

Log into heroku;

heroku login

You need to make heroku aware of the public key;

heroku keys:add

Ok, let’s make sure everything is set so far by starting up virtualenv

Lets use our project directory;

cd MyHerokuPyProject 

 We will add a virtual environment to our Python Heroku project;

virtualenv vedir --distribute
source vedir/bin/activate

Now we can get bottle like this. Notice that I am getting the bottle library, not the bottle.py library. It’s the same thing, but if you try to fetch bottle.py it will fail. Easy_install is a pythoin installation system, so it knows that that which we seek is a .py hootinanny.

pip install Flask

Add the Python image library, which requires the development library;

pip install pil

— or —

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

log out and log in


heroku login
heroku keys:add
cd MyHerokuPyProject
virtualenv vedir --distribute
source vedir/bin/activate
pip install Flask
pip install pil