交互目标: button按钮开始查询获取入站出站数据传到flask,flask写hive操作从外部表获取相应的数据并传到前端页面对应的文本标签内。
前言
准备flask的py文件,一个js文件
一、flask的py文件
from flask import Flask
from flask import render_template
from flask import request
import redis
import utils
from flask import jsonify
from flask import redirect, url_for
import json
import time
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/result', methods=['GET', 'POST'])
def get_station_info():
recv_data = request.get_data()
if recv_data:
json_re = json.loads(recv_data)
result = utils.get_result(json_re)
sw_time_min = result[0][0]
money = result[0][1]
change_min_num = result[0][2]
path_min = result[0][3]
return jsonify({"sw_time_min": sw_time_min,"money":money,"change_min_num":change_min_num,"path_min":path_min})
else:
print("receive data is empty")
if __name__ == '__main__':
app.run()
get_result()方法
def get_result(json_re):
conn = hive.Connection(host='192.168.30.140', port=10000, username='root', database='default')
cur = conn.cursor()
hql = "select sw_time_min,money,change_min_num,path_min from path where enter_station=\'"+json_re["enter_station"]+"\' and enter_line= \'"+json_re["enter_line"][0]+"\' and out_station= \'"+json_re["out_station"]+"\' and out_line=\'"+json_re["out_line"][0]+"\' "
cur.execute(hql)
result = cur.fetchall()
return result
二、js文件操作Ajax
function get_station_info() {
var line = document.getElementById("line").value;
var station = document.getElementById("station").value;
var line1 = document.getElementById("line1").value;
var station1 = document.getElementById("station1").value;
var msg = {enter_line:line,enter_station:station,out_line:line1,out_station:station1};
var senddata = JSON.stringify(msg);
console.log(senddata);
$.ajax({
url: "result",
type: "POST",
data: senddata,
dataType: "json",
success: function (data) {
$("#shijian").html(data.sw_time_min);
$("#piaojia").html(data.money);
$("#zhonzhuancishu").html(data.change_min_num);
$(".path_min").val(data.path_min);
}
});
}
三、html文件
<button class="button" id="button" onclick ="get_station_info()">开始查询</button>
总结
提示:总的来说,操作也不是很难,就是传来传去要理清思路,欢迎评论区讨论交流
|