Commit 1db88548 authored by Joel Collins's avatar Joel Collins
Browse files

Added example dynamic form

parent 45e7bcb1
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
from .plugin import DynamicExamplePlugin
from . import api
+24 −0
Original line number Diff line number Diff line
from openflexure_microscope.devel import (
    MicroscopeViewPlugin,
    JsonResponse,
    request,
    jsonify,
    taskify,
)

import logging


class DoAPI(MicroscopeViewPlugin):
    """
    A dynamic example API plugin
    """

    def get(self):
        values = {"val_int": self.plugin.val_int}
        return jsonify(values)

    def post(self):
        self.plugin.val_int += 1

        return jsonify({"response": "completed"})
+52 −0
Original line number Diff line number Diff line
import random
import time
import os
import json
from openflexure_microscope.devel import MicroscopePlugin, update_task_progress

from .api import DoAPI


class DynamicExamplePlugin(MicroscopePlugin):
    """
    An example plugin using a comprehensive form
    """

    api_views = {"/do": DoAPI}

    def __init__(self):
        MicroscopePlugin.__init__(self)

        self.val_int = 0
        self.val_str = "Hello"

        self.set_gui(self.dynamic_form)

    def dynamic_form(self):
        return {
            "id": "test-plugin",
            "icon": "pets",
            "forms": [
                {
                    "name": "Simple request",
                    "isCollapsible": False,
                    "isTask": False,
                    "selfUpdate": True,
                    "route": "/do",
                    "submitLabel": "Do things",
                    "schema": [
                        {
                            "fieldType": "numberInput",
                            "name": "val_int",
                            "label": "Number value",
                            "minValue": 0,
                        },
                        {
                            "fieldType": "htmlBlock",
                            "name": "html_block",
                            "content": f"<i>Value is: </i><br>{self.val_int}.",
                        },
                    ],
                }
            ],
        }