TIBCO Cloud™ Spotfire® User Guide

IronPython example scripts

This topic lists a number of example scripts that can be executed by clicking on action links or buttons in the text area.

See also Adding a script action to a text area or visualization item for more examples and detailed instructions on how to add scripts to a text area, and see IronPython Scripting in Spotfire on the Community for many more examples, as well as tutorials, on using scripts in Spotfire.
Tip: If the expression parameter is tied to a string property value, you can use a property control (for example, a drop-down list) with a number of predefined expression alternatives to create an analysis where complex expressions can be assigned to visualization axes in an easy way. See Adding a property control to a text area for information about how to add property controls.
Tip: You can test your IronPython script by running it with Run Script, without your having to close the New Script dialog. Any objects marked by print in the script definition will be shown in the Output list. Click Undo to undo to remove any results from the test from the document when you are done.

Refresh of calculations, on-demand data and data functions

# Example script that refreshes a table driven by
# a calculation, a data function
# or an information link loaded on demand.
# The script takes a parameter "table" of type DataTable
if table.IsRefreshable and table.NeedsRefresh:
table.Refresh()

Add columns from an SBDF file to a data table in the current document

# This script adds columns from an sbdf file to a data
table
# in the current document.
#
# Four arguments are expected:
# table - The data table in the current document
# that columns will be added to.
# path - The path to the sbdf file with columns to add.
# ColumnNameInTable - A column in the data table.
# ColumnNameInFile - A matching column in the sbdf file.
# The columns are used to join new data to the current data.
from Spotfire.Dxp.Data.Import import SbdfFileDataSource
from Spotfire.Dxp.Data import AddColumnsSettings, JoinType,
DataColumnSignature, DataType
# Create the join condition map. The column with the name
# specified in ColumnNameInTable from the current data table
# will be joined with ColumnNameInFile in the sbdf file.
columnInTable =
DataColumnSignature(table.Columns[ColumnNameInTable])
columnInFile = DataColumnSignature(ColumnNameInFile,
DataType.String)
joinConditionMap = {columnInTable : columnInFile}
ignoredCols = [ ]
settings = AddColumnsSettings( joinConditionMap,
JoinType.InnerJoin, ignoredCols)
ds = SbdfFileDataSource(path)
table.AddColumns(ds, settings)

Use a specific visualization ("Visual") as input and then change the X-axis of the visualization

# Sets the value of the XAxis Expression of a visual.
# This script expects two arguments:
# visual - The visual to the set the X-axis expression on.
# expression - The expression to set.
from Spotfire.Dxp.Application.Visuals import VisualContent
# Get the content of the visual. Use the most general type
# so that the script works for all VisualContent classes that
# have an X-axis property.
vc = visual.As[VisualContent]()
vc.XAxis.Expression = expression

IronPython version

The latest supported version of IronPython is selected by default for new scripts. You specify the IronPython version per script, which means that an analysis can contain scripts with different versions. If your analysis contains scripts of an older IronPython version, you do not have to convert them into a newer IronPython version, unless you want to use script functionality that is available only in newer versions, or if the version becomes unsupported. For information regarding differences between IronPython versions, refer to the official IronPython documentation.

Execute in transaction

Clear the Execute in transaction check box to avoid executing the script in the document transaction. This can be necessary if the IronPython script contains operations that do not work inside a transaction.