在 python 中使用 Tornado 模块在 handsontable 中呈现自定义数据
Rendering custom data in handsontable with the Tornado module in python
我正在努力将数据从我的 tornado 网络服务器移动到 javascript handsontable。我认为问题与正确转义或编码我的数据有关,但我无法弄清楚。
这是 python 代码。我正在获取列表列表并将其编码为 json.
class hot_index(tornado.web.RequestHandler):
def get(self):
self.render("hot_tradedata.html",
data=json.dumps([
['', 'Tesla', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
['2017', 10, 11, 12, 13, 15, 16],
['2018', 10, 11, 12, 13, 15, 16],
['2019', 10, 11, 12, 13, 15, 16],
['2020', 10, 11, 12, 13, 15, 16],
['2021', 10, 11, 12, 13, 15, 16]
])
)
if __name__ == "__main__":
app = tornado.web.Application(
handlers=[(r"/hot", hot_index)],
static_path=os.path.join(os.path.dirname(__file__), "static"),
template_path=os.path.join(os.path.dirname(__file__), "templates")
这是实际操作table 代码。我希望 table 填充我在 python 函数中定义的数据。
<div id="example1"></div>
<script>
var
data1 = {{data}},
container1 = document.getElementById('example1'),
settings1 = {
data: data1
},
hot1;
hot1 = new Handsontable(container1, settings1);
hot1.render();
</script>
浏览器控制台显示数据已成功传递到 html 页面,但看起来 javascript 不喜欢输入。我想我需要以不同的方式转义 {{data}} 吗?
<body><div id="example1"></div>
<script>
var
data1 = [["", "Tesla", "Nissan", "Toyota", "Honda", "Mazda", "Ford"], ["2017", 10, 11, 12, 13, 15, 16], ["2018", 10, 11, 12, 13, 15, 16], ["2019", 10, 11, 12, 13, 15, 16], ["2020", 10, 11, 12, 13, 15, 16], ["2021", 10, 11, 12, 13, 15, 16]],
container1 = document.getElementById('example1'),
settings1 = {
data: data1
},
hot1;
hot1 = new Handsontable(container1, settings1);
hot1.render();
</script>
默认 Tornado "autoescapes" 引号。您可以使用 raw
函数
正确渲染它
<div id="example1"></div>
<script>
var
data1 = {% raw data %},
...
另一种解决方案是将对象(不是 json)传递给渲染,并在模板中使用 json_encode
。我认为它看起来更干净,但如果你已经有 json 字符串(例如,你从其他来源收到)或者你有大 json 并且你想使用不同的(更快的)实现,情况就不是这样了(像 rapidjson, ujson)json 比 json_encode
.
更多信息http://www.tornadoweb.org/en/stable/template.html#syntax-reference
我正在努力将数据从我的 tornado 网络服务器移动到 javascript handsontable。我认为问题与正确转义或编码我的数据有关,但我无法弄清楚。
这是 python 代码。我正在获取列表列表并将其编码为 json.
class hot_index(tornado.web.RequestHandler):
def get(self):
self.render("hot_tradedata.html",
data=json.dumps([
['', 'Tesla', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
['2017', 10, 11, 12, 13, 15, 16],
['2018', 10, 11, 12, 13, 15, 16],
['2019', 10, 11, 12, 13, 15, 16],
['2020', 10, 11, 12, 13, 15, 16],
['2021', 10, 11, 12, 13, 15, 16]
])
)
if __name__ == "__main__":
app = tornado.web.Application(
handlers=[(r"/hot", hot_index)],
static_path=os.path.join(os.path.dirname(__file__), "static"),
template_path=os.path.join(os.path.dirname(__file__), "templates")
这是实际操作table 代码。我希望 table 填充我在 python 函数中定义的数据。
<div id="example1"></div>
<script>
var
data1 = {{data}},
container1 = document.getElementById('example1'),
settings1 = {
data: data1
},
hot1;
hot1 = new Handsontable(container1, settings1);
hot1.render();
</script>
浏览器控制台显示数据已成功传递到 html 页面,但看起来 javascript 不喜欢输入。我想我需要以不同的方式转义 {{data}} 吗?
<body><div id="example1"></div>
<script>
var
data1 = [["", "Tesla", "Nissan", "Toyota", "Honda", "Mazda", "Ford"], ["2017", 10, 11, 12, 13, 15, 16], ["2018", 10, 11, 12, 13, 15, 16], ["2019", 10, 11, 12, 13, 15, 16], ["2020", 10, 11, 12, 13, 15, 16], ["2021", 10, 11, 12, 13, 15, 16]],
container1 = document.getElementById('example1'),
settings1 = {
data: data1
},
hot1;
hot1 = new Handsontable(container1, settings1);
hot1.render();
</script>
默认 Tornado "autoescapes" 引号。您可以使用 raw
函数
<div id="example1"></div>
<script>
var
data1 = {% raw data %},
...
另一种解决方案是将对象(不是 json)传递给渲染,并在模板中使用 json_encode
。我认为它看起来更干净,但如果你已经有 json 字符串(例如,你从其他来源收到)或者你有大 json 并且你想使用不同的(更快的)实现,情况就不是这样了(像 rapidjson, ujson)json 比 json_encode
.
更多信息http://www.tornadoweb.org/en/stable/template.html#syntax-reference