Implementing gui_event
The gui_event
function serves as a gateway for all events.
The event must be dispatched to other functions that actually provide the intended functionality for the events. In this section the calling syntax, dispatching techniques, and input and output arguments will be discussed, followed by a small example.
Options for triggering events when a component value is changed or loses focus are described in the Advanced features
chapter.
Syntax
def gui_event(meta_data: dict, payload: dict) -> dict:
...
return payload
function payload = guiEvent(metaData, payload)
...
end
Register events explicitly
The recommended way to ensure the correct function is executed, is to explicitly register the event handler functions to the events in the gui_event
function. The event must then be dispatched to execute the registered function.
In the examples below the static method Form.eventHandler
is used to register the event
"SayHello" to the say_hello/sayHello function. The dispatch function ensures that the function registered to the event is executed. Note that the eventHandler
input arguments can be repeated to register multiple events.
def gui_event(meta_data: dict, payload: dict) -> dict:
Form.eventHandler(SayHello=say_hello)
callback = utils.getEventFunction(meta_data, payload)
return callback(meta_data, payload)
def say_hello(meta_data: dict, payload: dict) -> dict:
utils.setSubmissionData(payload, "display", "Hello, world!")
return payload
function payload = guiEvent(metaData, payload)
Form.eventHandler("SayHello", @sayHello);
payload = utils.dispatchEvent(metaData, payload);
end
function payload = sayHello(metaData, payload)
payload = utils.setSubmissionData(payload, "display", "Hello, world!");
end
Note that you can configure the behaviour of the registered event handler functions by adding inputs to a function that wraps the actual function.
def gui_event(meta_data: dict, payload: dict) -> dict:
Form.eventHandler(
SayHello=say_something("Hello"),
SayBye=say_something("Bye"),
)
callback = utils.getEventFunction(meta_data, payload)
return callback(meta_data, payload)
def say_something(word: str) -> Callable:
def inner(meta_data: dict, payload: dict) -> dict:
utils.setSubmissionData(payload, "display", word + ", world!")
return payload
return inner
function payload = guiEvent(metaData, payload)
Form.eventHandler( ...
'SayHello', say_something("Hello"), ...
'SayBye', say_something("Bye") ...
);
payload = utils.dispatchEvent(metaData, payload);
end
function func = say_something(word)
% Wrapper function to multiply app input value with a configurable factor.
func = @(metaData, payload) inner(metaData, payload, word);
end
function payload = inner(metaData, payload, word)
payload = simian.gui.utils.setSubmissionData(payload, "display", word + ", world!");
end
Dispatching events
Using the information in the payload input it is possible to programmatically route events to the correct function with if-else blocks.
Alternatively, you can let Simian dispatch the events to the intended function for you. This can be achieved by using the dispatchEvent
or getEventFunction
utility functions:
caller = utils.getEventFunction(meta_data, payload)
payload = caller(meta_data, payload)
payload = utils.dispatchEvent(metaData, payload);
Simian will look for and execute the function that:
- is explicitly registered to the event,
- has the same full name (including packages and classes) as the event ("reflection"),
- or throw an error
The dispatchEvent
function calls a function equal to the name of the event that was triggered, whereas getEventFunction
only returns the function object.
Note: In Python, breakpoints cannot be detected in functions executed via the
dispatchEvent
function. It is therefore recommended to usegetEventFunction
instead.
The functionality of the dispatching mechanism is further demonstrated in the example below.
Function arguments
meta_data
:
Meta data describing the client session.
Dict/struct with fields:
session_id
: unique ID for the session, can be used to differentiate between multiple instances of the applicationnamespace
: package name of the applicationmode
:local
ordeployed
client_data
:authenticated_user
: for deployed apps, the portal provides the logged on user infouser_displayname
: user name for printing (e.g. "John Smith")username
: unique identifier used by the authentication protocol
payload
:
When entering the function, this contains the event data containing all values as provided by the client. It is a dict/struct with fields:
action
:'event'
download
: information regarding the download of a file. Empty when no file is being downloaded.event
: name of the event that was triggered.followUp
(optional): name of a follow-up event that will be triggered after the current one has completed.formMap
: Map representing the form definition.key
: identifier of the component that triggered the eventsubmission
: dict/struct with fields:data
: dict/struct with fields:eventCounter
: incrementing counter; do not change.<key>
: multiple fields containing the values of the components identified by the keys.<nested form key>
: See Nested forms for more information. dict/struct with fields:data
: dict/struct with fields:<key>
: multiple fields containing the values of the components identified by the keys.
updateForm
: whentrue
, the form definition is sent to the front end. The default value isfalse
. Use this only when the form definition changes. Updating the form definition will be slower than only updating the submission data.alerts
(optional): array of dict/struct with fields:type
: message type, see Alertsmessage
: string
In the gui_event
function, the submission
field may be altered before sending payload
back to the front-end in order to present results to the user.
Additionally, the followUp
field can be changed, which is described below.
The key
is unique for each component within the context of its parent and/or nested form, but event
can be shared between components.
This can be useful when multiple components (partially) share functionality. See Example.
Follow-up Event
When the followUp
field is added to payload
, and its value is the name of another event, the follow-up event will be triggered after completion of the current one.
The component key will be identical for both events. The most common use case of this feature is to present the user with intermediate results of a calculation that consists of multiple stages.
Consider combining this with the StatusIndicator component.
An example of the follow-up event is given below:
import time
def gui_init(meta_data: dict) -> dict:
form = Form()
payload = {"form": form}
btnLoadData = component.Button("run", form)
btnLoadData.label = "Run"
btnLoadData.setEvent("Run")
txtId = component.TextField("id", form)
txtId.label = "ID"
return payload
def gui_event(meta_data: dict, payload: dict) -> dict:
if payload["event"] == "Run":
time.sleep(1)
utils.setSubmissionData(payload, "id", "Running phase 1")
payload["followUp"] = "RunNext"
elif payload["event"] == "RunNext":
time.sleep(1)
utils.setSubmissionData(payload, "id", "Finalizing...")
payload["followUp"] = "RunFinal"
elif payload["event"] == "RunFinal":
time.sleep(2)
utils.setSubmissionData(payload, "id", "Done!")
return payload
function payload = guiInit(metaData)
form = Form();
payload.form = form;
btnLoadData = component.Button("run", form);
btnLoadData.label = "Run";
btnLoadData.setEvent("Run");
txtId = component.TextField("id", form);
txtId.label = "ID";
end
function payload = guiEvent(metaData, payload)
switch payload.event
case "Run"
pause(1);
payload = utils.setSubmissionData(payload, "id", "Running phase 1");
payload.followUp = "RunNext";
case "RunNext"
pause(1);
payload = utils.setSubmissionData(payload, "id", "Finalizing...");
payload.followUp = "RunFinal";
case "RunFinal"
pause(2);
payload = utils.setSubmissionData(payload, "id", "Done!");
end
end
Example
The following example shows a form with several buttons, each with their own event.
btn1 = component.Button("btn1", parent);
btn1.setEvent("ModelS.getResults");
btn2 = component.Button("btn2", parent);
btn2.setEvent("writeToDatabase");
btn3 = component.Button("btn3", parent);
btn3.setEvent("ModelX.getValue");
The following gui_event
functions handle these events, the first one using conditional logic and the second one using the dispatchEvent
or getEventFunction
functions using event-function name reflection:
Note that the
ModelS
class contains agetResult
method and theModelX
class agetValue
method. For reflection, the event name must contain the class name!
def gui_event(meta_data, payload):
if payload["event"] == "ModelS.getResults":
ModelS.getResults(meta_data, payload)
elif payload["event"] == "writeToDatabase":
writeToDatabase(meta_data, payload)
elif payload["event"] == "ModelX.getValue":
ModelX.getValue(meta_data, payload)
def gui_event(meta_data, payload):
# Use the automatic dispatch.
caller = utils.getEventFunction(meta_data, payload)
payload = caller(meta_data, payload)
function payload = guiEvent(metaData, payload)
switch payload.event
case "ModelS.getResults"
payload = ModelS.getResults(metaData, payload);
case "writeToDatabase"
payload = writeToDatabase(metaData, payload);
case "ModelX.getValue"
payload = ModelX.getValue(metaData, payload);
end
end
function payload = guiEvent(metaData, payload)
% Use the automatic dispatch.
payload = utils.dispatchEvent(metaData, payload);
end