forked from moest-np/center-randomize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
84 lines (67 loc) · 3.1 KB
/
web.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from flask import Flask, render_template, request, jsonify
import os
import csv
import subprocess
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
@app.route('/autocomplete')
def autocomplete():
file_dir = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(file_dir, 'sample_data/centers_grade12_2081.tsv')
data = {}
with open(filename, 'r', newline='', encoding='utf-8') as file:
reader = csv.DictReader(file, delimiter='\t')
for row in reader:
data[row['cscode']] = row['name']
term = request.args.get('term', '')
results = {key: value for key, value in data.items() if term.lower() in value.lower()}
return jsonify(results)
@app.route('/school-exam-centers')
def school_exam_centers():
scode = request.args.get('scode',0, type=int)
data = []
if scode > 0:
#cmd = f'source venv/bin/activate python school_center.py ./sample_data/schools_grade12_2081.tsv ./sample_data/centers_grade12_2081.tsv ./sample_data/prefs.tsv -o school-center.tsv -s {scode}'
cmd = f'source venv/bin/activate python school_center.py ./sample_data/schools_grade12_2081.tsv ./sample_data/centers_grade12_2081.tsv ./sample_data/prefs.tsv -o school-center.tsv -s {scode}'
subprocess.run(cmd, shell=True, capture_output=True, text=True)
file_dir = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(file_dir, 'results/school-center.tsv')
with open(filename, 'r', newline='', encoding='utf-8') as file:
reader = csv.DictReader(file, delimiter='\t')
for row in reader:
if int(row['scode']) == scode:
data.append({'cscode': row['cscode'], 'center': row['center'], 'center_address': row['center_address']})
if len(data)>0:
rendered_template = render_template('centers.html', data=data)
return jsonify(data=rendered_template)
return jsonify(data=None)
@app.route("/exam-centers")
def exam_centers():
# Read TSV file and split into chunks
file_dir = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(file_dir, 'sample_data/centers_grade12_2081.tsv')
data = read_tsv(filename)
# Sort data by 'name' column
data.sort(key=lambda x: x['name'])
# Pagination
page = request.args.get('page', 1, type=int)
chunk_size = 10
start_index = (page - 1) * chunk_size
end_index = min(start_index + chunk_size, len(data))
chunked_data = data[start_index:end_index]
# Calculate total pages
total_pages = (len(data) + chunk_size - 1) // chunk_size
return render_template('exam_centers.html', data=chunked_data, page=page, total_pages=total_pages)
def read_tsv(file_path: str):
data = []
with open(file_path, 'r', newline='', encoding='utf-8') as file:
reader = csv.DictReader(file, delimiter='\t')
for row in reader:
data.append(dict(row))
return data
if __name__ == "__main__":
app.run(debug=True)
# for development server: $ python app.py
# from flask : flask --app app run