diff --git a/openflexure_microscope/api/utilities.py b/openflexure_microscope/api/utilities.py index b3f919e0..cdeca9d0 100644 --- a/openflexure_microscope/api/utilities.py +++ b/openflexure_microscope/api/utilities.py @@ -44,7 +44,7 @@ class JsonPayload: else: val = default - if convert: + if convert and (val is not None): val = convert(val) return val diff --git a/openflexure_microscope/plugins/default/autofocus/plugin.py b/openflexure_microscope/plugins/default/autofocus/plugin.py index 450f67e0..c9b44780 100644 --- a/openflexure_microscope/plugins/default/autofocus/plugin.py +++ b/openflexure_microscope/plugins/default/autofocus/plugin.py @@ -15,6 +15,8 @@ API_SCHEMA = { 'forms': [ { 'route': '/fast_autofocus', + 'isTask': True, + 'submitLabel': "Run autofocus", 'schema': [ { 'fieldType': "htmlBlock", diff --git a/openflexure_microscope/plugins/testing/__init__.py b/openflexure_microscope/plugins/testing/__init__.py index f42b093e..e69de29b 100644 --- a/openflexure_microscope/plugins/testing/__init__.py +++ b/openflexure_microscope/plugins/testing/__init__.py @@ -1 +0,0 @@ -from .plugin import Plugin \ No newline at end of file diff --git a/openflexure_microscope/plugins/testing/plugin.py b/openflexure_microscope/plugins/testing/plugin.py deleted file mode 100644 index 719eb62b..00000000 --- a/openflexure_microscope/plugins/testing/plugin.py +++ /dev/null @@ -1,13 +0,0 @@ -from openflexure_microscope.plugins import MicroscopePlugin - - -class Plugin(MicroscopePlugin): - """ - A set of default plugins - """ - - def identify(self): - """ - Tests for access to Microscope.camera, and Microscope.stage - """ - return self.microscope.camera, self.microscope.stage diff --git a/openflexure_microscope/plugins/testing/schema_example/__init__.py b/openflexure_microscope/plugins/testing/schema_example/__init__.py new file mode 100644 index 00000000..3e9a8c4d --- /dev/null +++ b/openflexure_microscope/plugins/testing/schema_example/__init__.py @@ -0,0 +1,2 @@ +from .plugin import ExamplePlugin +from . import api \ No newline at end of file diff --git a/openflexure_microscope/plugins/testing/schema_example/api.py b/openflexure_microscope/plugins/testing/schema_example/api.py new file mode 100644 index 00000000..6c518043 --- /dev/null +++ b/openflexure_microscope/plugins/testing/schema_example/api.py @@ -0,0 +1,40 @@ +from openflexure_microscope.api.utilities import JsonPayload +from openflexure_microscope.api.v1.views import MicroscopeViewPlugin +from openflexure_microscope.exceptions import TaskDeniedException + +from flask import request, Response, escape, jsonify + +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 = JsonPayload(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' + }) \ No newline at end of file diff --git a/openflexure_microscope/plugins/testing/schema_example/plugin.py b/openflexure_microscope/plugins/testing/schema_example/plugin.py new file mode 100644 index 00000000..e3d2b936 --- /dev/null +++ b/openflexure_microscope/plugins/testing/schema_example/plugin.py @@ -0,0 +1,60 @@ +import random +import time +import os +import json +from openflexure_microscope.plugins import MicroscopePlugin + +from .api import DoAPI + +HERE = os.path.dirname(os.path.realpath(__file__)) +SCHEMA_PATH = os.path.join(HERE, "schema.json") + +class ExamplePlugin(MicroscopePlugin): + """ + An example plugin using a comprehensive schema + """ + global SCHEMA_PATH + + with open(SCHEMA_PATH, 'r') as sc: + api_schema = json.load(sc) + + api_views = { + '/do': DoAPI, + } + + 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" + + def set_values(self, val_int, val_str, val_radio, val_check, val_select, val_disposable): + """ + Demonstrate a plugin with schema + """ + 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 + } \ No newline at end of file diff --git a/openflexure_microscope/plugins/testing/schema_example/schema.json b/openflexure_microscope/plugins/testing/schema_example/schema.json new file mode 100644 index 00000000..a26bd8c0 --- /dev/null +++ b/openflexure_microscope/plugins/testing/schema_example/schema.json @@ -0,0 +1,61 @@ +{ + "id": "test-plugin", + "forms": [ + { + "route": "/do", + "submitLabel": "Do things", + "selfUpdate": true, + "schema": [ + { + "fieldType": "htmlBlock", + "name": "heading", + "content": "This is a plugin!" + }, + [ + { + "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": "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" + } + ] + } + ] +} \ No newline at end of file