-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathweb_ui.py
More file actions
68 lines (55 loc) · 2.34 KB
/
Copy pathweb_ui.py
File metadata and controls
68 lines (55 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from flask import Flask, render_template, redirect, url_for, request, abort
from flask_bootstrap import Bootstrap
from flask_wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from indexer import Searcher, ShelveIndexes
from lang_proc import to_query_terms
import logging
import cgi
from datetime import datetime
import os
import workaround # NOQA
searcher = Searcher(os.environ["INDEXES_DIR"], ShelveIndexes)
app = Flask(__name__)
app.logger.setLevel(logging.DEBUG)
Bootstrap(app)
def url_for_other_page(page):
args = request.view_args.copy()
args['page'] = page
return url_for(request.endpoint, **args)
app.jinja_env.globals['url_for_other_page'] = url_for_other_page
class SearchForm(Form):
user_query = StringField('Query', validators=[DataRequired()])
search_button = SubmitField("Search!")
@app.route("/", methods=["GET", "POST"])
def index():
search_form = SearchForm(csrf_enabled=False)
if search_form.validate_on_submit():
return redirect(url_for("search_results", query=search_form.user_query.data))
return render_template("index.html", form=search_form)
@app.route("/search_results/<query>", defaults={'page': 1})
@app.route("/search_results/<query>/<int:page>")
def search_results(query, page):
start_time = datetime.now()
query_terms = to_query_terms(query)
app.logger.info("Requested [{}]".format(" ".join(map(str, query_terms))))
page_size = 25
search_results = searcher.find_documents_and_rank_by_points(query_terms)
docids = search_results.get_page(page, page_size)
pagination = search_results.get_pagination(page, page_size)
if page > pagination.pages:
abort(404)
docs = []
for docid in docids:
docs.append((searcher.indexes.get_url(docid), searcher.generate_snippet(query_terms, docid), searcher.indexes.get_title(docid)))
finish_time = datetime.now()
return render_template("search_results.html",
processing_time=(finish_time-start_time),
offset=((page-1)*page_size),
total_doc_num=search_results.total_doc_num(),
pagination=pagination,
query=cgi.escape(query),
docs=docs)
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')