【发布时间】:2018-04-05 05:03:02
【问题描述】:
我正在努力将数据从我的 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")
这是可操作的代码。我希望表格中填充我在 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>
【问题讨论】:
标签: javascript python tornado handsontable