Introduction
This example shows how to invoke pre-defined data functions in Spotfire using IronPython scripting.
Before we invoke the data function it must already be defined in Spotfire. This is done in the 'Edit > Data Function Properties' interface. After this is done we can descide how we want the function to execute (asyncrous/syncronous), if we need to define the input / output settings and other relevant items.
Note: Executing data functions without a dedicated TERR server is only possible on the desktop version of Spotfire (Analyst) to use this on the Spotfire Web Player a dedicated Statistics Services Server (TSSS) must be used.
Code samples
This is the easiest method. It will use the default settings and input and output options already defined in the data function.
Execute data function using the DataFunctionExecutorService (simple)
# Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license. # Datafunction service from Spotfire.Dxp.Data.DataFunctions import DataFunctionExecutorService, DataFunctionInvocation, DataFunctionInvocationBuilder dataManager = Document.Data app = Application dataFunction = None for function in dataManager.DataFunctions: if function.Name == 'Test2': dataFunction = function dataFunction.Execute()
There are two ways to execute data functions synchronously (waiting for it to finish). The first is the ExecuteSynchronously method. This will run the function directly and return a boolean value with the execution result. The second method is also asynchronous but with a callback that occurs after the function has completed. When choosing between the two types of synchronous execution, the callback method should be preferred since it will delegate when and how the function is executed to Spotfire that will optimize internally depending on the current state of the application.
Execute data function synchronously
# Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license. import clr from Spotfire.Dxp.Data.DataFunctions import * from Spotfire.Dxp.Framework.ApplicationModel import ConnectivityService, NotificationService from Spotfire.Dxp.Framework.Library import LibraryException, LibraryManager, LibraryItemType # add a simple data function that creates a new table with one single column. builder = clr.Reference[DataFunctionDefinitionBuilder](); builder = DataFunctionDefinitionBuilder("myNewDF", DataFunctionExecutorTypeIdentifiers.TERRScriptExecutor); outputBuilder = clr.Reference[OutputParameterBuilder](); outputBuilder = OutputParameterBuilder("output", ParameterType.Table); output = outputBuilder.Build(); builder.OutputParameters.Add(output); builder.Settings["Script"] = "col <- 1:100; output <- data.frame(col=col);"; dataFunctionDefinition = builder.Build(); dataFunction = Document.Data.DataFunctions.AddNew("myNewDF", dataFunctionDefinition); dataFunction.UpdateBehavior = DataFunctionUpdateBehavior.Manual; dataFunction.Visible = True; dataFunction.Outputs.SetTableOutput(output, "myNewTable"); dataFunction.ExecuteSynchronously();
Execute asynchronously (using callback)
# Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license. def datafunction_callback(): # Execute the next function after the first has returned OtherDataFunction.Execute() # DataFunction represents data function defined from a DataFunctionDefinition DataFunction.Execute(datafunction_callback)
References
License: TIBCO BSD-Style License
Recommended Comments
There are no comments to display.