IronPython and JavaScript to refresh data tables at a set interval

  • Are you connecting to data sources that update frequently?
  • Would you like the DXP or web player file to reload data at a set interval?
  • Would you like the DXP or web player to reload without hitting the reload button?

A few weeks ago, I found myself in a situation where I wanted Spotfire to update every 5 minutes without having to hit the reload button.  This is possible with a combination of IronPython and JavaScript.  How does it work?  Well, the IronPython script is attached to a button that the JavaScript presses or triggers at a specified interval.  The interval is detailed in the JavaScript.

IronPython

Here is IronPython you can copy and paste, as well as a screenshot to show the indents.

from Spotfire.Dxp.Data import *
from Spotfire.Dxp.Data.Import import *

# Refresh all tables

for t in Document.Data.Tables:
if t.IsRefreshable:
t.Refresh()

JavaScript

And here is the corresponding JavaScript and screenshot.

$( document ).ready(function() {
setInterval(function(){

$(“#myBtn1 .sf-element”).trigger(“click”);
}, 150000);
});

 

Caveats

Of course, I am going to list out the detailed steps for how to make this work.  However, before I do that, I want to provide two warnings.

  1. Know your data set.  Know how long it takes to load.  If your data set takes 10 minutes to load, and you set the JavaScript interval to 5 minutes, your project will completely lock up and be unusable.  Similarly, if the data set takes 30 seconds to load and the interval is set to every 2 min, that means every 2 min, Spotfire will “pause” while the data loads.  Keep this in mind when setting the interval.
  2. When copying and pasting the JavaScript, make sure your copy has the right interval number.  The script is written in milliseconds.  If you copy and paste a super short interval, intending to change it later, it can still break your file.  The first time I applied this JavaScript, the interval was set to refresh every 3 seconds (on accident of course, didn’t realize it was milliseconds).  Everything broke.  The file locked up, and I had to kill it and start over.  DON’T REPEAT MY MISTAKES!
  3. UPDATE 2/28/2018. Since the original post, a reader posted in comments that this type of solution can cause an issue in multi-page DXPs, which is pretty much all DXPs.  You can read about this complication on the community site at this link and download a DXP with a solution.  I haven’t had a chance to review the solution just yet.

For more general information on the setInterval function, go to this link.  Please read this before implementing.  Here is a converter if you need it.

 

Steps

I always like to provide the dear reader with as much detail on how to make this work, so here are the step by step instructions.

  1. Add a Text Area to the page.
  2. Copy the IronPython script to the clipboard
  3. Right-click on the Text Area and select Edit HTML.
  4. Click the Add Action Control button to add a button to the Text Area.
  5. Name the button. Click the Script button.  Click the New button.  Name the script and paste from your clipboard.  Check your indents.  No parameters are necessary.
  6.  Go back to the Text Area and Edit HTML. Wrap the Spotfire control (the button) in a <div> tag.  Add the id as shown in the screenshot below.  Don’t forget to close the div tag.
  7. Copy and paste the JavaScript to a text editor. Adjust the script for the desired interval by changing the number shown in line 8 of the screenshot.
  8. Copy the JavaScript to the clipboard.
  9. Click the Add JavaScript button to add the JavaScript.  Click the New button.  Name the script and paste from your clipboard.  Make sure that the id matches the JavaScript. Check indents and spacing.

 

You shouldn’t need to even click the button to kick it off.  Upon opening, the JavaScript should run the refresh at the specified interval.

 

 

 

6 thoughts on “IronPython and JavaScript to refresh data tables at a set interval

      1. That is a good share. Thank you very much for the feedback. I’ll add this to the caveats section.

    1. Thanks for spotting that. There was a bit of time and space between scripting in Spotfire and writing the blog post. I calculated what I need at the time of scripting but didn’t get it correct in the write up.

Leave a Comment

Your email address will not be published. Required fields are marked *