我有一个简单的烧瓶应用程序运行,在用户输入后发布一些数据。目前,它在json.dump字典中显示此数据,但我试图让它显示如下:
Name: yyy
Shack: yyy
Status: yyy
Payments: vvv
我已经看到了一些与jsonify和其他json方法有关的问题,但我似乎无法让它们发挥作用。 代码:
from flask import Flask, request, Response, render_template
import json
import sys
import numpy as np
app = Flask(__name__)
app.config['DEBUG'] = True
path = '/home/username/ishack/'
if path not in sys.path:
sys.path.append(path)
client_db = np.genfromtxt('/home/username/ishack/client_db.csv', delimiter=',', dtype=None, names=True)
@app.route('/')
def form():
return render_template('form_page.html')
@app.route('/address', methods=['POST'])
def display_info():
address = request.form['address']
ind = np.where(client_db['Shack'] == address)[0]
res = {'Name': client_db[ind]['Name'][0],
'Shack': client_db[ind]['Shack'][0],
'Status': client_db[ind]['Status'][0],
'Payments': client_db[ind]['Payments'][0]}
return Response(json.dumps(res), mimetype='application/json')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)
答案 0 :(得分:0)
更改响应以呈现具有所需格式的页面。您无法对纯JSON响应进行样式化。
@app.route('/address', methods=['POST'])
def display_info():
address = request.form['address']
ind = np.where(client_db['Shack'] == address)[0]
res = {'Name': client_db[ind]['Name'][0],
'Shack': client_db[ind]['Shack'][0],
'Status': client_db[ind]['Status'][0],
'Payments': client_db[ind]['Payments'][0]}
return render_template("address_page.html", address=res)
<强> address_page.html 强>:
<table>
{% for key,value in address.items() %}
<tr>
<td>{{key}}:</td><td>{{value}}</td>
</tr>
{% endfor %}
</table>