Jump to content

IronPython Script needs to automatically run every time filter panel changes


achen1
Go to solution Solved by David Boot-Olazabal,

Recommended Posts

Hello I have create this ironPython Script that takes the current filter panel settings and applies it to my data table called "MES_Calculations"

It then pulls the first value from the "longname" column  of my filtered data table and assigns this value as the title for all visualizations on my page.

It is working as expected HOWEVER

How can I make this script run every time there is a change on the filter panel? 

Filter Panel options that can be changed (screenshot below)

image.thumb.png.ffc5f27d67be3992d09c915733893ef5.png

image.thumb.png.380c19225aa9ce58139a50f7597c288d.png

Link to comment
Share on other sites

Hi @achen1,

I would create a calculated value (filtered by the corresponding filter scheme) with a RowCount() expression. 
This value will be pass to a document property using  javascript. Within the code you will save the value into the document property ONLY when its changes, this way the Iron Python script is triggered.

The javascript would be something like this:
 

var oldValue=0
transferValue = function(){

   //get value from calcVal spotfire Calculated Value Dyn Item
   value=$("#calcVal").text().trim()

   //update input when oldValue changes
   if(value!=oldValue){
      $("#inputPropertyControl input").val(value).blur(); 
   }
   oldValue=value
}

setInterval(transferValue,3000) //3 times per second

 And this your HTML
 

<span id="calcVal" style="display:none"><SpotfireControl id="95ca117d24b347a6923498cf495fb245" /></span>

<span id="inputPropertyControl" style="display:none"><SpotfireControl id="b98bc60cd1824746aacd1e1a747f42f9" /></span>


Your calculated value calcVal changes every time you change a filter in the filter panel

image.thumb.png.273a32d7e13744b70be02f565f4bc8f8.png

And the document property inputPropertyControl will store that value and run the sccript 

image.thumb.png.44f41ae00bf80176730321e81382a3f3.png

  • Like 2
Link to comment
Share on other sites

  • Solution

Hi Achen1, Vanessa,

Interesting solution! I've never seen that one, as I normally use a data function to cover this. It does the same in the end though, as it triggers the script to run on a change in the document property.
You can find more information about this solution here: https://spotfired.blogspot.com/2015/10/update-doc-property-trigger-ironpython.html

Kind regards,

David

Link to comment
Share on other sites

If you want a more efficient code (instead of checking every X seconds, you could use this one to be launched every time the calculated value changes.

 

//this is the target element to monitor changes
//just put the span id here. You can remove next line and add a script param called targetDomId
var targetDomId = "calcVal"

//no need to change after this line.
var target = document.getElementById(targetDomId)

//callback is the function to trigger when target changes
var oldVal = target.innerText.trim()
var callback = function(mutations) {
	
	newVal=$("#calcVal").text().trim();
	
   if(newVal!=oldVal){
          $("#inputPropertyControl input").val(value).blur();  
   }
   oldVal = newVal;

}

//this is to glue these two together
var observer = new MutationObserver(callback);

var opts = {
    childList: true, 
    attributes: true, 
    characterData: true, 
    subtree: true
}

observer.observe(target,opts);

 

  • Like 2
Link to comment
Share on other sites

You could also use a combination of a Python/TERR data function and your Ironpython script. Have the data function trigger automatically and when setting the input parameters make sure to include the filter scheme you're using. All this function would need to do is output a count of the rows in one of your columns, preferably a unique identifying column. Alternatively, you could have it just output the current system time. Either method should work fine. Then you can have your Ironpython script trigger off that document property. 

Assuming you go with a TERR function, you could create a data function with one input called input_col and one output called output. input_col would be of type column and allow any data type. output would have the Value type. The script would be:

output <- Sys.time()

You don't actually need to use the input_col here, it's just there to trigger the function. I think the system time should trigger the document property's change event, even if the data function triggers back to back in quick succession. I'd have to test to be sure, but I think it'd work even at the seconds level so if someone changed the filter and then immediately changed it again, it should still fire the change event.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...