Skip to main content
Skip Navigation LinksHome  Configuring the Spotfire Analysis  Configuring via Scripts

©Spotfire 2011

Configuring via Scripts

Custom scripts can configure any part of an analysis and add behaviors to it without creating and deploying extensions. The IronPython script language can access any part of TIBCO Spotfire available in the public API.

Overview

The TIBCO Spotfire add-in mechanism, which enables developers to extend the Spotfire feature set, is designed for the developers with access to a full-fledged development environment. Scripting does not replace this framework. It rather supplements the heavy-lifting programming with a light-weight option to configure Spotfire and extend its behaviors, a convenient and powerful means to solve simple as well as complex tasks.

The scripting language is IronPython version 2.0.2. It is modern, expressive and has access to the full public Spotfire automation API. Scripts are executed from an action control in the HTML text area. This also enables the use of scripting in the TIBCO Spotfire Web Player.

To create an analyses containing IronPython scripts, the analyst needs the Author Scripts license function. Scripts in analysis files that are shared via the library by licensed script authors are considered as trusted. For security reasons you should always examine and approve a script that is not listed as trusted before executing it.

See also:

Examples

This section provides a set of script snippets to start from or use to get up in speed scripting:

Changing the Title of the Active Visualization

All scripts can access the Application and Document objects as variables. To set the title of the active visual the script expects one argument, the title:

Document.ActiveVisualReference.Title = title

Note that the ActiveVisualReference is a regular part of the TIBCO Spotfire automation API, which is available in its entirety to IronPython scripts.

Changing the Filtering Scheme

This code snippet changes the filtering scheme of the active page to the one named Filters2:

from Spotfire.Dxp.Application.Filters import FilterPanel

# Get hold of the filter panel for the current page.
panels = Document.ActivePageReference.Panels
# The return value and out parameters are returned as a tuple in IronPython
# and can be bound with pattern matching.
(found, filterPanel) = panels.TryGetPanel[FilterPanel]()

#Find the filtering selection named "Filters2" and set it as current 
for filteringScheme in Document.FilteringSchemes:
    filteringSelection = filteringScheme.FilteringSelectionReference
    if filteringSelection.Name == "Filters2":
        filterPanel.FilteringSchemeReference = filteringScheme

Adding Columns from an SBDF File

To add columns from an SBDF file to a table, the following simple script will do:

# 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)

Refreshing a Data Table

The behavior of the red refresh button in the title bar of a plot can be implemented with the following script:

# This script 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()

Accessing Python Modules

Python standard modules, implemented for IronPython and packaged in the IronPython.Modules.dll, are available for use in Spotfire Python scripts. For instance, to use the re library, which is the Python library for regular expressions, a simple import re statement is all that is needed.

Python custom modules, py files, can be packaged as public resources in Spotfire modules and accessed by including them in the Python sys.path. The ModulesService is bound to a Python variable and can be used to determine the path to the directory containing a given public resource.
Assume that the MyModule.py file is exposed as a resource with the same name and contains the following function definition:

def renderToProperty(document, visual, propertyName, width, height):
	"Renders the visual to an image and updates the specified binary document property."
	from System.Drawing import Bitmap, Graphics, Rectangle, Point
	from System.Drawing.Imaging import ImageFormat
	from System.IO import MemoryStream, SeekOrigin
	from Spotfire.Dxp.Application.Visuals import VisualContent
	from Spotfire.Dxp.Data import BinaryLargeObject
	
	vc = visual.As[VisualContent]()
	bm = Bitmap(width, height)
	g = Graphics.FromImage(bm)
	r = Rectangle(Point(0,0), bm.Size)
	vc.Render(g, r)
	stream = MemoryStream()
	bm.Save(stream, ImageFormat.Png)
	stream.Seek(0, SeekOrigin.Begin)
	blob = BinaryLargeObject.Create(stream)
	document.Properties[propertyName] = blob

A script importing MyModule and calling renderToProperty to render a visual to a 500x500 pixels image placed in the my.image property, may look like this:

import sys
sys.path.append(ModulesService.GetResourceDirectoryPath("MyModule.py"))
from MyModule import renderToProperty
renderToProperty(Document, visual, "my.image", 500, 500)