Add flask app with auto-reload to render the form template

This commit is contained in:
Benoît Seignovert 2024-02-14 10:02:07 +01:00
parent 433862d0ad
commit 2333ccd168
Signed by: Benoît Seignovert
GPG key ID: F5D8895227D18A0B
12 changed files with 662 additions and 61 deletions

View file

@ -0,0 +1,55 @@
"""GLiCID form templates module."""
from jinja2 import Environment, PackageLoader, select_autoescape
from .micromamba import get_envs
from .resources import CPU, GPU, RAM
TEMPLATES = Environment(
loader=PackageLoader('glicid_spawner'),
autoescape=select_autoescape(),
)
def options_attrs(username: str) -> dict:
"""Form options attributes."""
return {
'username': username,
'python_envs': get_envs(username),
'cpu_available': CPU,
'ram_available': RAM,
'gpu_available': GPU,
}
def options_form(username: str) -> str:
"""Render default spawner form."""
template = TEMPLATES.get_template('interactive.html')
return template.render(**options_attrs(username))
def options_from_form(formdata) -> dict:
"""Export options from default form."""
# Resources choices indexes
i_cpu = int(formdata['cpu'][0])
i_ram = int(formdata['ram'][0])
i_gpu = int(formdata['gpu'][0])
duration = min(
CPU[i_cpu].max_duration,
RAM[i_ram].max_duration,
GPU[i_gpu].max_duration,
)
# Export options
options = {
'pyenv': formdata['python-env'][0],
'nprocs': int(CPU[i_cpu].description),
'memory': RAM[i_ram].description.replace(' ', ''),
'runtime': f'{duration:02d}:00:00',
}
if i_gpu:
options['gres'] = 'gpu:' + GPU[i_gpu].description.lower()
return options

View file

@ -1,11 +1,9 @@
"""GLiCID spawner module."""
from batchspawner import SlurmSpawner
from jinja2 import Environment, PackageLoader, select_autoescape
from traitlets import Unicode
from .micromamba import get_envs
from .resources import CPU, GPU, RAM
from .form import options_form, options_from_form
class GlicidSpawner(SlurmSpawner):
@ -16,46 +14,10 @@ class GlicidSpawner(SlurmSpawner):
help='Glicid spawner singleuser command.',
).tag(config=True)
def _options_form_default(self) -> str:
def options_form(self) -> str:
"""JupyterHub rendered form template."""
environment = Environment(
loader=PackageLoader('glicid_spawner'),
autoescape=select_autoescape(),
)
template = environment.get_template('interactive.html')
return template.render(
username=self.user.name,
python_envs=get_envs(self.user.name),
cpu_available=CPU,
ram_available=RAM,
gpu_available=GPU,
)
return options_form(self.user.name)
def options_from_form(self, formdata) -> dict:
"""Export options from form."""
# Index of user resources choices
i_cpu = int(formdata['cpu'][0])
i_ram = int(formdata['ram'][0])
i_gpu = int(formdata['gpu'][0])
duration = min(
CPU[i_cpu].max_duration,
RAM[i_ram].max_duration,
GPU[i_gpu].max_duration,
)
# Export options
options = {}
options['pyenv'] = formdata['python-env'][0]
options['cpus-per-task'] = CPU[i_cpu].description
options['mem'] = RAM[i_ram].description.replace(' ', '')
if i_gpu:
options['gres'] = 'gpu:' + GPU[i_gpu].description
options['time'] = f'{duration:02d}:00:00'
return options
return options_from_form(formdata)

View file

@ -6,7 +6,7 @@
</div>
</div>
<div class="form-group">
<label for="username" class="col-sm-3 control-label">Python environment:</label>
<label for="python-env" class="col-sm-3 control-label">Python environment:</label>
<div class="col-sm-9">
<select class="form-control" name="python-env">
{%- for pyenv in python_envs -%}
@ -21,7 +21,7 @@
{%- for cpu in cpu_available -%}
<div class="col-sm-2">
<input type="radio" name="cpu" id="cpu_{{loop.index0}}" value="{{loop.index0}}"
data-max-duration="{{cpu.max_duration}}" {%- if loop.first -%}checked{%- endif -%}>
data-max-duration="{{cpu.max_duration}}"{% if loop.first %} checked{% endif %}>
<label for="cpu_{{loop.index0}}" class="btn btn-default btn-block">
{{ cpu.description }}
</label>
@ -35,7 +35,7 @@
{%- for ram in ram_available -%}
<div class="col-sm-2">
<input type="radio" name="ram" id="ram_{{loop.index0}}" value="{{loop.index0}}"
data-max-duration="{{ram.max_duration}}" {%- if loop.first -%}checked{%- endif -%}>
data-max-duration="{{ram.max_duration}}"{% if loop.first %} checked{% endif %}>
<label for="ram_{{loop.index0}}" class="btn btn-default btn-block">
{{ ram.description }}
</label>
@ -49,7 +49,7 @@
{%- for gpu in gpu_available -%}
<div class="col-sm-2">
<input type="radio" name="gpu" id="gpu_{{loop.index0}}" value="{{loop.index0}}"
data-max-duration="{{gpu.max_duration}}" {%- if loop.first -%}checked{%- endif -%}>
data-max-duration="{{gpu.max_duration}}"{% if loop.first %} checked{% endif %}>
<label for="gpu_{{loop.index0}}" class="btn btn-default btn-block">
{{ gpu.description }}
</label>