我使用 Flask 创建了一个搜索引擎,它返回从与健康主题相关的文章生成的维基百科语料库的搜索结果。有些查询返回数百个结果,因此我想添加一个功能,将结果拆分为多个页面。下面是生成网页的index.html
代码:
{% extends "base.html" %} {% block title %}Search Page{% endblock %} {% block contents %} <div class="container"> <div class="row"> <div class="col-lg-12"> <div class="search-result-box card-box"> <div class="row"> <div class="col-md-8 offset-md-2"> <div class="pt-3 pb-4"> <div class="search-form"> <form action="#" method="POST"> <div class="input-group"> <input type="text" name="msg" class="form-control input-lg"> <div class="input-group-btn"> <button class="btn btn-primary" type="submit">Search</button> </div> </div> </form> </div> <div class="mt-4 text-center"><h4>Search Results For {{user_query}}</h4></div> </div> </div> </div> <!-- end row --> <ul class="nav nav-tabs tabs-bordered"> <li class="nav-item"><a href="#home" data-toggle="tab" aria-expanded="true" class="nav-link active">All results <span class="badge badge-success ml-1">{{search_results_list|length}}</span></a></li> </ul> <div class="tab-content"> <div class="tab-pane active" id="home"> <div class="row"> <div class="col-md-12"> <div class="search-item"> {% if search_results_list|length > 0 %} {% for r in search_results_list %} <div class="font-13 text-success mb-3"><a href='{{r[0]}}' target="_blank">{{r[0]}}</a></div> <div class="font-13 text-success mb-3"><p target="_blank">{{r[1]}}</p></div> {% endfor %} {% else %} <p class="mt-4 text-center">No search result</P> {% endif %} </div> <ul class="pagination justify-content-end pagination-split mt-0"> <li class="page-item"><a class="page-link" href="#" aria-label="Previous"><span aria-hidden="true">«</span> <span class="sr-only">Previous</span></a></li> <li class="page-item active"><a class="page-link" href="#">1</a></li> <li class="page-item"><a class="page-link" href="#" aria-label="Next"><span aria-hidden="true">»</span> <span class="sr-only">Next</span></a></li> </ul> <div class="clearfix"></div> </div> </div> </div> <!-- end All results tab --> </div> </div> </div> </div> <!-- end row --> </div> <!-- Footer --> <footer id="main-footer" class="pt-2 py-4 bg-dark text-white text-center"> Copyright © <span class="year"></span> Team Dream </footer> <!-- container --> {% endblock %}
我没有太多使用 HTML 创建网页的经验,所以我不完全确定如何添加此功能。这里还有生成应用程序的 Python 代码:
from flask import Flask, render_template, request from search_engine import query_prep, OkapiBM25 import pickle app = Flask(__name__) @app.route('/') def results(): return render_template('index.html') @app.route('/', methods=['POST']) def process_res(): with open("inv_index.pickle", "rb") as file: inv_ind = pickle.load(file) user_search_query = request.form['msg'] queries = {'q': query_prep(user_search_query)} ranking = OkapiBM25(inv_ind, queries)['q'] seen = set() newRes= [] myDict = pickle.load(open('text_summaries.pickle','rb')) for r in ranking: newRes.append((r[1], myDict[r[1]])) return render_template('index.html', search_results_list = newRes, user_query=user_search_query) if __name__ == "__main__": app.run(debug=True)
该代码使用语料库的倒排索引打开一个 pickle 文件,然后针对该索引运行查询,对结果进行排名并返回它们。每个搜索结果下方还显示每个维基百科文章的文本摘要。如果您想尝试一下并看看我正在尝试做什么,这里是网页的部署版本:https://searchenginecapstone.herokuapp.com/
如果您搜索“健康”,您会看到一页上有 442 个结果。我想调整代码(我认为这将仅在 HTML 部分中完成 - 但不完全确定)以将结果分成 10-20 个块。页面右下角已经有一个用于页面的按钮,但它目前没有任何作用。我感谢您提供的任何帮助或想法。如果您还需要从编码方面查看其他内容来回答这个问题,请告诉我。
我发现 Flask 对 paginate()
方法的分页有本机支持,因此如果我使用以下内容修改 render_template()
中返回的内容:
from flask_paginate import Pagination, get_page_parameter def process_res(): with open("inv_index.pickle", "rb") as file: inv_ind = pickle.load(file) user_search_query = request.form['msg'] queries = {'q': query_prep(user_search_query)} ranking = OkapiBM25(inv_ind, queries)['q'] seen = set() newRes= [] myDict = pickle.load(open('text_summaries.pickle','rb')) for r in ranking: newRes.append((r[1], myDict[r[1]])) page = request.args.get(get_page_parameter(), type=int, default=1) pagination = Pagination(page=page, total=len(newRes), search=user_search_query, record_name='Search Results') return render_template('index.html', search_results_list = newRes, user_query=user_search_query, Pagination = pagination)
该页面似乎可以工作(没有出现分页),所以我想我必须更新索引文件以反映分页更改,但我不知道如何。
如果您使用 Flask-SQLAlchemy 扩展,则可以使用 paginate() 方法将搜索引擎结果拆分为不同的页面。