2014-06-19 93 views
1

Twitter的風格滾動負載我有一個搜索頁面將通過一個靜態文件搜索和抓住一些信息拋出一個頭版。以下是我如何管理創建搜索的算法。的燒瓶stream_with_context頁面

@search.route('/') 
def properties_search(): 
    if len(request.args) > 0: 
     d = CSVReader() 
     row_count = len(list(search_csv(d, request.args))) 
     gen = stream_with_context(search_csv(d, request.args)) 
     return Response(
      stream_with_context(
       stream_template(
        'advanced_search/results.html', 
        rows=gen 
       ) 
      ) 
     ) 
    return render_template('advanced_search/advanced.html') 

def search_csv(rows, form): 
    for row in rows: 
     if satisfies_all_requirements(row, form): 
      yield row 

但是,當這個命中頁面時,它會繼續渲染每一個結果而不停止。

<div class="results"> 
    {% for each in rows %} 
    {# blah blah some html goes here, you get the point #} 
    {% endfor %} 
</div> 

如何啓用無限滾動此模板,以便它不會在第一次打擊時呈現每個單獨的結果?

+1

是你能解決問題了嗎?我遇到了同樣的問題。還是你使用大衛的方法? – user805981

+0

這是相當一段時間以前,但我最終做了長期的工作是設置可發送數據量的限制,並使用pg-query-stream將其寫入套接字用JavaScript來回應客戶端 – corvid

回答

4

這是您的流響應一個單獨的概念。相反,創建一個返回另一塊數據(可能是JSON格式)的路由,並在頁面滾動到底部並適當插入數據時,在客戶端使用JavaScript調用此路由。

@app.route('/') 
def index(): 
    # this page just loads the start page containing the javascript that will load more results 
    # you could pre-load the first set of data so the page renders something at first 
    data = get_some_data() 
    return render_template('index.html', data=data) 

@app.route('/more') 
def more(): 
    # this route will only be called from JavaScript when the page is scrolled 

    # read query parameters to know what data to get 
    page = request.args.get('page', 1) 
    per_page = request.args.get('per_page', 20) 

    # get the requested set of data 
    data = get_some_data(page, per_page) 

    # return it as json 
    return jsonify(data=format_data_appropriate_for_jsonify(data))