Product: TIBCO Spotfire®
Python script to append new Rows to an existing Data Table
Here are the steps to create the Python script. Each step is marked down in the script for reference.
1). Create some data with columns that match the existing table to which rows need to be added. \r\n are used for a newline character.
2). A Memory Stream is used, writing out the columns to a place we can access to make a data source.
3). Here are the settings to tell the system what data is being imported. It is defined as comma separated data types of the columns.
4). Use settings to automatically have the system match column names for between the data table and the memory data source created above.
5). Add the rows from our datasource.
Note-Memory stream is just used for illustration purpose. The same process can be followed with a URL to a file as well.
Sample dxp attached for reference(Add_Rows_DataTable.dxp)
Resolution
from Spotfire.Dxp.Data import AddRowsSettings
from System.IO import StreamWriter, MemoryStream, SeekOrigin
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings
//Step-1
textData = "CarMake,Price,Country\r\nMaruti,10000,India\r\n"
//Step-2
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)
//Step-3
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)
//Step-4
settings = AddRowsSettings(Document.ActiveDataTableReference,textDataSource)
//Step-5
Document.ActiveDataTableReference.AddRows(textDataSource,settings)
Disclaimer:
The script code in this article is only a sample to be used as a reference. It is not intended to be used "As Is" in a Production environment. Always test in a Development environment. Make modifications to the script per your implementation specifications that suit best your business requirements. Refer to the API reference(s) cited in this article for usage of the classes and methods used in the script.
Reference
https://docs.tibco.com/pub/doc_remote/spotfire/7.6.0/doc/api/html/T_Spotfire_Dxp_Data_AddRowsSettings.htm
https://docs.tibco.com/pub/doc_remote/spotfire/7.6.0/doc/api/html/T_Spotfire_Dxp_Data_DataType.htm
https://docs.tibco.com/pub/doc_remote/spotfire/7.6.0/doc/api/html/T_Spotfire_Dxp_Data_Import_TextFileDataSource.htm
https://docs.tibco.com/pub/doc_remote/spotfire/7.6.0/doc/api/html/M_Spotfire_Dxp_Data_Import_TextDataReaderSettings_AddColumnNameRow.htm
https://msdn.microsoft.com/en-us/library/system.io.streamwriter(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/883dhyx0(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.io.memorystream(v=vs.110).aspx
Comments
0 comments
Article is closed for comments.