Summary
This article explains how to execute one IronPython script from within another. This is useful for creating "utility scripts" (e.g., a standard logging or calculation script) that can be reused across your analysis without rewriting code.
Introduction
Often it is useful to be able to call one IronPython script from another. For example, you may define some useful utility scripts that are used in multiple places in other scripts.
Code Sample
The script below uses the ScriptManager to look up a script by name. The parameters are then defined and the script is executed.
# Copyright © 2017. TIBCO Software Inc.
# Licensed under TIBCO BSD-style license.
# Author: Andrew Berridge
#Example on how to call a script
from System.Collections.Generic import Dictionary
from Spotfire.Dxp.Application.Scripting import ScriptDefinition, ScriptLanguage
import clr
# Get a reference to the script
# Note: this is an interesting example of how
# to use an OUT parameter in IronPython
# - the out parameter is the scriptDef
# - an "empty" variable is created by using
# the clr (Common Language Runtime) to generate
# a reference to a ScriptDefinition object
scriptDef = clr.Reference[ScriptDefinition]()
Document.ScriptManager.TryGetScript("Name of Script", scriptDef)
# Defines the values of the parameters
paramDict = {"MyParam":"Value1",
"Parameter2":"Value2"}
params = Dictionary[str, object](paramDict)
# At this point, scriptDef will be an instance of StrongBox[ScriptDefinition]
# There is an ExecuteScript method that expects an instance of ScriptDefinition.
# StrongBox is confusing, but easily sorted by
# getting scriptDef.Value - this is the instance of ScriptDefinition that
# the StrongBox references
# Explicitly use IronPython 2.7.7 to ensure correct execution
Document.ScriptManager.ExecuteScript(scriptDef.ScriptCode, ScriptLanguage.IronPython277, params)License: TIBCO BSD-Style License
Comments
0 comments
Article is closed for comments.