Added a long running task to schema example

This commit is contained in:
Joel Collins 2019-06-18 15:56:02 +01:00
parent 54dcee0083
commit e91e58daa3
3 changed files with 47 additions and 19 deletions

View file

@ -3,6 +3,7 @@ from openflexure_microscope.api.v1.views import MicroscopeViewPlugin
from openflexure_microscope.exceptions import TaskDeniedException
from flask import request, Response, escape, jsonify
import logging
class DoAPI(MicroscopeViewPlugin):
"""
@ -37,4 +38,27 @@ class DoAPI(MicroscopeViewPlugin):
return jsonify({
'response': 'completed'
})
})
class TaskAPI(MicroscopeViewPlugin):
"""
A task example API plugin
"""
def get(self):
return jsonify({
'run_time': self.plugin.run_time
})
def post(self):
# Get payload JSON
payload = JsonPayload(request)
# Extract a values from the JSON payload.
val_int = payload.param('run_time', default=5, convert=int)
logging.info("Running task...")
task = self.microscope.task.start(self.plugin.generate_random_numbers_for_a_while, val_int)
# return a handle on the autofocus task
return jsonify(task.state), 202

View file

@ -4,7 +4,7 @@ import os
import json
from openflexure_microscope.plugins import MicroscopePlugin
from .api import DoAPI
from .api import DoAPI, TaskAPI
HERE = os.path.dirname(os.path.realpath(__file__))
SCHEMA_PATH = os.path.join(HERE, "schema.json")
@ -20,6 +20,7 @@ class ExamplePlugin(MicroscopePlugin):
api_views = {
'/do': DoAPI,
'/task': TaskAPI
}
def __init__(self):
@ -30,6 +31,8 @@ class ExamplePlugin(MicroscopePlugin):
self.val_select = "Most"
self.val_unused = "I'm an unused string, here to confuse the form parsing"
self.run_time = 5
def set_values(self, val_int, val_str, val_radio, val_check, val_select, val_disposable):
"""
Demonstrate a plugin with schema
@ -57,4 +60,13 @@ class ExamplePlugin(MicroscopePlugin):
'val_check': self.val_check,
'val_select': self.val_select,
'val_unused': self.val_unused
}
}
def generate_random_numbers_for_a_while(self, run_time: int):
self.run_time = run_time
vals = []
for _ in range(run_time):
vals.append(random.random())
time.sleep(1)
return vals

View file

@ -60,27 +60,19 @@
]
},
{
"name": "Different form",
"isTask": false,
"name": "Task form",
"isTask": true,
"selfUpdate": true,
"route": "/do",
"submitLabel": "Do different things",
"route": "/task",
"submitLabel": "Start task",
"schema": [
[
{
"fieldType": "numberInput",
"placeholder": "Some integer",
"name": "val_int",
"label": "Number value",
"minValue": 0
},
{
"fieldType": "textInput",
"placeholder": "Some string",
"label": "String value",
"name": "val_str"
"placeholder": "",
"name": "run_time",
"label": "Run time (seconds)",
"minValue": 1
}
]
]
}
]