Flask动态数据更新,无需重新加载页面 [英] Flask Dynamic data update without reload page

查看:319
本文介绍了Flask动态数据更新,无需重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建类似Google的建议工具"(通过Recommendation api http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q=query )

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 )

我正在监听输入更改,然后将数据发送到GET:

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
      }
    });

之后,我要处理这些数据并将其发送到Google API,并在Python中得到响应:

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)

在Jinja中,尝试动态地将其打印为HTML:

In Jinja trying to print it in HTML dynamically:

        <label id="value_lable">


            {% for suggestion in suggestions_list %}
                {{ suggestion }}
            {% endfor %}

        </label>

但是Jinja中的变量不会动态更新,并且不会打印空白列表.

But variable in Jinja doesn't update dynamically and print empty list.

如何在HTML中动态打印列表中的建议?

How to print suggestions from list dynamically in HTML?

推荐答案

工作示例:

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

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

suggestions.html

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

这篇关于Flask动态数据更新,无需重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆