Jump to content
  • IronPython script to load an In memory csv file in Spotfire®


    It is often necessary to be able to create a data table in Spotfire programatically. This could be used to create a data table for containing possible values for property (drop-down) controls.

    Introduction

    It is often necessary to be able to create a data table in Spotfire programatically. This could be used to create a data table for containing possible values for property (drop-down) controls, for example.

    Load CSV file

    The below script can be used to create an in memory CSV file as a Spotfire data table.

    Special notes:

    1. Please check the types of the columns as loaded in the LoadCSV function - in the example shown, the first column (column 0) is an integer column, so its datatype must be specified as DataType.Integer. All columns are string type columns unless otherwise specified
    2. The example CSV being generated in memory is two rows of data. The first line is the column headers
    # Copyright © 2017. TIBCO Software Inc.  Licensed under TIBCO BSD-style license.
    
    #Author - Andrew Berridge, TIBCO Software, December 2014
    import System
    import Spotfire.Dxp.Application
    from Spotfire.Dxp.Data import *
    from Spotfire.Dxp.Application.Visuals import VisualContent
    from System.Collections.Generic import HashSet
    from System.IO import FileStream, FileMode, File, MemoryStream, SeekOrigin, StreamWriter
    import System.String
    from Spotfire.Dxp.Data.Import import TextDataReaderSettings
    from Spotfire.Dxp.Data.Import import TextFileDataSource
    
    # Function to load the CSV file
    def LoadCSV(dataTableName, stream):
    	settings = TextDataReaderSettings()
    	settings.Separator = ","
    	settings.AddColumnNameRow(0)
    	settings.ClearDataTypes(False)
    	settings.SetDataType(0, DataType.Integer)
    	stream.Seek(0, SeekOrigin.Begin)
    	fs = TextFileDataSource(stream, settings)
    	if Document.Data.Tables.Contains(dataTableName):
    		Document.Data.Tables[dataTableName].ReplaceData(fs)
    	else:
    		Document.Data.Tables.Add(dataTableName, fs)
    		
    stream = MemoryStream()
    csvWriter = StreamWriter(stream)#, Encoding.UTF8)
    csvWriter.WriteLine("column1,column2rn")
    csvWriter.WriteLine("1,row1value2rn")
    csvWriter.WriteLine("5,row2value2rn")
    
    csvWriter.Flush()
    LoadCSV("Data Table Name", stream)
     

    License:  TIBCO BSD-Style License

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...