Search code examples
ironpythonspotfire

Is there a way to make an IronPython script that takes the data in a filtered column, and sends it to another page?


I have this situation within my dashboard, where I can choose parameters to filter a table with:

enter image description here

And I need to export this data to a JSON file, using a button. This button should read the data on my table and write it down to a JSON file for sharing.

So far, what I have been able to make is not much:

from System.IO import File
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers

writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.StdfDataWriter)

table = Document.Data.Tables['']

filtered = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet()

stream = File.OpenWrite("C:\Users\user\Desktop\TIBCO\Test_JSON.json")

names =[]
for col in table.Columns:
    names.append(col.Name)
writer.Write(stream, table, filtered, names)
stream.Close()

The problem with this code above is that it does not export out what I have marked, but all of the data in the table.


Solution

  • If the only problem is that it does not export marked items, this minor modification should work. You were exporting filtered items.

    from System.IO import File
    from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
    
    writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.StdfDataWriter)
    
    table = Document.Data.Tables['xxx']
    
    selection='marking'
    if selection=='filter':
        selected = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet()
    else:
        selected = Document.ActiveMarkingSelectionReference.GetSelection(table).AsIndexSet()
    
    #print('n. selected',len(list(selected.GetEnumerator())))
    
    stream = File.OpenWrite("C:\Temp\Test_JSON_"+selection+".json")
    
    names =[]
    for col in table.Columns:
        names.append(col.Name)
    writer.Write(stream, table, selected, names)
    stream.Close()