-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
55 lines (43 loc) · 1.88 KB
/
run.py
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
from flask import Flask, request, render_template, redirect, url_for
from search import search, init_url_anchor
from indexer import indexer, create_index, create_index_of_index, open_files, close_files, load_json
from collections import defaultdict
from timeit import default_timer as timer
app = Flask(__name__)
@app.route("/", methods=['POST', 'GET'])
def ask_for_input():
error = None
if request.method == 'POST':
query = request.form['search']
return redirect(url_for('results_page', query=query))
else:
error = 'Try again'
return render_template('form.html', error=error)
@app.route("/results/<query>", methods=['POST', 'GET'])
def results_page(query: str):
if request.method == 'POST':
query = request.form['search']
return redirect(url_for('results_page', query=query))
else:
search_start = timer()
search_results = search(query.strip())
search_end = timer()
tagged_urls = [f"<li><a href=\"{u}\">{u}</a></li>" for u in search_results]
url_list = "<ol>" + ''.join(tagged_urls) + "</ol>"
return f"<form method='POST'><input type=\"text\" name=\"search\" placeholder=\"{query}\"> \
<button type=\"submit\" name=\"submit_button\" >Search</button> <lb> \
</form> <br><small>({search_end - search_start} seconds)</small> {url_list}"
if __name__ == "__main__":
try:
createIndex = False #in case we do not want to create index from scratch
if createIndex:
create_index()
url_index, anchor_dict = indexer()
else:
open_files() #if we dont create index from scratch, we still want to open all of the files
url_index, anchor_dict = load_json()
create_index_of_index()
init_url_anchor(url_index, anchor_dict)
app.run()
finally:
close_files()