Issue
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. It is of the format {"John":"Yes","David":"No"} format. But I need to convert this JSON into a HTML table and render it as a html template. 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.
Solution
First import render_template_string
from flask by adding from flask import render_template_string
in the script
then in the route instead of return jsonify(labels)
replace with the below,
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.
Answered By - George J Padayatti