How to pass javascript array to python script using flask [using flash drive example]

I am trying to get a flask / jquery / ajax example working for my specific case, but every time I get short. I know that this question has been asked several times, but the answers do not help me (yes, I'm new to this).

The example passes a string from javascript to python. I would like to pass an array. The network suggests this is possible. Here is what I have:

HTML / Flask Template:

{% extends "layout.html" %} {% block title %}Test{% endblock %} {% block content %} <div> <h1>Flask Jquery Test</h1> <div> <input type="button" value="Transfer" id="button" /> </div> <div> Wordlist<br /> <select multiple="multiple" id="wordlist" size="5"> <option>Volvo</option> <option>Audi</option> <option>BMW</option> <option>Mercedes</option> <option>Toyota</option> </select> <span id="result"></span> </div> </div> {% endblock %} 

JS Script:

 $(document).ready(function() { $("#button").bind('click', function(){ //Get all words from list var list = []; $("#wordlist option").each(function(){ list.push($(this).val()); }); //var list = $( "#wordlist option" ).val(); console.log(list); $.getJSON($SCRIPT_ROOT + '/_array2python', { wordlist: list.toString() }, function(data){ console.log(data.result) $( "#result" ).text(data.result); }); return false; }); }); 

Python:

 @app.route('/') def start_page(): return render_template('index.html') @app.route('/_array2python') def array2python(): wordlist = request.args.get('wordlist', []) return jsonify(result=wordlist) @app.errorhandler(404) def page_not_found(e): """Return a custom 404 error.""" return 'Sorry, nothing at this URL.', 404 

Now, going into the list variable a line (for example, var list = $( "#wordlist option" ).val(); ), this code works fine. However, when you try to use it with an array, it only ever passes a fallback value (ie [] ).

Does this mean that I can only pass strings in python? What is the best way to pass a javascript array in python?

Thank you all for your help!

PS Maybe it's important to mention. I use the Google engine to host this code.

==================================================== =================================

FYI, these are the SO sites I tried to track, and they did not help me:

Passing Javascript Array to Flask (Great and very detailed answer, but it can't make it work)

Passing data from javascript to Flask

Return data from html / js in python

+6
source share
1 answer

Here is another way to pass a JS array to python:

JS Side:

 $.getJSON($SCRIPT_ROOT + '/_array2python', { wordlist: JSON.stringify(list) }, function(data){ console.log(data.result) $( "#result" ).text(data.result); }); 

Python side:

 import json @app.route('/_array2python') def array2python(): wordlist = json.loads(request.args.get('wordlist')) # do some stuff return jsonify(result=wordlist) 

As far as I know, strings are the only way to pass an array from client to server.

+13
source

Source: https://habr.com/ru/post/970068/


All Articles