Updated visuals and expanded website.

This commit is contained in:
2024-02-19 15:33:19 +01:00
parent 42a3f773a9
commit 71de08ffed
18 changed files with 995 additions and 78 deletions
+53 -20
View File
@@ -8,7 +8,7 @@ import time
import requests
from flask import Flask, render_template, request, send_file, redirect, url_for
from flask_headers import headers
from flask_babel import Babel, gettext
from werkzeug.utils import secure_filename
from stark import run
@@ -27,10 +27,45 @@ TABLE_COLUMNS2DISPLAYED_TABLE_COLUMNS = {
't-score': 't-score'
}
DISPLAYED_TABLE_COLUMNS2TABLE_COLUMNS = {v: k for k, v in TABLE_COLUMNS2DISPLAYED_TABLE_COLUMNS.items()}
DEFAULT_LANGUAGE = 'en'
LANGUAGES = ['en', 'sl']
_translations = {
'en': {
'hello': 'Hello',
'welcome': 'Welcome',
'greeting': 'How are you?',
'name': 'Your name:',
'code': 'en',
'switch_code': 'SL',
'switch_link': '?lang=sl',
},
'sl': {
'hello': 'Hola',
'welcome': 'Bienvenido',
'greeting': '¿Cómo estás?',
'name': 'Tu nombre:',
'code': 'sl',
'switch_code': 'EN',
'switch_link': '?lang=en',
},
}
def get_locale():
lang = request.args.get('lang')
if lang in LANGUAGES:
return lang
return DEFAULT_LANGUAGE
def create_app():
app = Flask(__name__)
babel = Babel(app, locale_selector=get_locale, default_translation_directories='translations')
babel.list_translations = ['en', 'sl']
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def create_default_configs():
@@ -137,6 +172,7 @@ def create_app():
@app.route('/', methods=['GET', 'POST'])
# @headers({'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'})
def index():
translations = _translations[get_locale()]
if request.method == 'POST':
form = request.form
configs = {}
@@ -156,8 +192,8 @@ def create_app():
configs['input_path'] = input_path
if 'input_url' in form and form['input_url']:
validation['file'] = 'Please insert either input url or file, not both of them.'
validation['input_url'] = 'Please insert either input url or file, not both of them.'
validation['file'] = gettext('Please insert either input url or file, not both of them.')
validation['input_url'] = gettext('Please insert either input url or file, not both of them.')
# TODO OPTIONALLY ADD conllu FILE CHECK
elif 'input_url' in form and form['input_url']:
try:
@@ -167,10 +203,10 @@ def create_app():
open(input_path, "wb").write(response.content)
configs['input_path'] = input_path
except:
validation['input_url'] = 'Incorrect URL!'
validation['input_url'] = gettext('Incorrect URL!')
else:
validation['file'] = 'Please insert either input url or provide a file.'
validation['input_url'] = 'Please insert either input url or provide a file.'
validation['file'] = gettext('Please insert either input url or provide a file.')
validation['input_url'] = gettext('Please insert either input url or provide a file.')
tree_size_min = None
if 'tree_size_min' in form:
@@ -182,11 +218,11 @@ def create_app():
def validate_tree_size(tree_size_min, tree_size_max):
if tree_size_min is None or tree_size_max is None:
validation['tree_size'] = 'Please provide information about minimum and maximum tree size.'
validation['tree_size'] = gettext('Please provide information about minimum and maximum tree size.')
return False
if int(tree_size_min) > int(tree_size_max):
validation['tree_size'] = 'Tree size minimum should be smaller than tree size maximum.'
validation['tree_size'] = gettext('Tree size minimum should be smaller than tree size maximum.')
return False
return True
@@ -197,12 +233,12 @@ def create_app():
# TODO EXPAND NODE TYPE
node_type_options = {'upos', 'form', 'lemma', 'upos', 'xpos', 'feats', 'deprel'}
if len(node_type) == 0:
validation['node_type'] = 'Please select at least one node type.'
validation['node_type'] = gettext('Please select at least one node type.')
return False
for el in node_type:
if el not in node_type_options:
validation['node_type'] = f'Node option {el} is not supported. Please enter valid options.'
validation['node_type'] = gettext('Node option') + f' {el} ' + gettext('is not supported. Please enter valid options.')
return False
return True
@@ -246,7 +282,7 @@ def create_app():
try:
int(form['frequency_threshold'])
except ValueError:
validation['frequency_threshold'] = f'Please insert an Integer.'
validation['frequency_threshold'] = gettext('Please insert an Integer.')
else:
configs['frequency_threshold'] = int(form['frequency_threshold'])
@@ -266,24 +302,21 @@ def create_app():
name = ''.join(random.choices(string.ascii_uppercase + string.digits, k=60))
configs['output'] = os.path.join('media', name)
if len(validation) > 0:
a = request.args.get('noreload')
b = request.args
c = request
return render_template('index.html', validation=validation)
return render_template('index.html', validation=validation, translations=translations)
try:
run(configs)
except Exception as e:
validation['general'] = 'Processing failed! Please recheck your settings, e.g. input format or head node description.'
validation['general'] = gettext('Processing failed! Please recheck your settings, e.g. input format or head node description.')
if len(validation) > 0:
return render_template('index.html', validation=validation)
return render_template('index.html', validation=validation, translations=translations)
# check if there are no results
with open(os.path.join('media', name), 'r') as rf:
content = list(csv.reader(rf, delimiter='\t'))
if len(content) == 1:
validation['results'] = False
return render_template('index.html', validation=validation)
return redirect(url_for('result', result_id=name, order_by='Frequency ', order_type='desc'))
return render_template('index.html')
return render_template('index.html', validation=validation, translations=translations)
return redirect(url_for('result', result_id=name, order_by='Frequency ', order_type='desc', lang=gettext('code')))
return render_template('index.html', translations=translations)
return app