0

I have a javascript code that obtains the value of several checked boxes and inserts their value (integers) into an array:

var my_list = $('.item:checked').map(function(){return $(this).attr('name');}).get();

I want to pass this javascript array to a python function as a paramter. This is how I am doing it right now, with ajax and jQuery:

$.ajax({
    url : "{{tg.url('/foo/bar')}}",
    data : {
            my_list: JSON.stringify(my_list),
        ...
    },

On the python side, I have this code, to get the parameters:

item_list = get_paramw(kw, 'my_list', unicode)

This works, but I am not receiving the array as an array of integers, but as a single string containing the "[", "]", "," and """ symbols, which I would have to parse and isn't the most elegant way of dealing with this whole situation I think.

How should it be done to receive a javascript array of integers as an array of integers in python (not as a string)?

4
  • 1
    I think that it is the most elegant way of dealing with that situation. The step you need is to parse the JSON in the python side: pymotw.com/2/json Commented Aug 16, 2013 at 23:36
  • What framework/interface are you using to run this Python code? It might already have functionality to handle JSON built-in. If not… then it's just a call to json.parse, as acdcjunior suggests. Commented Aug 16, 2013 at 23:39
  • I am using the TurboGears 2.2.2 framework. Commented Aug 16, 2013 at 23:41
  • OK, I've never used TurboGears. You may want to look through the docs or ask around on a TurboGears forum to see if it has any JSON functionality. But meanwhile, I can write you a generic answer. Commented Aug 16, 2013 at 23:45

2 Answers 2

3

The easiest way to send simple arbitrarily-structured data from Javascript to Python is JSON. You've already got the Javascript side of it, in that JSON.stringify(my_list). All you need is the Python side. And it's one line of code:

item_list_json = get_paramw(kw, 'my_list', unicode)
item_list = json.loads(item_list_json)

If item_list in your JS code were the Javascript array of numbers [1, 2, 3, 4], item_list in your Python code would be the Python list of ints [1, 2, 3, 4].

(Well, you also have to import json at the top of your script, so I guess it's two lines.)

Sign up to request clarification or add additional context in comments.

Comments

1

The proper way to send multiple values for one key is

data: {
    my_list: my_list,
},
traditional: true
...

Suppose my_list = [ 1, 2, 3 ], this results a query string / POST data

my_list=1&my_list=2&my_list=3

Without the traditional flag, the result would be

my_list[]=1&my_list[]=2&my_list[]=3

Which of course works, but in TurboGears you need to access it as my_list[].

On the other hand, I am not sure if the TG keyword argument passing work with multiple values; if you get a single value in kw, you can use request.getall('foo') to return all the foo keys as a list (possibly an empty list).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.