Jump to content

Question: How do you create a new table based on marked rows from another table?


Go to solution Solved by mike.mcgarvey,

Recommended Posts

Posted

This feels like it should be a simple task, but I keep going in error circles with Copilot trying to create an IronPython script to handle this. I want to make this pared down table so that I can add just those rows to a third table. Here is the script I'm starting with that is failing. It seems to fail when I call out "Marking". It seemed to work when using Document.ActiveMarkingSelectionReference, but the resulting table didn't match what was marked so I wanted to call out specifically the name of the marking I'm using. Any help would be greatly appreciated.

from Spotfire.Dxp.Data import DataFlowBuilder
from Spotfire.Dxp.Data.Import import DataTableDataSource
from Spotfire.Dxp.Data import *

# Source table name
tableName = "table1"
dt = Document.Data.Tables[tableName]

# Use specific marking called "Marking"
marking = Document.Data.Markings["Marking"]
dataSelection = marking.GetSelection(dt)
ds = DataTableDataSource(Document.Data.Tables[tableName], dataSelection)
dfb = DataFlowBuilder(ds, Application.ImportContext)
context = Document

flow = dfb.Build()

# Overwrite "MarkedLRP" table if it exists
if "MarkedLRP" in Document.Data.Tables:
    Document.Data.Tables.Remove("Markedtable1")

# Add data table
Document.Data.Tables.Add("Markedtable1", flow)
  • Solution
Posted

Of course, I figure it out almost as soon as I post it. Below is the working script.

from Spotfire.Dxp.Data import DataFlowBuilder
from Spotfire.Dxp.Data.Import import DataTableDataSource
from Spotfire.Dxp.Data import RowSelection, IndexSet

# Source
tableName = "table1"
dt = Document.Data.Tables[tableName]
markings = Document.Data.Markings["Marking"]

# Filter columns
ds = DataTableDataSource(Document.Data.Tables[tableName], markings)
dfb = DataFlowBuilder(ds, Application.ImportContext)
context = Document
flow = dfb.Build()

# All columns are imported, and only marked rows:
try:  # if the table already exists
    filtered_table = Document.Data.Tables["Markedtable1"]
    filtered_table.ReplaceData(ds)
except:  # if the table was not created before
    Document.Data.Tables.Add("Markedtable1", flow)
  • Like 2
Posted

I would actually suggest using a Python data function instead of an Ironpython script. For your use case, it would take less scripting and, I believe, would be faster as well.

The script could literally be this simple:

image.png.90982096a16fb52e9097ed4d32aa8e81.png

The Input Parameter would be inputTable, which is a table type parameter. The Output Parameter would be outputTable, which would also be a table type parameter. When you set the input parameter when you actually run the function, you can specify a filter scheme and a marking (or markings) to use. Then you can output to a new table (future runs of the function will automatically default to over-writing that table, so it won't just keep creating new tables). Also, you can select the Refresh function automatically and that will cause it to run whenever the marking changes, so you could have a user select rows and it'll automatically move that marked data to the specified table.

image.thumb.png.281b46112c3869b1c407606f150779dd.png

The only issue I can see is if no rows are marked when the function runs, since it can't really handle columns made entirely of null values, I can't remember what the workaround to that is off the top of my head, maybe someone else knows. I'd imagine the Ironpython script would have a similar issue.

  • Like 1
Posted

Thank you, barchiel33! That's a much better solution than the IronPython script! Much cleaner and great that it updates automatically. Already incorporated it and it works great!

  • Like 1

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...