我目前在flask-SQLAlchemy中设置了一个API,它以JSON格式返回输出,我很难把它变成可展示的表格或HTML格式。我希望它能以更多的表格形式而不是JSON形式呈现在我的网页上。
我试着用'json2html'包来转换json_data,但它只输出'Content-Type application/json',格式不正确。jsonify函数返回一个JSON形式的RESPONSE。 任何简单的建议或修复方法都可以帮助我们获得以表格形式呈现的数据!
from flask import Flask,jsonify, request, Response, render_template,redirect, url_for
import configparser, pymysql, json, requests
from json2html import *
from flask_sqlalchemy import SQLAlchemy
# API method to get a hike recommendation. This takes a URL containing several arguments related to hike preferences
@app.route('/findhike_result', methods=['GET'])
def findBestHike():
park = request.args.get('park')
level = request.args.get('level')
min = request.args.get('min')
max = request.args.get('max')
bath= request.args.get('bath')
dog= request.args.get('dog')
feat1= request.args.get('feat1')
feat2= request.args.get('feat2')
feat3= request.args.get('feat3')
feat4= request.args.get('feat4')
feat5= request.args.get('feat5')
proc_call = "call find_the_best_hike('" + park + "','" + level + "','" + min + "','" + max + "','" + bath + "','" \
+ dog + "','" + feat1 + "','" + feat2 + "','" + feat3 + "','" + feat4 + "','" + feat5 + "')"
print(proc_call)
result = mysql.engine.execute(proc_call)
data_all = []
for item in result:
data_all.append([item['trail name'], str(item['distance in miles']), item['description'],
str(item['average user rating']), item['features'], item['messages'], str(item['score'])])
json_data = jsonify(trails=data_all),{'Content-Type':'application/json'}
return json2html.convert(json=json_data)
**Sample JSON OUTPUT**
"trails": [
"Upper Yosemite Falls",
"7.20",
"Upper Yosemite Falls Trail is a 7.2 mile heavily trafficked out and back trail located near Yosemite Valley, California that features a waterfall and is only recommended for very experienced adventurers. The trail offers a number of activity options and is accessible year-round.",
"2.53",
"Upper Yosemite Falls has Waterfall , Climbing , Rocky , Forest , Scenic Views as features.",
"Upper Yosemite Falls goes above Alpine Zone. Please use caution.",
"5.00"
"Vernal and Nevada Falls via the Mist Trail",
"6.40",
"Vernal and Nevada Falls via the Mist Trail is a 6.4 mile heavily trafficked loop trail located near Yosemite Valley, California that features a waterfall and is rated as difficult. The trail is primarily used for hiking, walking, nature trips, and bird watching and is best used from April until October.",
"3.18",
"Vernal and Nevada Falls via the Mist Trail has Waterfall , Rocky , Forest , Scenic Views as features.",
"Have fun hiking Vernal and Nevada Falls via the Mist Trail!",
"3.80"
"Half Dome",
"14.80",
"Half Dome Trail is a 14.8 mile heavily trafficked out and back trail located near Yosemite Valley, California that features a waterfall and is only recommended for very experienced adventurers. The trail is primarily used for hiking, rock climbing, and nature trips and is best used from April until October.",
"3.07",
"Half Dome has Waterfall , Rocky , Forest , Scenic Views as features.",
"Half Dome requires a permit. Please consult with Park Rangers before attempting this trail. Half Dome is a long hike. Consider doing this trail over multiple days. Half Dome goes above Alpine Zone. Please use caution.",
"3.80"
"Four Mile Trail",
"7.50",
"Four Mile Trail is a 7.5 mile heavily trafficked out and back trail located near Yosemite Valley, California that features a waterfall and is only recommended for very experienced adventurers. The trail offers a number of activity options and is best used from April until November.",
"3.00",
"Four Mile Trail has Waterfall , Forest , Scenic Views as features.",
"Four Mile Trail goes above Alpine Zone. Please use caution.",
"2.80"
"North Dome",
"13.70",
"Yosemite Falls Trail to North Dome is a 13.7 mile out and back trail located near Yosemite Valley, California that offers the chance to see wildlife and is rated as difficult. The trail is primarily used for hiking, nature trips, and bird watching.",
"2.45",
"North Dome has Waterfall , Forest , Scenic Views as features.",
"North Dome is a long hike. Consider doing this trail over multiple days. North Dome goes above Alpine Zone. Please use caution.",
"2.80"
我希望根据我的GET方法所产生的结果,有一个类似于表格的输出,有7个列字段和X行数。我认为这个问题与创建的JSON文件的格式有关,但我还没能调试出来。