我正在尝试在烧瓶和蟒蛇中做一个安静的api。这是我的代码:
from flask import Flask, jsonify, request, render_template
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)
class Tracks(Resource):
@app.route('/')
def get(self):
test = {
"name": "json2html",
"description": "Converts JSON to HTML tabular representation"
}
return jsonify(test)
api.add_resource(Tracks, '/tracks')
if __name__ == '__main__':
app.run(port='5002')
我的问题是如何修改它以便能够在表格中显示我的返回值并添加例如一些CSS。谢谢!
答案 0 :(得分:2)
返回以下内容:
return Response(render_template('test.html', result=test, mimetype='text/html'))
并在test.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<table>
{% for key, value in result.iteritems() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
</body>
</html>