添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
大气的芒果  ·  Difference between ...·  2 年前    · 
失恋的白开水  ·  java ...·  2 年前    · 
憨厚的茶壶  ·  (Matlab) ...·  2 年前    · 

在Python中把JSON数据输出转换为HTML-TABLE格式 - Flask?

0 人关注

我目前在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文件的格式有关,但我还没能调试出来。

5 个评论
你在向 .convert() 传递一个元组。那是故意的吗?
你能贴出 json_data 打印出来的样子吗?
另外,我相信你可以在你的jsonify之后去掉 ,{'Content-Type':'application/json'}
这个元组并不是故意的,但如果能让它更容易操作,我愿意改变它。还有 @GageBrklacich - 我在上面上传了JSON输出的样本!
为了进一步澄清-- jsonify 函数返回一个JSON RESPONSE类型。所以我不确定这个问题是否与这里的格式有关
python
json
flask
flask-sqlalchemy
user6557421
发布于 2019-08-09
2 个回答
John Keyes
John Keyes
发布于 2021-01-29
已采纳
0 人赞同

你的 data_all 是一个列表的列表。如果你向 json2html 传递一个dicts的列表,它将为你把它格式化为一个HTML表格。

data_all = []
for item in result:
    data_all.append({
        "Name": item["trail name"],
        "Description": item["description"]

然后你就可以把这个列表传给json2html

html_data = json2html.convert(json=data_all)

由此产生的HTML将采用以下格式。

<table border="1">
<thead>
  <tr><th>Name</th><th>Description</th></tr>
</thead>
<tbody>
    <td>Upper Yosemite Falls</td>
    <td>Upper Yosemite Falls Trail ...</td>
    <td>Vernal and Nevada Falls via the Mist Trail</td>
    <td>Vernal and Nevada Falls via the Mist Trail is a 6.4 mile ...</td>
</tbody>
</table>
    
TheBroda
TheBroda
发布于 2021-01-29
0 人赞同

你可以把你的json数据传给json2html模块,然后把这个变量传给html模板。

在flask的python文件中配置了一个路由------。

@app.route('/json_html')
def json_html():
    table_data = (json2html.convert(json = json_data))
    return render_template("json_template.html", table_data=table_data)

而json_template.html可以有以下结构

<!doctype html>
</head>
      {{json_data | safe}}