Added a long running task to schema example
This commit is contained in:
parent
54dcee0083
commit
e91e58daa3
3 changed files with 47 additions and 19 deletions
|
|
@ -3,6 +3,7 @@ from openflexure_microscope.api.v1.views import MicroscopeViewPlugin
|
||||||
from openflexure_microscope.exceptions import TaskDeniedException
|
from openflexure_microscope.exceptions import TaskDeniedException
|
||||||
|
|
||||||
from flask import request, Response, escape, jsonify
|
from flask import request, Response, escape, jsonify
|
||||||
|
import logging
|
||||||
|
|
||||||
class DoAPI(MicroscopeViewPlugin):
|
class DoAPI(MicroscopeViewPlugin):
|
||||||
"""
|
"""
|
||||||
|
|
@ -37,4 +38,27 @@ class DoAPI(MicroscopeViewPlugin):
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'response': 'completed'
|
'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
|
||||||
|
|
@ -4,7 +4,7 @@ import os
|
||||||
import json
|
import json
|
||||||
from openflexure_microscope.plugins import MicroscopePlugin
|
from openflexure_microscope.plugins import MicroscopePlugin
|
||||||
|
|
||||||
from .api import DoAPI
|
from .api import DoAPI, TaskAPI
|
||||||
|
|
||||||
HERE = os.path.dirname(os.path.realpath(__file__))
|
HERE = os.path.dirname(os.path.realpath(__file__))
|
||||||
SCHEMA_PATH = os.path.join(HERE, "schema.json")
|
SCHEMA_PATH = os.path.join(HERE, "schema.json")
|
||||||
|
|
@ -20,6 +20,7 @@ class ExamplePlugin(MicroscopePlugin):
|
||||||
|
|
||||||
api_views = {
|
api_views = {
|
||||||
'/do': DoAPI,
|
'/do': DoAPI,
|
||||||
|
'/task': TaskAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
@ -30,6 +31,8 @@ class ExamplePlugin(MicroscopePlugin):
|
||||||
self.val_select = "Most"
|
self.val_select = "Most"
|
||||||
self.val_unused = "I'm an unused string, here to confuse the form parsing"
|
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):
|
def set_values(self, val_int, val_str, val_radio, val_check, val_select, val_disposable):
|
||||||
"""
|
"""
|
||||||
Demonstrate a plugin with schema
|
Demonstrate a plugin with schema
|
||||||
|
|
@ -57,4 +60,13 @@ class ExamplePlugin(MicroscopePlugin):
|
||||||
'val_check': self.val_check,
|
'val_check': self.val_check,
|
||||||
'val_select': self.val_select,
|
'val_select': self.val_select,
|
||||||
'val_unused': self.val_unused
|
'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
|
||||||
|
|
@ -60,27 +60,19 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Different form",
|
"name": "Task form",
|
||||||
"isTask": false,
|
"isTask": true,
|
||||||
"selfUpdate": true,
|
"selfUpdate": true,
|
||||||
"route": "/do",
|
"route": "/task",
|
||||||
"submitLabel": "Do different things",
|
"submitLabel": "Start task",
|
||||||
"schema": [
|
"schema": [
|
||||||
[
|
|
||||||
{
|
{
|
||||||
"fieldType": "numberInput",
|
"fieldType": "numberInput",
|
||||||
"placeholder": "Some integer",
|
"placeholder": "",
|
||||||
"name": "val_int",
|
"name": "run_time",
|
||||||
"label": "Number value",
|
"label": "Run time (seconds)",
|
||||||
"minValue": 0
|
"minValue": 1
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldType": "textInput",
|
|
||||||
"placeholder": "Some string",
|
|
||||||
"label": "String value",
|
|
||||||
"name": "val_str"
|
|
||||||
}
|
}
|
||||||
]
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue