Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

javascript - Flask Dynamic data update without reload page

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Working example:

app.py

from flask import Flask, render_template, request
import requests
from bs4 import BeautifulSoup


app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/suggestions')
def suggestions():
    text = request.args.get('jsdata')

    suggestions_list = []

    if text:
        r = requests.get('http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q={}&gl=in'.format(text))

        soup = BeautifulSoup(r.content, 'lxml')

        suggestions = soup.find_all('suggestion')

        for suggestion in suggestions:
            suggestions_list.append(suggestion.attrs['data'])

        #print(suggestions_list)

    return render_template('suggestions.html', suggestions=suggestions_list)


if __name__ == '__main__':
    app.run(debug=True)

index.html

<!DOCTYPE html>

<html>

<head>
    <title>Suggestions</title>
</head>

<body>

Search: <input type="text" id="search_form_input"></input>

<div id="place_for_suggestions"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>
$("#search_form_input").keyup(function(){
    var text = $(this).val();

    $.ajax({
      url: "/suggestions",
      type: "get",
      data: {jsdata: text},
      success: function(response) {
        $("#place_for_suggestions").html(response);
      },
      error: function(xhr) {
        //Do Something to handle error
      }
    });
});
</script>

</body>

</html>

suggestions.html

<label id="value_lable">
    {% for suggestion in suggestions %}
        {{ suggestion }}<br>
    {% endfor %}
</label>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...