-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
48 lines (40 loc) · 1.48 KB
/
app.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
from flask import Flask, render_template, request, send_from_directory
import subprocess
import os
app = Flask(__name__)
html_directory = 'files'
def search_results(matching_documents):
docs_data = []
for document in matching_documents[:-1]:
split1 = document.split('\t')
split2 = [a.split(':') for a in split1]
data = [a[1].strip() for a in split2]
docs_data.append(data)
return docs_data
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search', methods=['POST'])
def search():
query = request.form['query']
searched = False
try:
#calling query processor script and capture its output
result = subprocess.check_output(['python3', 'query.py', '-q', query, '-d', 'invertedfiles'])
matching_documents = result.decode('utf-8').split('\n')
matching_documents = search_results(matching_documents)
searched = True
except Exception as e:
print(f'Error: {str(e)}')
matching_documents = []
return render_template('index.html',query=query, matching_documents=matching_documents, searched = searched)
@app.route('/open_file/<filename>')
def open_file(filename):
# Ensure the requested file exists in the "files" directory
file_path = os.path.join(html_directory,filename)
if os.path.isfile(file_path):
return send_from_directory(html_directory, filename)
else:
return "File not found"
if __name__ == '__main__':
app.run()