LoginSignup
1
2

More than 5 years have passed since last update.

Bluemix の flask で、ビットコインと円のレートを取得

Last updated at Posted at 2017-06-25

Bluemix で、Hello World のプログラムは簡単にかけますが、その先に進むのは大変です。一歩進んだだけのサンプルを作成しました。
Bluemix で作成したサンプルプログラムをダウンロードして、それを改造し、アップロードします。

改造するファイルは、
manifest.yml
requirements.txt
welcome.py
static/index.html の4つです。

manifest.yml
applications:
- path: .
  memory: 128M
  instances: 1
  domain: mybluemix.net
  name: get_rate
  host: getrate
  disk_quota: 1024M
  buildpack: python_buildpack
requirements.txt
Flask==0.10.1
requests==2.7.0
welcome.py
# -*- coding: utf-8 -*-
#
#   get_rate/welcome.py
#
#                   Jun/25/2017
# ----------------------------------------------------------------------
import os
from flask import Flask, jsonify
import requests

# ----------------------------------------------------------------------
app = Flask(__name__)

@app.route('/')
def Welcome():
    return app.send_static_file('index.html')

@app.route('/myapp')
def WelcomeToMyapp():
    return 'Welcome again to my app running on Bluemix!'

# ----------------------------------------------------------------------
@app.route('/get_rate')
def WelcomeToGet_rate():
    url='https://api.bitflyer.jp/v1/ticker?product_code=BTC_JPY'
    rr=requests.get (url)
    dict=rr.json()
    return jsonify(results=dict)
#
# ----------------------------------------------------------------------
@app.route('/api/people')
def GetPeople():
    list = [
        {'name': 'John', 'age': 28},
        {'name': 'Bill', 'val': 26}
    ]
    return jsonify(results=list)

@app.route('/api/people/<name>')
def SayHello(name):
    message = {
        'message': 'Hello ' + name
    }
    return jsonify(results=message)

port = os.getenv('PORT', '5000')
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=int(port))
static/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="static/stylesheets/style.css">
<title>get_rate Jun/25/2017</title>
</head>
<body>
<h1>get_rate</h1>
<blockquote>
<a href="/get_rate">/get_rate</a><br />
</blockquote>
Jun/25/2017 PM 18:42<p />
</body>
</html>

cf push をかけた結果

bluemix_jun2501.png

bluemix_jun2502.png

bluemix_jun2503.png

1
2
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
1
2