Customer Banners (Ads) - SpiceUp. AX and SpotfireX Disclaimer



If you find this site useful and you want to support, buy me a coffee   to keep this site alive and without ads.

Create a JavaScript programatically from IronPython

Create a Script

# Creates a JavasSript and adds it to the applicaiton document
from Spotfire.Dxp.Application.Scripting import ScriptDefinition, ScriptParameterCollection 
from Spotfire.Dxp.Application.Scripting.ScriptLanguage import JavaScript

#this is the JavaScript code we want to create
jsCode = '''
   function world(){
      alert("world!")
   }
'''

# 1. Creates the script
# 1.1 Define parameters (none in this example)
scriptParams = ScriptParameterCollection([])

# 1.2 Define the script with title, description, code, language, params and perform in transaction (not applies to JS)
scriptDef = ScriptDefinition.Create("myJavaScript","hello world",jsCode, JavaScript, scriptParams, False)

# 2. Adds the script to the application document
Application.Document.ScriptManager.AddScriptDefinition(scriptDef) 




List available scripts

# list available scripts and their coding language
scripts = Application.Document.ScriptManager.GetScripts()
for script in scripts:
   print script.Name, " ► ",script.Language.Language

# Get a specific script
script = Application.Document.ScriptManager.TryGetScript("myJavaScript" )
if(script[0]):
   print "script found!:", script[1].Name, script[1].Language.Language
else:
   print "script not found. Check the script name and try again"

Read the code from a script
script = Application.Document.ScriptManager.TryGetScript("renderImage")[1]
print (script.ScriptCode)


Attach a script form IronPyton to a text area

# Attach a script from IronPyton to a text area
# Attached and existing JavasSript from the Applicatin Document to a text area
from Spotfire.Dxp.Application.Scripting import HtmlTextAreaScript

# Get the script from the Document
script = Application.Document.ScriptManager.TryGetScript("myJavaScript" )[1]

# Prepare the script for the text area
htmlScript = HtmlTextAreaScript(script,{}) 

# Attach the script to the TextArea. ta is a TextArea Visualization script parameter
from Spotfire.Dxp.Application.Visuals import HtmlTextArea
vis = ta.As[HtmlTextArea]()
vis.Scripts.Add(htmlScript)




No comments: