如何在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 Python 3中制作一个简单的比特币应用程序

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

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

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模板。确保将其保存在模板文件夹中。

<!DOCTYPE html>

<html>

<head>

<title>Node App</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<link rel="stylesheet" href="../static/style.css">

<link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<div class="container">

<h3 class="header">Hello, Bitcoin!</h3>

<br>

<h3 class="message">{{message}}</h3>

<form method="post">

<input type="text" name="text" placeholder="currency code">

<br><br>

<input type="submit" name="submit" value="Submit">

</form>

<br>

</div>

</body>

</html>

有些事情要注意我们的HTML。我们使用<link>标签导入谷歌字体和引导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,编码:柯西编码:柯西编码:柯西

猜你喜欢

转载自blog.csdn.net/qq_42022255/article/details/80142589