6

I have a function in flask called array that takes in a list and prints out the items in the list:

def array(list):
    string = ""
    for x in list:
        string+= x
    return string

On the client side, I want to pass in a javascript array named str into this array. How would I do that? Here's what I have right now, but Flask isn't reading the added variable. Any ideas?

for (var i = 0; i < response.data.length; i++) {
            console.log(i);

            // str = str + "<br/><b>Pic</b> : <img src='"+ response.data[i].picture +"'/>";

            str[i] = response.data[i].picture;
        }
        window.location = "{{ url_for('array', str=list ) }}";
Aloke Desai
  • 1,029
  • 8
  • 17
  • 27
  • I'm kind of clueless in this area but this might help you. http://stackoverflow.com/questions/15537254/passing-data-from-javascript-into-flask – Shashank Sep 14 '13 at 00:11
  • Also http://stackoverflow.com/questions/14778167/return-data-from-html-js-to-python/14778465#14778465 – Shashank Sep 14 '13 at 00:11
  • you'd like for list to be transformed into a query string and appended to the URL there in the last line? – JAL Sep 14 '13 at 00:23
  • @JAL that's correct, none of the links Shashank posted seemed to show how to do this. Any ideas? – Aloke Desai Sep 14 '13 at 01:32
  • okay, so it needs to be in the name=val& sort of format. I've done this in django and PHP but not flask... but anyway, it's called query string encoding. – JAL Sep 14 '13 at 02:41
  • 1
    Oh yeah, you can do this in urllib of Python. http://stackoverflow.com/questions/5607551/python-urlencode-string – JAL Sep 14 '13 at 02:42

1 Answers1

9

Flask has a built in object called request. In request there is a multidict called args.

You can use request.args.get('key') to retrieve the value of a query string.

from flask import request

@app.route('/example')
def example():
    # here we want to get the value of the key (i.e. ?key=value)
    value = request.args.get('key')

Of course this requires a get request (if you use a post then use request.form). On the javascript side you can make a get request using pure javascript or jquery. I'm going to use jquery in my example.

$.get(
    url="example",
    data={key:value}, 
    success=function(data) {
       alert('page content: ' + data);
    }
);

This is how you pass data from the client into flask. The function portion of the jquery code is how you pass data from flask to jquery. Let's say for example you have a view called /example and from the jquery side you pass in a key value pair of "list_name":"example_name"

from flask import jsonify
def array(list):
    string = ""
    for x in list:
        string+= x
    return string

@app.route("/example")
def example():
    list_name = request.args.get("list_name")
    list = get_list(list_name) #I don't know where you're getting your data from, humor me.
    array(list)
    return jsonify("list"=list) 

and in the success function in jquery you'd say

  success=function(data) {
       parsed_data = JSON.parse(data)
       alert('page content: ' + parsed_data);
    }

Note that flask does not allow for top level lists in json response for security reasons.

Random Nerd
  • 134
  • 1
  • 9
AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103
  • In case of POST, the data on the flask side may be in `request.json` when sent as json. – Mahdi Jun 17 '16 at 14:17