Thursday, April 18, 2013

Creating a basic Python app on Cloud 9 for use on Heroku

As part of our upcoming Code Wars competition in June, I've been working on creating services for multiple languages that will exist on various cloud hosting providers.  Python is one of the main languages we want to support this time around so I'm documenting my journey on creating a basic "web service" type app that will live on Heroku.

Create Cloud 9 app

Luckily Python is one of the languages supported by Cloud 9 so I didn't bother to actually install Python on my local machine.  When you create a new workspace you can select Python/Django as an option.

I deleted the hello-world.py file and created an app.py file to be used as the main program to run the web service.  I don't know that much about Django other than it is a close equivalent of Rails.  For this simple web service I decided to use something simpler than will just need to respond to a few url routes with a json response.

I found Bottle which involved pasting the contents of a single file into my project.  Here is a link to the Bottle website and here is a direct link to the python code you would need to copy and then paste into Cloud 9 as the file bottle.py.

Now for the actual app.py code contents:
# imports
import os
import json
from bottle import route, request

# routes
@route('/')
def index():
    return "Placeholder for Python app"
    
@route('/command', method='POST')
def index():
    status = json.loads(request.forms.get('status')) # can also use request.query.get
    players = status["players"]
    return json.dumps(status)

# start server and listen for requests
# for Cloud9 run(host=os.environ["OPENSHIFT_DIY_IP"], port=int(os.environ["PORT"]), debug=True)
run(host='0.0.0.0', port=int(os.environ["PORT"]), debug=True)

What's important to note is on line 18, the value used for the host variable on Cloud 9 is os.environ["OPENSHIFT_DIY_IP"].  A lot of people on StackOverflow were saying to just use os.environ["IP"] but that key did not exist for me when I tried it.

You'll notice line 18 is commented out in favor of line 19.  Line 18 is for testing out your app on Cloud 9 specifically, and line 19 is for when you're ready to deploy to Heroku.


Files needed for Heroku deployment

Heroku will look for 2 specific files when you deploy a Python app to it: Procfile and requirements.txt

For Procfile you will want to enter the following (assuming your main file is app.py):
web: python app.py

For requirements.txt you actually don't need anything in it, just create it as a blank file in the root folder.

From here you can init a new git repository, add all files to it, commit it, and then deploy to Heroku.  I always use the deploy tab on Cloud 9 (the hot air balloon icon on the left) to create my initial Heroku repository.  From there I use terminal to do the actual git deployment.

And that's it, from there you should have a python app that responds to requests!

1 comment:

  1. Would app.py not also need to import run from bottle.py?

    Thanks for the tutorial!

    ReplyDelete