spotfire ironpython : Append new row to a data-table

dhvlnyk picture dhvlnyk · Jun 1, 2015 · Viewed 7.4k times · Source

I have a csv string input
string_in="country,100,color"
Can anyone please suggest me how to append this input(string_in) to an already existing
datatable visualisation using ironpython script in spotfire.
Thanks.

Answer

clesiemo3 picture clesiemo3 · Jun 1, 2015

You'll want to pull in a handful of different functions to pull this off through scripting. I have my script below but overall what you're looking to do is to set up your input as a data source and then add the rows from that data source to your existing one. I borrowed a good amount from this spotfire tutorial by TIBCO and modified it as necessary to get us where you want to be. There may be a couple excess imports you can remove as a result.

I use datTab as an input parameter to the script as the data table we want to add our rows to.

from Spotfire.Dxp.Data import AddRowsSettings
import System
from System import DateTime
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings

#First we need to create your data with some columns. 
#Here I have a comma separated miniature table built up with 
#a commented option for your variable itself. \r\n used for newline
textData = "name,values,category\r\ncountry,100,color\r\n" 
#textData = "col1,col2,col3\r\n" + string_in + "\r\n"

#Memory Stream stuff. Simply just writing our variable 
#into a place we can access to make a data source
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)

#you need settings to tell the system what stuff you're importing.
#here we define it is comma separated and what data types our columns are.
readerSettings = TextDataReaderSettings()
readerSettings.Separator = ","
readerSettings.AddColumnNameRow(0)
readerSettings.SetDataType(0, DataType.String)
readerSettings.SetDataType(1, DataType.Integer)
readerSettings.SetDataType(2, DataType.String)
textDataSource = TextFileDataSource(stream,readerSettings)

#We create some settings here automatically having the system match
#column names for us between the data table and our data source.
settings = AddRowsSettings(datTab,textDataSource)
#And finally we add the rows from our datasource with our settings.
datTab.AddRows(textDataSource,settings)

You can of course make this more complex with longer input variables, loop through things to add multiple rows, etc. You can also follow this same process with a URL to a file instead of the memory stream stuff. Depends on your input types.

Let me know if you have any questions. I tried to comment the important parts but there could be further explanation on specific functions if needed.

Edit: See my screenshot below after I added the record a couple different times screenshot