Unify service pages.

This commit is contained in:
Patrick Jentsch 2019-09-24 16:55:24 +02:00
parent a267de0867
commit 787bf4c377
5 changed files with 80 additions and 117 deletions

View File

@ -1,4 +1,4 @@
from flask import current_app, flash, redirect, render_template, url_for
from flask import abort, current_app, flash, redirect, render_template, url_for
from . import services
from flask_login import current_user, login_required
from .forms import NewOCRJobForm, NewNLPJobForm
@ -8,93 +8,56 @@ import json
import os
@services.route('/nlp', methods=['GET', 'POST'])
SERVICE_REQUIREMENTS = {'nlp': {'mem_mb': 4096, 'n_cores': 2},
'ocr': {'mem_mb': 8192, 'n_cores': 4}}
@services.route('/<service_handle>', methods=['GET', 'POST'])
@login_required
def nlp():
new_nlp_job_form = NewNLPJobForm()
if new_nlp_job_form.validate_on_submit():
nlp_job = Job(creator=current_user,
description=new_nlp_job_form.description.data,
mem_mb=4096,
n_cores=2,
service='nlp',
service_args=json.dumps(
['-l {}'.format(new_nlp_job_form.language.data)]
),
service_version=new_nlp_job_form.version.data,
def service(service_handle):
if service_handle not in ['nlp', 'ocr']:
abort(404)
elif service_handle == 'nlp':
page_title = 'Natural Language Processing'
new_job_form = NewNLPJobForm()
elif service_handle == 'ocr':
page_title = 'Optical Character Recognition'
new_job_form = NewOCRJobForm()
if new_job_form.validate_on_submit():
_service_args = ['-l {}'.format(new_job_form.language.data)]
if service_handle == 'ocr':
if not new_job_form.binarization.data:
_service_args.append('--skip-binarisation')
job = Job(
creator=current_user,
description=new_job_form.description.data,
mem_mb=SERVICE_REQUIREMENTS[service_handle]['mem_mb'],
n_cores=SERVICE_REQUIREMENTS[service_handle]['n_cores'],
service=service_handle,
service_args=json.dumps(_service_args),
service_version=new_job_form.version.data,
status='preparing',
title=new_nlp_job_form.title.data)
db.session.add(nlp_job)
title=new_job_form.title.data
)
db.session.add(job)
db.session.commit()
dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
str(nlp_job.user_id),
str(job.user_id),
'jobs',
str(nlp_job.id))
str(job.id))
try:
os.makedirs(dir)
except OSError:
flash('OSError!')
db.session.remove(nlp_job)
db.session.remove(job)
db.session.commit()
else:
for file in new_nlp_job_form.files.data:
for file in new_job_form.files.data:
file.save(os.path.join(dir, file.filename))
nlp_job.status = 'submitted'
job.status = 'submitted'
db.session.commit()
flash('Job created!')
return redirect(url_for('services.nlp'))
return render_template('services/nlp.html.j2',
title='Natrual Language Processing',
new_nlp_job_form=new_nlp_job_form)
@services.route('/ocr', methods=['GET', 'POST'])
@login_required
def ocr():
new_ocr_job_form = NewOCRJobForm()
if new_ocr_job_form.validate_on_submit():
ocr_job = Job(creator=current_user,
description=new_ocr_job_form.description.data,
mem_mb=8192,
n_cores=4,
service='ocr',
service_args=json.dumps(
[
'-l {}'.format(new_ocr_job_form.language.data),
'' if new_ocr_job_form.binarization.data
else '--skip-binarisation'
]
),
service_version=new_ocr_job_form.version.data,
status='preparing',
title=new_ocr_job_form.title.data)
db.session.add(ocr_job)
db.session.commit()
dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
str(ocr_job.user_id),
'jobs',
str(ocr_job.id))
try:
os.makedirs(dir)
except OSError:
flash('OSError!')
db.session.remove(ocr_job)
db.session.commit()
else:
for file in new_ocr_job_form.files.data:
file.save(os.path.join(dir, file.filename))
ocr_job.status = 'submitted'
db.session.commit()
flash('Job created!')
return redirect(url_for('services.ocr'))
return render_template('services/ocr.html.j2',
title='Optical Character Recognition',
new_ocr_job_form=new_ocr_job_form)
return redirect(url_for('services.{}'.format(service_handle)))
return render_template('services/{}.html.j2'.format(service_handle),
title=page_title,
new_job_form=new_job_form)

View File

@ -148,8 +148,8 @@
<li><a href="{{ url_for('main.dashboard') }}"><i class="material-icons">dashboard</i>Dashboard</a></li>
<li><div class="divider"></div></li>
<li><a class="subheader">Services</a></li>
<li><a href="{{ url_for('services.nlp') }}"><i class="material-icons">format_textdirection_l_to_r</i>NLP</a></li>
<li><a href="{{ url_for('services.ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
<li><a href="{{ url_for('services.service', service_handle='nlp') }}"><i class="material-icons">format_textdirection_l_to_r</i>NLP</a></li>
<li><a href="{{ url_for('services.service', service_handle='ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
{% if current_user.is_administrator() %}
<li><div class="divider"></div></li>
<li><a class="subheader">Administration</a></li>

View File

@ -134,7 +134,7 @@
</div>
<ul id='new-job-dropdown' class='dropdown-content'>
<li><a href="{{ url_for('services.nlp') }}"><i class="material-icons">format_textdirection_l_to_r</i>NLP</a></li>
<li><a href="{{ url_for('services.ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
<li><a href="{{ url_for('services.service', service_handle='nlp') }}"><i class="material-icons">format_textdirection_l_to_r</i>NLP</a></li>
<li><a href="{{ url_for('services.service', service_handle='ocr') }}"><i class="material-icons">find_in_page</i>OCR</a></li>
</ul>
{% endblock %}

View File

@ -55,14 +55,14 @@
<div class="card">
<form method="POST" enctype="multipart/form-data" id="new-nlp-job-form">
<div class="card-content">
{{ new_nlp_job_form.hidden_tag() }}
{{ new_job_form.hidden_tag() }}
<div class="row">
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">title</i>
{{ new_nlp_job_form.title(data_length='32') }}
{{ new_nlp_job_form.title.label }}
{% for error in new_nlp_job_form.title.errors %}
{{ new_job_form.title(data_length='32') }}
{{ new_job_form.title.label }}
{% for error in new_job_form.title.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -70,9 +70,9 @@
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">language</i>
{{ new_nlp_job_form.language() }}
{{ new_nlp_job_form.language.label }}
{% for error in new_nlp_job_form.language.errors %}
{{ new_job_form.language() }}
{{ new_job_form.language.label }}
{% for error in new_job_form.language.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -80,9 +80,9 @@
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">language</i>
{{ new_nlp_job_form.version() }}
{{ new_nlp_job_form.version.label }}
{% for error in new_nlp_job_form.version.errors %}
{{ new_job_form.version() }}
{{ new_job_form.version.label }}
{% for error in new_job_form.version.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -92,13 +92,13 @@
<div class="col s12 m6">
<div class="file-field input-field">
<div class="btn">
<span>{{ new_nlp_job_form.files.label.text }}</span>
{{ new_nlp_job_form.files(accept='text/plain') }}
<span>{{ new_job_form.files.label.text }}</span>
{{ new_job_form.files(accept='text/plain') }}
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
{% for error in new_nlp_job_form.files.errors %}
{% for error in new_job_form.files.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -106,9 +106,9 @@
<div class="col s12 m6">
<div class="input-field">
<i class="material-icons prefix">description</i>
{{ new_nlp_job_form.description(data_length='255') }}
{{ new_nlp_job_form.description.label }}
{% for error in new_nlp_job_form.description.errors %}
{{ new_job_form.description(data_length='255') }}
{{ new_job_form.description.label }}
{% for error in new_job_form.description.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -116,7 +116,7 @@
</div>
</div>
<div class="card-action right-align">
{{ new_nlp_job_form.submit(class='btn') }}
{{ new_job_form.submit(class='btn') }}
</div>
</form>
</div>

View File

@ -56,14 +56,14 @@
<div class="card">
<form method="POST" enctype="multipart/form-data" id="new-ocr-job-form">
<div class="card-content">
{{ new_ocr_job_form.hidden_tag() }}
{{ new_job_form.hidden_tag() }}
<div class="row">
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">title</i>
{{ new_ocr_job_form.title(data_length='32') }}
{{ new_ocr_job_form.title.label }}
{% for error in new_ocr_job_form.title.errors %}
{{ new_job_form.title(data_length='32') }}
{{ new_job_form.title.label }}
{% for error in new_job_form.title.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -71,9 +71,9 @@
<div class="col s12 m8">
<div class="input-field">
<i class="material-icons prefix">description</i>
{{ new_ocr_job_form.description(data_length='255') }}
{{ new_ocr_job_form.description.label }}
{% for error in new_ocr_job_form.description.errors %}
{{ new_job_form.description(data_length='255') }}
{{ new_job_form.description.label }}
{% for error in new_job_form.description.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -81,13 +81,13 @@
<div class="col s12 m5">
<div class="file-field input-field">
<div class="btn">
<span>{{ new_ocr_job_form.files.label.text }}</span>
{{ new_ocr_job_form.files(accept='application/pdf, image/tiff') }}
<span>{{ new_job_form.files.label.text }}</span>
{{ new_job_form.files(accept='application/pdf, image/tiff') }}
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
{% for error in new_ocr_job_form.files.errors %}
{% for error in new_job_form.files.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -95,9 +95,9 @@
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">language</i>
{{ new_ocr_job_form.language() }}
{{ new_ocr_job_form.language.label }}
{% for error in new_ocr_job_form.language.errors %}
{{ new_job_form.language() }}
{{ new_job_form.language.label }}
{% for error in new_job_form.language.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -105,9 +105,9 @@
<div class="col s12 m3">
<div class="input-field">
<i class="material-icons prefix">apps</i>
{{ new_ocr_job_form.version() }}
{{ new_ocr_job_form.version.label }}
{% for error in new_ocr_job_form.version.errors %}
{{ new_job_form.version() }}
{{ new_job_form.version.label }}
{% for error in new_job_form.version.errors %}
<span class="helper-text red-text">{{ error }}</span>
{% endfor %}
</div>
@ -194,7 +194,7 @@
<div class="col s3">
<div class="switch">
<label>
{{ new_ocr_job_form.binarization() }}
{{ new_job_form.binarization() }}
<span class="lever"></span>
</label>
</div>
@ -204,7 +204,7 @@
</div>
</div>
<div class="card-action right-align">
{{ new_ocr_job_form.submit(class='btn') }}
{{ new_job_form.submit(class='btn') }}
</div>
</form>
</div>