Commit e91e58da authored by Joel Collins's avatar Joel Collins
Browse files

Added a long running task to schema example

parent 54dcee00
Loading
Loading
Loading
Loading
+25 −1
Original line number Diff line number Diff line
@@ -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):
    """
@@ -38,3 +39,26 @@ 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
 No newline at end of file
+14 −2
Original line number Diff line number Diff line
@@ -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
@@ -58,3 +61,12 @@ class ExamplePlugin(MicroscopePlugin):
            '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
 No newline at end of file
+8 −16
Original line number Diff line number Diff line
@@ -60,28 +60,20 @@
      ]
    },
    {
      "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
          }
      ]
      ]
    }
  ] 
}
 No newline at end of file