Search code examples
spotfire

How can I use IronPython to save an analysis which I have modified using the Spotfire Consumer back to the library?


Is it possible to save an analysis file that is opened in the Spotfire's Web Player (i.e. Spotfire Consumer) programmatically by calling some API or that is all handled by the UI (Web Player)? I am using Spotfire version 10.1

Example, can I do that: enter image description here

... in code (e.g. using Web API)


Solution

  • thanks for the clarification!

    the following IronPython code will save an analysis. you will find a limitation, however, when saving in the Web Player, that the dropdown in the upper right corner must be set to "Viewing" (in 10.0). to reiterate: you cannot use this code to save a document that is currently in "Editing" mode in the Web Player.

    from Spotfire.Dxp.Application import DocumentSaveSettings
    from Spotfire.Dxp.Framework.Library import LibraryManager, LibraryItemType, LibraryItemMetadataSettings
    from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread
    
    def save_to_library(app, path, filename, meta, settings):
      def f():
        app.SaveAs(path, filename, meta, settings)
      return f
    
    # path info
    lib_path = "/path/to/file"
    filename = "My Analysis"
    
    # reference the LibraryManager
    lm = Application.GetService[LibraryManager]()
    
    # determine if the path exists
    success, lib_folder = lm.TryGetItem(lib_path, LibraryItemType.Folder)
    
    if success:
      # save the file
      Application.GetService[ApplicationThread]().InvokeAsynchronously(save_to_library(Application, lib_folder, filename, LibraryItemMetadataSettings(), DocumentSaveSettings()))
    else:
      print "folder " + lib_path + " does not exist in the Library"
    

    code based on this article from the TIBCO Wiki.