Skip to main content

Get started with Python Flask Framework on IBM Bluemix

Get started with Python Flask Framework on IBM Bluemix

The tutorial will develop a Python web application based on Flask framework and deploy the app on IBM Bluemix. The app shows up a basic form which users can type their names. When hit enter the name will be added to database and can be accessed at http://base_url/api/visitors

1. Requirement

2. Clone example code and run locally

git clone https://github.com/IBM-Bluemix/get-started-python
cd get-started-python

  • Rename your app
Open up,manifest.yml similar like:
    applications:
        - name: Lecheebot
        random-route: true
        memory: 128M
now, you can run your app locally by typing the command:
pip install -r requirements.txt
python hello.py
Your app is now accessible at http://localhost:8080

3. Deploy to IBM Bluemix using Cloud Foundry CLI

Ensure you have IBM Bluemix account and installed CF CLI on your machine. if not, follow the steps: [https://github.com/cloudfoundry/cli#downloads
  • Log in to CF CLI
Run CMD as administrator, then login to CF with the commands:
cf api https://api.ng.bluemix.net
cf login
Enter your email and password.
  • Deploy your app
Open CMD at your app reposit that you cloned in step 2. Execute the command to deploy your app:
cf push
You can view your app status by the command:
cf apps

4. Add Database

  • To connect to Cloudant NoSQL DB, follow these steps:
  • Navigate to your app. Click on "Connect new" button on Connections Group. Choose Cloudant NoSQL DB service on the categories. Finally, click Create button.
  • Ensure that you connect Cloudant NoSQL DB service to your application. If not, click on the "Connect existing" to connect with service.
Now, You can test your app at your app URL.
  • To connect Database for working locally:
  • Create a file with the name vcap-local.json
  • Add the following detail:
{
 "services": {
   "cloudantNoSQLDB": [
     {
       "credentials": {
        "username": "<your_username>",
        "password": "<your_password>",
        "host": "your_host",
        "port": 443,
        "url": "yoururl"

       },
       "label": "cloudantNoSQLDB"
     }
   ]
 }
}
You can find the username, password, host by click on Cloudant NoSQL DB service at your IBM Blumix console. Then click on "Service Credentials". Copy and pass information to your vcap-local.json file.
Now you can test your app locally with the connected database.
python hello.py


Comments

Popular posts from this blog

Final report for Open IoT Challenge 3.0

MQTT demo

Overview and Architecture of Wireless Sensor Network System (WSN)