0

I'm dynamically generating a list of check boxes using values from a list as follows:

return flask.jsonify(result='\n'.join('<input type="checkbox" class="av-sources">%s<br>'% src for src in my_list))

The problem I am facing is, when trying to include a list item multiple times:

For Ex:

.join('<input type="checkbox" class="av-sources" value="%s">%s<br>'% src for src in my_list))

i know src is not referenced mulitple times, i dont have it in a tuple (item1, item2) so I get the following error:

TypeError: TypeError('not enough arguments for format string',) is not JSON serializable

I dont know how to reference the string twice when using % src for src in my_list)).

I have tried switching to value="{0}">{0}<br>.format(src for src in my_list))

but the code does not iterate through my list and only returns one object

< i n p u t t y p e = " c h e c k b o x " c l a s s = " a v - s o u r c e s " v a l u e = " < g e n e r a t o r o b j e c t < g e n e x p r

a t 0 x 0 0 0 0 0 0 0 0 0 3 1 E D 9 0 0 > " > < g e n e r a t o r o b j e c t < g e n e x p r > a t 0 x 0 0 0 0 0 0 0 0 0 3 1 E D 9 0 0 > < b r >

my function is here:

@app.route("/_srcs")
def source():


    try:
        source_txt = a JSON Blob
        source_load = json.loads(source_txt)
        sources = []
        for i in source_load['sources']:
            sources.append(i['name'])

        return flask.jsonify(result='\n'.join('<input type="checkbox" class="av-sources" value="{0}">{0}<br>'.format(src for src in sources)))

    except Exception as e:
        print(e)
        return flask.jsonify(error=e)

Here is my javascript:

$(function() {
var pythonsources = function(e) {
    console.log('kibi sources');
  $.getJSON($SCRIPT_ROOT + '/_srcs', {

  }, function(data) {

    $('#sources').html(data.result);
  });
  return false;
};
$('#menu-toggle2').bind('click', pythonsources );
});

If someone can point me in the right direction, thanks

glls
  • 2,325
  • 1
  • 22
  • 39
  • Try this : http://stackoverflow.com/questions/7568627/using-python-string-formatting-with-lists – Wajahat Jul 01 '16 at 21:00

1 Answers1

3

Looks like you just have a bracket in the wrong place.

Change...

'\n'.join('<input type="checkbox" class="av-sources" value="{0}">{0}<br>'.format(src for src in sources))

...to...

'\n'.join('<input type="checkbox" class="av-sources" value="{0}">{0}<br>'.format(src) for src in sources)
Aya
  • 39,884
  • 6
  • 55
  • 55