Updated index page + Added about and result pages
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import configparser
|
||||
import csv
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
import time
|
||||
|
||||
import requests
|
||||
from flask import Flask, render_template, request, send_file
|
||||
from flask import Flask, render_template, request, send_file, redirect, url_for
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
from stark import run
|
||||
@@ -10,6 +15,7 @@ from stark import run
|
||||
app = Flask(__name__)
|
||||
UPLOAD_FOLDER = 'uploads'
|
||||
ALLOWED_EXTENSIONS = {'conllu'}
|
||||
DAYS_BEFORE_DELETION = 1
|
||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||
|
||||
|
||||
@@ -53,6 +59,7 @@ def allowed_file(filename):
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
@app.route('/upload')
|
||||
def upload_file2():
|
||||
return render_template('upload.html')
|
||||
@@ -66,14 +73,65 @@ def upload_file():
|
||||
return 'file uploaded successfully'
|
||||
|
||||
|
||||
@app.route('/about', methods=['GET'])
|
||||
def about():
|
||||
return render_template('about.html')
|
||||
|
||||
|
||||
# @app.route('/result/<result_id>/download', methods=['GET'])
|
||||
# def download_result(result_id):
|
||||
# return
|
||||
|
||||
|
||||
@app.route('/result/<result_id>', methods=['GET', 'POST'])
|
||||
def result(result_id):
|
||||
|
||||
if request.method == 'POST':
|
||||
for filename in os.listdir('media'):
|
||||
file_path = os.path.join('media', filename)
|
||||
f_t = os.path.getmtime(file_path)
|
||||
c_t = time.time()
|
||||
file_age_seconds = c_t - f_t
|
||||
if file_age_seconds > DAYS_BEFORE_DELETION * 86400:
|
||||
os.remove(file_path)
|
||||
# TODO ADD LINKS
|
||||
# TODO TEST VARYING SIZES OF TEXT IN TABLE
|
||||
return send_file(os.path.join('media', result_id), as_attachment=True, download_name='results.tsv')
|
||||
|
||||
order_by = request.args.get('order_by')
|
||||
order_type = request.args.get('order_type')
|
||||
with open(os.path.join('media', result_id), 'r') as rf:
|
||||
|
||||
content = list(csv.reader(rf, delimiter='\t'))
|
||||
head = content[0]
|
||||
content_dict = {h: [] for h in head}
|
||||
if order_by is not None and order_by[:-1] in head:
|
||||
sort_id = head.index(order_by[:-1])
|
||||
if order_type == 'asc':
|
||||
# check if a number can be converted to float or int
|
||||
ordered_content = sorted(content[1:], key=lambda x: -1 * float(x[sort_id]) if x[sort_id].isnumeric() or re.match(r'^-?\d+(?:\.\d+)$', x[sort_id]) is not None else x[sort_id], reverse=True)
|
||||
else:
|
||||
ordered_content = sorted(content[1:], key=lambda x: -1 * float(x[sort_id]) if x[sort_id].isnumeric() or re.match(r'^-?\d+(?:\.\d+)$', x[sort_id]) is not None else x[sort_id])
|
||||
else:
|
||||
ordered_content = content[1:]
|
||||
|
||||
for i, row in enumerate(ordered_content):
|
||||
for j, v in enumerate(row):
|
||||
content_dict[head[j]].append(v)
|
||||
|
||||
# content.sort(key=lambda x: x[1])
|
||||
a = request
|
||||
print(result_id)
|
||||
return render_template('result.html', head_row=head, content=content_dict)
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
if request.method == 'POST':
|
||||
form = request.form
|
||||
a = request
|
||||
configs = {}
|
||||
# mandatory parameters
|
||||
configs['input_path'] = 'data/sl_ssj-ud_v2.4.conllu'
|
||||
configs['input_path'] = ''
|
||||
validation = {}
|
||||
|
||||
|
||||
@@ -93,7 +151,8 @@ def index():
|
||||
# TODO OPTIONALLY ADD conllu FILE CHECK
|
||||
elif 'input_url' in form and form['input_url']:
|
||||
try:
|
||||
input_path = os.path.join('media', 'input.conllu')
|
||||
name = form['input_url'].split('/')[-1]
|
||||
input_path = os.path.join('media', name)
|
||||
response = requests.get(form['input_url'])
|
||||
open(input_path, "wb").write(response.content)
|
||||
configs['input_path'] = input_path
|
||||
@@ -138,10 +197,13 @@ def index():
|
||||
|
||||
return True
|
||||
|
||||
# TODO radio button (maybe checkbutton)
|
||||
node_type = []
|
||||
if 'node_type' in form:
|
||||
node_type = form['node_type']
|
||||
if 'node_type_upos' in form:
|
||||
node_type.append('upos')
|
||||
if 'node_type_form' in form:
|
||||
node_type.append('form')
|
||||
if 'node_type_lemma' in form:
|
||||
node_type.append('lemma')
|
||||
|
||||
if validate_node_type(node_type):
|
||||
configs['node_type'] = '+'.join(node_type)
|
||||
@@ -154,12 +216,13 @@ def index():
|
||||
|
||||
# TODO FINALIZE THIS!
|
||||
configs['complete_tree_type'] = True
|
||||
configs['dependency_type'] = True
|
||||
configs['node_order'] = True
|
||||
configs['association_measures'] = False
|
||||
configs['dependency_type'] = 'labeled_trees' in form and form['labeled_trees'] == 'on'
|
||||
configs['node_order'] = 'fixed_order' in form and form['fixed_order'] == 'on'
|
||||
|
||||
configs['label_whitelist'] = []
|
||||
configs['root_whitelist'] = []
|
||||
if 'root_restriction' in form and form['root_restriction']:
|
||||
configs['root_whitelist'] = form['root_restriction'].split('|')
|
||||
|
||||
configs['query'] = None
|
||||
|
||||
@@ -182,14 +245,16 @@ def index():
|
||||
|
||||
# configs = read_configs(config, args)
|
||||
|
||||
|
||||
|
||||
configs['output'] = os.path.join('media', 'result.tsv')
|
||||
configs['complete_tree_type'] = form['complete'] == 'yes'
|
||||
configs['association_measures'] = False
|
||||
configs['grew_match'] = 'grewmatch_patterns' in form and form['grewmatch_patterns'] == 'on'
|
||||
configs['depsearch'] = False
|
||||
name = ''.join(random.choices(string.ascii_uppercase + string.digits, k=60))
|
||||
configs['output'] = os.path.join('media', name)
|
||||
|
||||
run(configs)
|
||||
# TODO DELETE STORED FILE AFTER PROCESSING
|
||||
return send_file(configs['output'], as_attachment=True)
|
||||
return redirect(url_for('result', result_id=name))
|
||||
# return send_file(configs['output'], as_attachment=True)
|
||||
# return render_template('index.html')
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user