Jump to content
  • How to Dynamically Change Images in TextArea in Spotfire® Using IronPython Scripting


    Below is a sample IronPython Script on how to change images in Text Area based on the selection from a dropdown.

    Introduction

    Below is a sample IronPython Script on how to change images in Text Area based on the selection from a dropdown. For this example add a dropdown to the text area and then configure to execute the below script on the document property change

    Code Sample

    # Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license.
    
    from Spotfire.Dxp.Application.Visuals import *
    from System.IO import *
    from System.Drawing import *
    from System.Drawing.Imaging import *
    from System.Text.RegularExpressions import *
    #from System.Drawing.Imaging.ImageFormat import *
    
    vis=txtArea.As[HtmlTextArea]()
    
    
    
    image1 = Image.FromFile(r"C:SourceJapan.png");
    image2 = Image.FromFile(r"C:SourceSpain.png");
    image3 = Image.FromFile(r"C:SourceFrance.png");
    
    image1Exists=vis.Images.Contains("Japan")
    image2Exists=vis.Images.Contains("Spain")
    image3Exists=vis.Images.Contains("France")
    
    if not image1Exists:
    	vis.Images.Add("Japan",image1,ImageFormat.Png)
    if not image2Exists:
    	vis.Images.Add("Spain",image2,ImageFormat.Png)
    if not image3Exists:
    	vis.Images.Add("France",image3,ImageFormat.Png)
    
    content=vis.HtmlContent
    
    #check if image is already present
    htmlImage=content.Contains("img src")
    #Get which Image
    imageName=Document.Properties["showImage"]
    if htmlImage:
    	#code to replace the image
    	input = vis.HtmlContent;
    	oldValue = Regex.Match(input, ""(.+?)"").Groups[1].Value;
    	print oldValue
    	input = input.Replace(oldValue,imageName );
    	#print input
    	vis.HtmlContent=input
    	print vis.HtmlContent
    else:
    	addImage= "<img src=""+imageName+"" style="border: 0; width: 100px; height: 100px;"/>"
    	vis.HtmlContent=addImage+content
     

    References

    License:  TIBCO BSD-Style License

    Back to IronPython Scripting in TIBCO Spotfire Examples:  https://community.spotfire.com/s/article/IronPython-Scripting-in-Spotfire

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...