LoginSignup
4
3

More than 5 years have passed since last update.

Bluemix の flask で Cloudant からデータを取得する

Posted at

次のページに、Cloudant への接続方法が書いてあります。
How can I connect to Cloudant from a Flask App running in Bluemix?

この例では、データベースの作成の例しかないので、データベースのリードのサンプルを作成しました。

作成したデータベースへのデータの入れ込みは手動で行いました。

改造が必要なのは、welcome.py だけです。

# -*- coding: utf-8 -*-
#
#   welcome.py
# ----------------------------------------------------------------------
import os
import json
import requests
from flask import Flask
from flask import jsonify

app = Flask(__name__)

app.config.update(
    DEBUG=True,
)

# ----------------------------------------------------------------------
def define_url_auth_proc():
    vcap = json.loads(os.getenv("VCAP_SERVICES"))['cloudantNoSQLDB']

    cl_username = vcap[0]['credentials']['username']
    cl_password = vcap[0]['credentials']['password']

    url         = vcap[0]['credentials']['url']
#
    auth        = ( cl_username, cl_password )
#
    return url,auth
# ----------------------------------------------------------------------
@app.route('/')
def welcome():
    return app.send_static_file('index.html')

# ----------------------------------------------------------------------
@app.route('/getdb/<db>')
def get_db(db):
    try:
        url,auth = define_url_auth_proc()
    except:
        return 'A Cloudant service is not bound to the application.  Please bind a Cloudant service and try again.'

    rr=requests.get( url + '/' + db, auth=auth )
    dict=rr.json()
    return jsonify(results=dict)


# ----------------------------------------------------------------------
@app.route('/getdb2/<db>')
def get_db2(db):
    try:
        url,auth = define_url_auth_proc()
    except:
        return 'A Cloudant service is not bound to the application.  Please bind a Cloudant service and try again.'

    url_aa =  url + '/' + db + '/_all_docs?include_docs=true'
    rr=requests.get( url_aa, auth=auth )
    dict=rr.json()
    return jsonify(results=dict)
#
# ----------------------------------------------------------------------
@app.route('/createdb/<db>')
def create_db(db):
    try:
        url,auth = define_url_auth_proc()
    except:
        return 'A Cloudant service is not bound to the application.  Please bind a Cloudant service and try again.'

    requests.put( url + '/' + db, auth=auth )
    return 'Database %s created.' % db
# ----------------------------------------------------------------------
port = os.getenv('VCAP_APP_PORT', '5000')

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=int(port))
# ----------------------------------------------------------------------

手動でデータを入れた状況

cloudant_jun2700.png

実行結果
http://ekzemplarocc.mybluemix.net/getdb/test

cloudant_jun2701.png

cloudant_jun2702.png

実行する時には、アプリが、Cloudant NoSQL に接続している必要があります。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3