Jump to content
  • How to create, execute, and save a Spotfire® data function using IronPython


    This article provides an example of creating, executing, and saving a Spotfire data function. This example uses the TIBCO Enterprise Runtime for R (TERR) script executor.

    Introduction

    This article provides an example of creating, executing, and saving a Spotfire data function. This example uses the TIBCO Enterprise Runtime for R (TERR) script executor.

    Code sample

    # 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
    
    # Notification service.
    notify = Application.GetService[NotificationService]()
    
    # 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");
    
    # Un-comment line below to run the data function and create the output table.
    # dataFunction.ExecuteSynchronously();
    
    # Un-comment the needed lines below to save the data function to the Library.
    # Check that the client instance is online, i.e., connected to the Library. 
    # If found, save the data function to the specified library folder.
    # stringFolder= "/MyDataFunctionsMain/NewFunctionsFolder";
    # Search the library with the above path. If found, assign the library item.
    # manager = Application.GetService[LibraryManager]();
    # (found, item) = manager.TryGetItem(stringFolder, LibraryItemType.Folder)
    # if found:
    #	sfLibraryFolderPath = item;
    # Save the data function.
    # if (Application.GetService[ConnectivityService]().IsOnline & found):
    #	try:
    #		savedFunctionDefinition = dataFunctionDefinition.SaveAs(sfLibraryFolderPath, "myNewDF", "");
    #	except LibraryException:
    #		notify.AddErrorNotification("Error saving the datafunction to the Library","Error details",str(LibraryException))
     

    References

    License:  TIBCO BSD-Style License

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...