简体   繁体   English

使用Python中的Tornado模块以handontable方式呈现自定义数据

[英]Rendering custom data in handsontable with the Tornado module in python

I am struggling to move data from my tornado webserver to a javascript handsontable. 我正在努力将数据从龙卷风Web服务器移动到javascript handontable。 I think the problem is related to escaping or encoding my data properly, but I can't figure it out. 我认为问题与转义或正确编码数据有关,但我无法弄清楚。

Here's the python code. 这是python代码。 I am taking a list of lists and encoding it as json. 我正在获取列表列表并将其编码为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")

Here's the handsontable code. 这是动手操作的代码。 I'd like the table to populate with the data I defined in my python function. 我希望表格中填充我在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>

The browser console indicates that the the data successfully was passed to the html page, but it looks like the javascript doesn't like the input. 浏览器控制台指示已成功将数据传递到html页面,但看起来javascript不喜欢输入。 I think I need to escape the {{data}} differently? 我想我需要以不同的方式逃避{{data}}吗?

<body><div id="example1"></div>
<script>
var
data1 = [[&quot;&quot;, &quot;Tesla&quot;, &quot;Nissan&quot;, &quot;Toyota&quot;, &quot;Honda&quot;, &quot;Mazda&quot;, &quot;Ford&quot;], [&quot;2017&quot;, 10, 11, 12, 13, 15, 16], [&quot;2018&quot;, 10, 11, 12, 13, 15, 16], [&quot;2019&quot;, 10, 11, 12, 13, 15, 16], [&quot;2020&quot;, 10, 11, 12, 13, 15, 16], [&quot;2021&quot;, 10, 11, 12, 13, 15, 16]],
container1 = document.getElementById('example1'),
settings1 = {
data: data1
},
hot1;
hot1 = new Handsontable(container1, settings1);
hot1.render();
</script>

By default Tornado "autoescapes" quotes. 默认情况下,龙卷风使用“自动转义”引号。 You can render it correctly using raw function 您可以使用raw函数正确渲染它

<div id="example1"></div>

<script>
    var
    data1 = {% raw data %},
    ...

The other solution is to pass object (not json) to render and in the template use json_encode . 另一种解决方案是传递对象(不是json)进行渲染,并在模板中使用json_encode It looks cleaner I think, but it is not the case if you already have json string (eg. you received from other source) or you have large json and you want to use different(faster) implementation (like rapidjson, ujson) of json than json_encode . 我认为它看起来更干净,但是如果您已经有json字符串(例如,从其他来源收到)或拥有较大的json并想使用json的其他(更快)实现(例如Rapidjson,ujson),则情况并非如此比json_encode

More info http://www.tornadoweb.org/en/stable/template.html#syntax-reference 更多信息http://www.tornadoweb.org/en/stable/template.html#syntax-reference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM