Jump to content
We've recently updated our Privacy Statement, available here. ×
  • How to Write/Export CrossTable data to a MemoryStream and creating a TextFileDataSource from the same using IronPython in Spotfire®


    It is worthy to note that when you execute an IronPython script on the Web Player, the script is executed on the Web Player Server's host, and not the client computer and hence there may be access related error if writing to or reading from File. As a workaround you may want to avoid writing to a file and instead write to a memory stream. Below is a sample code which CrossTable Plot data to a MemoryStream and creates a TextFileDataSource from the same .

    Introduction

    It is worthy to note that when you execute an IronPython script on the Web Player, the script is executed on the Web Player Server's host, and not the client computer and hence there may be access related error if writing to or reading from File. As a workaround you may want to avoid writing to a file and instead write to a memory stream. Below is a sample code which CrossTable Plot data to a MemoryStream and creates a TextFileDataSource from the same .

    Code sample

    # Copyright © 2017. TIBCO Software Inc.  Licensed under TIBCO BSD-style license.
    
    from System.IO import *
    from System.IO import Path, File, StreamWriter
    from Spotfire.Dxp.Data import DataTableSaveSettings
    from Spotfire.Dxp.Application.Visuals import CrossTablePlot
    from Spotfire.Dxp.Data.Import import TextFileDataSource
    from Spotfire.Dxp.Data.Import import TextDataReaderSettings
    from Spotfire.Dxp.Application.Visuals import TablePlot
    from Spotfire.Dxp.Application.Visuals import VisualTypeIdentifiers
    
    #Export CrossTable data to the MemoryStream
    mStream = MemoryStream();
    mWriter = StreamWriter(mStream);
    
    CrossTable.As[CrossTablePlot]().ExportText(mWriter)
    
    #Reset stream back to the beginning
    mStream.Seek(0, SeekOrigin.Begin)
    
    #Add cross table data back into Spotfire using TextFileDataSource
    readerSet = TextDataReaderSettings()
    readerSet.Separator = "t"
    readerSet.AddColumnNameRow(0)
    
    textDataSource = TextFileDataSource(mStream, readerSet)
    
    for table in Document.Data.Tables:
        if table.Name == "CrossTableData":
            Document.Data.Tables.Remove(table.Name)
    
    myNewTable = Document.Data.Tables.Add("CrossTableData",textDataSource)
    
    #If you want to change the data table to be embedded and not linked, use the following code
    settings = DataTableSaveSettings (myNewTable,False, False);
    Document.Data.SaveSettings.DataTableSettings.Add(settings);
     

    References

    License:  TIBCO BSD-Style License

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...