[英]How do I convert python JSON into a html table in Flask server?
I have installed flask in raspberry pi. 我在树莓派中安装了烧瓶。
from flask import Flask
from flask import jsonify
import os
import cv2
import numpy as np
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello world'
@app.route('/detect')
def detect():
labels = os.popen('python3 detect.py').read()
return jsonify(labels)
if __name__ == '__main__':
app.run(debug=True, port=8000, host='0.0.0.0')
in the detect() method i have converted the labels into JSON. 在detect()方法中,我已将标签转换为JSON。 It is of the format {"John":"Yes","David":"No"} format.
格式为{“ John”:“是”,“ David”:“否”}格式。 But I need to convert this JSON into a HTML table and render it as a html template.
但是我需要将此JSON转换为HTML表并将其呈现为html模板。 so that it looks like
这样看起来
Name Status
John Yes
David No
How do I achieve it?? 我该如何实现? I have seen lot of questions in StackOverflow but I don't get any correct solution for my question.
我在StackOverflow中看到了很多问题,但是我的问题没有任何正确的解决方案。
First import render_template_string
from flask by adding from flask import render_template_string
in the script 首先进口
render_template_string
通过添加瓶from flask import render_template_string
脚本
then in the route instead of return jsonify(labels)
replace with the below, 然后在路由中而不是
return jsonify(labels)
替换为以下内容,
return render_template_string('''
<table>
<tr>
<td> Name </td>
<td> Status </td>
</tr>
{% for name, status in labels.items() %}
<tr>
<td>{{ name }}</td>
<td>{{ status }}</td>
</tr>
{% endfor %}
</table>
''', labels=labels)
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.