Jump to content

How to insert a data read from ActiveMarkingSelection as a row into a new datatable?


PBR
Go to solution Solved by Olivier Keugue Tadaa,

Recommended Posts

Hello.

I have a line chart of Var vs. Time. I want to make a selection on the line chart, then get the min and max Time for that selection and then insert both as a row into a new Table.
I have this code in IronPython that returns the start_date and end_date correctly based on the marking on selection. However, I don't know how to insert them into the table.
I have created a New_Table data table with columns Start_Date and End_Date and no available data at the moment.

from System.Collections.Generic import List
from Spotfire.Dxp.Data import *
from System.Globalization import CultureInfo
import System

# VarData is the existing table with data
dataTable = Document.Data.Tables["VarData"]
cursor = DataValueCursor.CreateFormatted(dataTable.Columns["Time"])


markings = Document.ActiveMarkingSelectionReference.GetSelection(dataTable)
markedata = List [str]();

# Iterate through the data table rows to retrieve the marked rows
for row in dataTable.GetRows(markings.AsIndexSet(),cursor):
    value = cursor.CurrentValue
    if value <> str.Empty:
        markedata.Add(value)

# Convert string dates to DateTime objects
date_format = "yyyy-MM-dd h:mm:ss tt" 
culture_info = CultureInfo.InvariantCulture
date_objects = [System.DateTime.ParseExact(date, date_format, culture_info) for date in markedata if date]

start_date = min(date_objects)
end_date = max(date_objects)

 

Link to comment
Share on other sites

 

Hi PBR,

Not tested but you should try something like this 👇
 

#add these imports
from System.IO import MemoryStream
from System.Text import Encoding, StringBuilder
from Spotfire.Dxp.Data.Import import TextFileDataSource,AddRowsSettings

#........

#at the end of your script
builder = StringBuilder()
builder.Append("start_date\tend_date")
builder.Append(start_date)
builder.Append("\t")
builder.Append(end_date)

stream = MemoryStream(Encoding.UTF8.GetBytes(builder.ToString()))
dataSource = TextFileDataSource(stream)

#This Constructor performs an automatic match, no columns are ignored.
settings = AddRowsSettings(Document.Data.Tables["new_table"], dataSource)
Document.Data.Tables[datablename].AddRows(dataSource,settings)
              

Let us know if it works

Link to comment
Share on other sites

23 minutes ago, Olivier Keugue Tadaa said:

 

Hi PBR,

Not tested but you should try something like this 👇
 

#add these imports
from System.IO import MemoryStream
from System.Text import Encoding, StringBuilder
from Spotfire.Dxp.Data.Import import TextFileDataSource,AddRowsSettings

#........

#at the end of your script
builder = StringBuilder()
builder.Append("start_date\tend_date")
builder.Append(start_date)
builder.Append("\t")
builder.Append(end_date)

stream = MemoryStream(Encoding.UTF8.GetBytes(builder.ToString()))
dataSource = TextFileDataSource(stream)

#This Constructor performs an automatic match, no columns are ignored.
settings = AddRowsSettings(Document.Data.Tables["new_table"], dataSource)
Document.Data.Tables[datablename].AddRows(dataSource,settings)
              

Let us know if it works

Thank you for the reply. I get this error on import: "ImportError: Cannot import name AddRowsSettings"

I am using Spotfire Analyst 12.0.4

Link to comment
Share on other sites

My bad.  here are the right imports

#add these imports
from System.IO import MemoryStream
from System.Text import Encoding, StringBuilder
from Spotfire.Dxp.Data.Import import TextFileDataSource
from Spotfire.Dxp.Data import AddRowsSettings


#........

#at the end of your script
builder = StringBuilder()
builder.Append("start_date\tend_date")
builder.Append(start_date)
builder.Append("\t")
builder.Append(end_date)

stream = MemoryStream(Encoding.UTF8.GetBytes(builder.ToString()))
dataSource = TextFileDataSource(stream)

#This Constructor performs an automatic match, no columns are ignored.
settings = AddRowsSettings(Document.Data.Tables["new_table"], dataSource)
Document.Data.Tables[datablename].AddRows(dataSource,settings)

 

Edited by Olivier Keugue Tadaa
Link to comment
Share on other sites

1 hour ago, Olivier Keugue Tadaa said:

My bad.  here are the right imports

#add these imports
from System.IO import MemoryStream
from System.Text import Encoding, StringBuilder
from Spotfire.Dxp.Data.Import import TextFileDataSource
from Spotfire.Dxp.Data import AddRowsSettings


#........

#at the end of your script
builder = StringBuilder()
builder.Append("start_date\tend_date")
builder.Append(start_date)
builder.Append("\t")
builder.Append(end_date)

stream = MemoryStream(Encoding.UTF8.GetBytes(builder.ToString()))
dataSource = TextFileDataSource(stream)

#This Constructor performs an automatic match, no columns are ignored.
settings = AddRowsSettings(Document.Data.Tables["new_table"], dataSource)
Document.Data.Tables[datablename].AddRows(dataSource,settings)

 

I am a little confused about the last two lines. Where datablename should be defined? is that a new document property like below?

datablename = Document.Data.Tables["new_table"]
settings = AddRowsSettings(Document.Data.Tables["new_table"], dataSource)
Document.Data.Tables[datablename].AddRows(dataSource,settings)

When I run above, I get this "TypeError: expected str, got DataTable"

Link to comment
Share on other sites

  • Solution
Posted (edited)

You should assign your data tablename variable with a string like this

datablename = "New_table"

see below 👇

 

#add these imports
from System.IO import MemoryStream
from System.Text import Encoding, StringBuilder
from Spotfire.Dxp.Data.Import import TextFileDataSource
from Spotfire.Dxp.Data import AddRowsSettings


#........

#at the end of your script
builder = StringBuilder()
builder.Append("start_date\tend_date")
builder.Append(start_date)
builder.Append("\t")
builder.Append(end_date)

stream = MemoryStream(Encoding.UTF8.GetBytes(builder.ToString()))
dataSource = TextFileDataSource(stream)
datablename = "new_table"

#This Constructor performs an automatic match, no columns are ignored.
settings = AddRowsSettings(Document.Data.Tables[datablename], dataSource)
Document.Data.Tables[datablename].AddRows(dataSource,settings)

# dispose the stream
stream.Dispose()

 

 

Edited by Olivier Keugue Tadaa
Link to comment
Share on other sites

3 hours ago, Olivier Keugue Tadaa said:

You should assign your data tablename variable with a string like this

datablename = "New_table"

see below 👇

 

#add these imports
from System.IO import MemoryStream
from System.Text import Encoding, StringBuilder
from Spotfire.Dxp.Data.Import import TextFileDataSource
from Spotfire.Dxp.Data import AddRowsSettings


#........

#at the end of your script
builder = StringBuilder()
builder.Append("start_date\tend_date")
builder.Append(start_date)
builder.Append("\t")
builder.Append(end_date)

stream = MemoryStream(Encoding.UTF8.GetBytes(builder.ToString()))
dataSource = TextFileDataSource(stream)
datablename = "new_table"

#This Constructor performs an automatic match, no columns are ignored.
settings = AddRowsSettings(Document.Data.Tables[datablename], dataSource)
Document.Data.Tables[datablename].AddRows(dataSource,settings)

# dispose the stream
stream.Dispose()

 

 

Thank you Olivier. This one worked in making new rows but it is not properly aligned under my table columns. I tried to fix it with this code:
It is creating new columns as Start_Date(2), End_Date(2), and keeps copying the names for every run.
If I import the datatable and uncheck all column, running the code creates Start_Date and End_Date, but I need to manually add the columns to table visualization. Then, running the code works well and keep generating rows under the right column. Maybe the formatting between the existing columns and new columns is wrong.

builder = StringBuilder()
builder.AppendLine("Start_Date\tEnd_Date")
builder.AppendLine("{0}\t{1}".format(start_date.ToString("yyyy-MM-dd HH:mm:ss"), end_date.ToString("yyyy-MM-dd HH:mm:ss")))

 

Link to comment
Share on other sites

1 hour ago, Olivier Keugue Tadaa said:

As you have pointed out, it should be a matter of data format. Can you check the type of the added columns Start_Date(2) and End_Date(2)? they are probably strings. If so, then I'll check the datatypes and try again.

 

The issue was the format of columns in imported datable. Thank you again

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...