将列表从python传递给js使用瓶子的最佳方式是什么? [英] What would be the best way to pass a list from python to js using bottle?

查看:134
本文介绍了将列表从python传递给js使用瓶子的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我正在做{{myList}}时,瓶子转义为单独的列表中的字符串的引号,并将它们显示为'
JS反过来对于获取的内容不是非常满意。 >

我设法找到一个解决方案,但我不认为这是一个最佳的解决方案。

  var tempList ='{{eval(myList)}}'。replace(/' / g,'); 
var myNewList = eval(tempList);

我想知道,有没有更好的方法?



upd:我把我找到的解决方案转移到'Answers'部分。

解决方案

我开始使用json(json_dumps在Python3中,simplejson不会安装),但是瓶仍然转义为单引号。我发现在瓶手册中,您可以使用感叹号跳过转义,并更改了我的代码:

  var myNewList = {{!myList }}; 


I am using Bottle as a web server and need to pass a python list to javascript.

When I am doing just {{myList}}, Bottle escapes single quotes for strings in the list and shows them as ' JS, in turn, isn't very happy with what it gets.

I managed to find a solution, but I don't think it's an optimal one.

var tempList = '{{eval(myList)}}'.replace(/'/g, "'");
var myNewList = eval(tempList);

I wonder, is there a better way to do this?

upd: I moved the solution I found into the 'Answers' section.

解决方案

I started using json (json_dumps in Python3, simplejson won't install), but bottle was still escaping single quotes. I found in Bottle manual that you can skip escaping using the exclamation sign and changed my code:

var myNewList = {{!myList}};

这篇关于将列表从python传递给js使用瓶子的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆