i'm trying to create something like Google Suggest Tool (via suggest api http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q=query )
I'm listening input changes, and send data go GET:
$("#search_form_input").keyup(function(){ var some_var = $(this).val(); $.ajax({ url: "", type: "get", //send it through get method data:{jsdata: some_var}, success: function(response) { }, error: function(xhr) { //Do Something to handle error } }); After that i'm handling this data and send it to Google API and got response in Python:
@app.route('/', methods=['GET', 'POST']) def start_page_data(): query_for_suggest = request.args.get('jsdata') if query_for_suggest == None: suggestions_list = ['',] pass else: suggestions_list = [] r = requests.get('http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q={}&gl=in'.format(query_for_suggest), 'lxml') soup = BeautifulSoup(r.content) suggestions = soup.find_all('suggestion') for suggestion in suggestions: suggestions_list.append(suggestion.attrs['data']) print(suggestions_list) return render_template('start_page.html', suggestions_list=suggestions_list) In Jinja trying to print it in HTML dynamically:
<label id="value_lable"> {% for suggestion in suggestions_list %} {{ suggestion }} {% endfor %} </label> But variable in Jinja doesn't update dynamically and print empty list.
How to print suggestions from list dynamically in HTML?
success:function is empty so you do nothing with data from Flask.successfunction you have to find element in HTML - using$(...)- and then you can update it - using data which you have inresponse.