Aside

Hello World on Bluemix With Python

Getting Started

Get an account

Access bluemix Web site here: https://ace.ng.bluemix.net/
Click on the “Sign Up” for Bluemix button.
Read quick start  http://www.ng.bluemix.net/docs/QuickStart.jsp

Installing the cf tool

Before you begin, install the command line tool that is used to upload and manage your application. BlueMix uses a tool called cf command line interface.
Download cf command line interface from https://github.com/cloudfoundry/cli
The cf command is a program that you need to install. Select the appropriate installer for your operating system. After the cf command tool is installed, you can use cf -v command to verify its version as follows:

$cf -vcf version 6.0.0-90db382

Login using the cf tool

Login. Be sure to use the right endpoint. To see the end point type:

$cf –t
API endpoint: https://api.ng.bluemix.net (API version: 2.2.0)Not logged in. Use 'cf login' to log in.
$cf login

I had to reset mine to the beta site.

$ cf api https://api.stage1.ng.bluemix.net

Important files

Procfile is where the command is for launching your app

Web: python hello.py

Hello.py s the contents of your hello world app

import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return 'Hello World!'
    port = os.getenv('VCAP_APP_PORT', '5000')
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=int(port))

Get help getting answers to your questions

Go to IBM.com forums

Prepare the environment for your chosen runtime

Python is my language of choice. Bluemix doesn’t have Python as a pre-existing runtime. You can however create your own runtime by using buildpacks that create the environment for you.   In this hello world example we will use Flask as our HTTP server.
requirements.txt file describes the application prequisites.

Flask==0.10.1

You can push your application to Bluemix along with a Python buildpack:
You can find Python buildpacks on Github. https://github.com/ephoning/heroku-buildpack-python From this page you can click on the Download Zip file on the right column.
Unzip the file into a local directory. Extract the zip into directory named hello-python-master in the folder where cf tool was installed in.

c:\CloudFoundry\hello-python-master

Push App to Bluemix

cf push <YOUR_APP_NAME> -m 128M -b https://github.com/ephoning/heroku-buildpack-python.git
$ c:\CloudFoundry\hello-python-master>cf push hello-python-master -m 128M -b https://github.com/ephoning/heroku-buildpack-python.git

Check the logs in the command window for errors. You likely might get some because you aren’t executing the commands in the right folder. Make sure you invoke the command where the requirements.txt file is for the helloworld.py file. If successful you should see the output below.

…-----> Uploading droplet (26M) 0 of 1 instances running, 1 starting1 of 1 instances running 
App started Showing health and status for app hello-python-master in org carlos.ferreira@ youremail.com / space dev as carlos.ferreira@youremail.com...
OK requested state: startedinstances: 1/1usage: 128M x 1 instancesurls: hello-python-master.stage1.ng.bluemix.net      
state     since                   cpu   memory           disk#0   
running   2014-05-08 09:51:17 PM   0.0%   109.4M of 128M   99M of 1G


Try your App in the browser

Open a browser and type the name of the app pre-pended to your Bluemix URL.
http://hello-python-master.stage1.ng.bluemix.net
My app is hello-python-master
My Bluemix URL is stage1.ng.bluemix.net
Figure 1 Hello World
hello
Check the logs in the command line window
Check your dashboard on bluemix that the app is running

Figure 2. 1 Bluemix Dashboard
helloRunning
Took me about 1.5 hours to get the app running.   This was much faster than my hello world experience with Heroku.

Advertisements

2 thoughts on “Hello World on Bluemix With Python

  1. Maddie Von says:

    I think in the source code of the hello.py file there is at least one type – the last two lines probably should be

    if __name__ == ‘__main__’:
    app.run(host=’0.0.0.0′, port=int (port))

    instead of

    if __name__ == “__main__”:
    app.run(host=’0.0.0.0ort=int(port))

    But even after correcting this, I couldn’t get it running. In the end I used this source code:
    https://github.com/IBM-Bluemix/python-hello-world-flask
    (from this tutorial: https://www.youtube.com/watch?v=b-SF3bgaQTw )

    and with that it worked.

    • Carlos says:

      Hi Maddie, There are different ways you can deploy a Python processes. This example allows you to run a python process that behaves like a Web server. This is fine for Web Development but not production. There are other deployment models that include or provide a web container like the one you mention Flask. And others like http://nginx.org/ Thanks for sharing. I don’t believe you need to modify the “hello.py” code. In the version you tried you also had an additional step to bind the postgres service to your web app. In the latest version I modified the manifest file to also declare the postgres service and bind to it. So there should not be any additional manual binding step now to deploy/run your app. Also that other sample you suggest doesn’t include Django or create the Admin user in your database so that you can then add other users through the Django Admin site yourapp.mybluemix.net/admin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s