首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Flask Python 3中制作一个简单的比特币应用程序

我想与大家分享如何在Flask Python 3中制作一个简单的比特币应用程序。本文将作为Flask的简介,制作API以及编写完整的应用程序服务器和数据库。在本教程结束时,您将拥有一个功能齐全的应用程序,可以在heroku应用程序引擎上发布。

第一部分:安装依赖关系

这个项目的依赖性非常小。我建议使用官方的Python 3包管理器,pip。

pip install flask tinydb requests

Flask将帮助构建我们的应用程序服务器。TinyDB将作为一个高性能的JSON数据库。requests将帮助我们构建我们的API。

第二部分:项目结构

Flask只需要两个文件夹:static和templates。CSS和Node文件放在static文件夹,html模板放入templates。

您的文件夹结构应与上述类似。

第三部分:构建我们的应用服务器

Flask使构建应用程序服务器和Web服务器变得非常容易。继续并确保server.py反映下面的代码。

from flask import Flask, render_template

from tinydb import TinyDB, Query

import requests

app = Flask(__name__)

@app.route('/')

def index_route ():

return render_template('index.html')

if __name__ == "__main__":

app.run(port=3000)

我们使用函数的装饰器将URL http://localhost:3000/绑定到index_route()。 接下来,我们需要添加数据库支持。应用程序声明后添加以下行。

...

db = TinyDB('assets / database.json')

table = db.table('table')

...

现在我们的应用程序服务器可以访问JSON数据库。

第四部分:添加HTML模板

这部分相当简单。没有必要解释。这是我将使用的HTML模板。确保将其保存在模板文件夹中。

Node App

Hello, Bitcoin!

{}

有些事情要注意我们的HTML。我们使用

标签导入谷歌字体和引导UI框架。Bootstrap自动处理样式,以便我们的CSS可以最小化。

接下来,我们需要设计我们的模板。在静态文件夹中,将其添加到您的CSS样式表文件中。

.container {

margin: 0 auto;

width: 50%;

height: 700px;

border: 1px solid lightgrey;

} .header {

font-family: 'Muli', sans-serif;

margin: 0 auto;

padding-top: 30px;

text-align: center;

} .message {

font-family: 'Muli', sans-serif;

margin: 0 auto;

text-align: center;

}

form {

width: 50%;

height: 200px;

padding-top: 100px;

margin: 0 auto;

}

input[type="text"] {

outline: 0;

border: 0;

border-bottom: 1px solid lightgrey;

font-family: 'Muli', sans-serif;

}

input[type="text"]:hover, input[type="text"]:active {

border-bottom: 1px solid darkgrey;

}

input[type="submit"] {

background-color: #4CAF50;

border-radius: 32px;

font-family: 'Muli', sans-serif;

color: white;

width: 60px;

height: 20px;

border: 0; outline: 0;

-webkit-transition-duration: 0.4s; /* Safari */

transition-duration: 0.4s;

}

input[type="submit"]:hover {

background-color: white; /* Green */

color: #4CAF50;

border: 1px solid #4CAF50;

}

第五部分:添加一个API

在这里,我们将为我们的程序添加一个简单的GET API。它将使用用户提供的国家代码出去并获取当前比特币在该国的价格。这听起来令人困惑,但并不困难。这是你的server.py的样子。

from flask import Flask, render_template

from tinydb import TinyDB, Query

import requests

app = Flask(__name__)

db = TinyDB('assets/database.json')

table = db.table('table')

@app.route('/')

def index_route ():

return render_template('index.html')

if __name__ == "__main__":

app.run(port=3000)

继续并添加另一个路由功能。我们将该方法设置为POST,以便当用户在我们所做的表单上提交时,这个函数将被调用。

@app.route('/', methods=['GET','POST'])

def index_post_route ():

return render_template('index.html', message=None)

现在我们需要添加我们的api调用代码。我不会解释它。任何曾经使用过Node.js或Ruby的人都应该知道发生了什么。把它放在我们的数据库代码中。

def api_call (country_code):

url = "https://api.coindesk.com/v1/bpi/currentprice.json"

response = (requests.get(url))

data = response.json()

price = data['bpi'][country_code]['rate']

return str(price).strip('.', 1)[0]

现在我们需要编辑我们的发布功能,看起来像这样。

@app.route('/', methods=['GET','POST'])

def index_post_route ():

return render_template('index.html',

message=api_call(request.form['text']))

我们的应用程序快完成了。回到我们的API调用中,我们需要添加一个将价格保存到我们的数据库中的函数。

def api_call (country_code):

url = "https://api.coindesk.com/v1/bpi/currentprice.json"

response = (requests.get(url))

data = response.json()

price = data['bpi'][country_code]['rate']

table.insert({'price':price})

return str(price).strip('.', 1)[0]

这里是我们的server.py文件:

from flask import Flask, render_template

from tinydb import TinyDB, Query

import requests

app = Flask(__name__)

db = TinyDB('assets/database.json')

table = db.table('table')

def api_call (country_code):

url = "https://api.coindesk.com/v1/bpi/currentprice.json"

response = (requests.get(url))

data = response.json()

price = data['bpi'][country_code]['rate']

table.insert({'price':price})

return str(price).strip('.', 1)[0]

@app.route('/', methods=['GET'])

def index_route ():

return render_template('index.html')

@app.route('/', methods=['GET','POST'])

def index_post_route ():

return render_template('index.html',

message=api_call(request.form['text']))

if __name__ == "__main__":

app.run(port=3000)

上面就是小编所分享的内容,想要学习的可以加49+130+8659,编码:柯西 编码:柯西 编码:柯西

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180429A0ODBI00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券