Plugin schema are now just plugin forms (requires eV 1.2)

This commit is contained in:
Joel Collins 2019-09-14 15:51:51 +01:00
parent 0d7a5f71e0
commit d9e07f913a
16 changed files with 106 additions and 98 deletions

View file

@ -0,0 +1,2 @@
from .plugin import ExamplePlugin
from . import api

View file

@ -0,0 +1,61 @@
from openflexure_microscope.devel import MicroscopeViewPlugin, TaskDeniedException, JsonResponse, Response, request, escape, jsonify
import logging
class DoAPI(MicroscopeViewPlugin):
"""
A simple example API plugin
"""
def get(self):
return jsonify(self.plugin.get_values_dict())
def post(self):
# Get payload JSON
payload = JsonResponse(request)
# Extract a values from the JSON payload.
val_int = payload.param('val_int', default=None, convert=int)
val_str = payload.param('val_str', default=None, convert=str)
val_radio = payload.param('val_radio', default=None)
val_check = payload.param('val_check', default=None)
val_select = payload.param('val_select', default=None)
val_disposable = payload.param('val_disposable', default=None)
self.plugin.set_values(
val_int,
val_str,
val_radio,
val_check,
val_select,
val_disposable
)
print(self.plugin.get_values_dict())
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 = JsonResponse(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

@ -0,0 +1,80 @@
{
"id": "test-plugin",
"icon": "pets",
"forms": [
{
"name": "Simple request",
"isCollapsible": false,
"isTask": false,
"selfUpdate": true,
"route": "/do",
"submitLabel": "Do things",
"form": [
[
{
"fieldType": "numberInput",
"placeholder": "Some integer",
"name": "val_int",
"label": "Number value",
"minValue": 0
},
{
"fieldType": "textInput",
"placeholder": "Some string",
"label": "String value",
"name": "val_str"
}
],
[
{
"fieldType": "radioList",
"name": "val_radio",
"label": "Radio value",
"options": ["First", "Second", "Third"]
},
{
"fieldType": "checkList",
"name": "val_check",
"label": "Checklist values",
"options": ["Foo", "Bar", "Baz"]
}
],
{
"fieldType": "htmlBlock",
"name": "html_block",
"content": "<i>This is a block of HTML in a plugin!</i><br>I can do paragraph breaks and stuff."
},
{
"fieldType": "selectList",
"name": "val_select",
"multi": false,
"label": "Some selection",
"options": ["Most", "Average", "Least"]
},
{
"fieldType": "textInput",
"placeholder": "Some string",
"label": "Non-persistent string",
"name": "val_disposable"
}
]
},
{
"name": "Task form",
"isTask": true,
"selfUpdate": true,
"route": "/task",
"submitLabel": "Start task",
"form": [
{
"fieldType": "numberInput",
"placeholder": "",
"name": "run_time",
"label": "Run time (seconds)",
"minValue": 1
}
]
}
]
}

View file

@ -0,0 +1,72 @@
import random
import time
import os
import json
from openflexure_microscope.devel import MicroscopePlugin
from .api import DoAPI, TaskAPI
HERE = os.path.dirname(os.path.realpath(__file__))
FORM_PATH = os.path.join(HERE, "forms.json")
class ExamplePlugin(MicroscopePlugin):
"""
An example plugin using a comprehensive form
"""
global FORM_PATH
with open(FORM_PATH, 'r') as sc:
api_form = json.load(sc)
api_views = {
'/do': DoAPI,
'/task': TaskAPI
}
def __init__(self):
self.val_int = 10
self.val_str = "Hello"
self.val_radio = "First"
self.val_check = ["Foo", "Bar"]
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 form
"""
if val_int:
self.val_int = int(val_int)
if val_str:
self.val_str = str(val_str)
if val_radio:
self.val_radio = val_radio
if val_check is not None:
print(val_check)
self.val_check = val_check if (type(val_check) is list) else [val_check]
if val_select:
self.val_select = val_select
if val_disposable:
print("DISPOSABLE VALUE: {}".format(val_disposable))
def get_values_dict(self):
return {
'val_int': self.val_int,
'val_str': self.val_str,
'val_radio': self.val_radio,
'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