Wednesday, June 17, 2015

Executing Python Script based on Marking Selection

Usually Spotfire allows us to execute the Python script on a Property Change or Action Control Button. However lot of times there is a need to execute a script based on markings in a visualization for better User experience. 

Prerequisites:-
 - Spotfire Marking concepts
 - Property Controls/Document Property
 - Python Script
 - Statistical Server in your project (TERR)


Below is one of the use case:
Lets consider a scenario where in we have 3 visualizations in a page and lets say the visualizations are Level 1, Level 2 and Level 3. Level 2 data is filtered based on marking selection from Level 1 and similarly Level 3 based on Level 2 marking selection.

Step 1: After the marking selection is made at Level 1 and Level 2, Level 3 visualization is populated accordingly. 

Step 2: The moment we change the marking of Level 1, Level 2 visualization data will get updated and even though if no marking is done at Level 2, Level 3 visualization is populated based on the marking selection made on level 2 at Step 1 (kind of stale marking selection) and users would get confuse with the data at level 3 as we made the different selection on Level 1 at Step 2.   

In order to show the Level 3 visualization as Blank whenever Marking is updated at Level 1 visualization, we would need to execute a python script for the same.


Below is the python script for reference


# Import required libraries
from Spotfire.Dxp.Data import *
from Spotfire.Dxp.Application.Filters import *

def resetMarking():
# Loop through each data table
     for dataTable in Document.Data.Tables:
# Selecting the datatable(s) for which we need to unmark
             if dataTable.Name=="2":
# Navigate through each marking in a given data table
                         for marking in Document.Data.Markings:
# Selecting the Marking(s) which we need to reset
                                   if marking.Name=="D2":  
                                         rows = RowSelection(IndexSet(dataTable.RowCount, False))
                                         marking.SetSelection(rows, dataTable)
resetMarking()

Lets assume we have created the python script and now the goal is to trigger the script each time whenever marking gets updated.

 Steps to trigger Python Script based on Marking:

1) Register a new Data Function(TERR) with the script as below
    DocProperty <- Input





2) Set the Input Parameters and Output Parameter types



3) Click on Run button

4) Identify a KEY column in the data table  & apply some Aggregation Function (like UniqueCount, UniqueConcat,Sum, etc as per your data) and consider it as Input for the R- script, so that we can assign this value to the Document Property.
While defining the R Script Parameters, select the Marking on which we would need to execute the python script as below



5) The unique value which is generated from the expression based on the Marking selection (step 4). Now, we are assigning this value to one of the Document Property as below,
 

6) Assign the Python Script which we have created to this Document Property


7) Make sure to check the box 'Refresh function automatically'

8) If R-Script identifies any value change in the Input Parameters (our Input Parameters are evaluated based on marking selection), it would assign the value to the Document property and thus there is a change in the document property, python script will be executed.

Hope this would solve the Problem..! :)





 

6 comments:

  1. nice blog . Thanks for sharing..

    ReplyDelete
  2. I'm not able to setup the output. What can be wrong?. Would be useful if you can give us a .dxp file for this script. Thanks

    ReplyDelete


  3. Well Done ! the blog is great and Interactive it is about interactive Executing Python Script based on Marking Selection it is useful for students and Python Developers for more updates on python follow the link

    Python Online course

    ReplyDelete
  4. This is really a good post. But if there are Table1 & Table2, Vis1 and Vis3 belong to Table 1 and Vis2 belongs to Table2. Vis2 values populate based on marking1 in Vis1. Vis3(Cross Table) values should display based on marking any cell values in Vis1(Cross Table , marking1) and all colorby values in Vis2(pie chart, marking2). Is it possible to populate values in Vis3 by only marking any values in Vis1 and without marking (all the pies) in Vis2 ? Is there any script and a way to trigger a script to mark all the pies in vis2 after you mark any cell values in cross table?
    Any help would be great. Thank You.

    ReplyDelete
  5. Thanks for Sharing a very Informative Post and I Must say its really helpful for us.your blog helps me to know more about the course.
    Python Course in Delhi


    Thanks for Sharing a very Informative Post and I Must say its really helpful for.your blog helps me to know more about the course.
    Python Course in Noida


    Thanks for Sharing a very Informative Post and I Must say its really helpful for.your blog helps me to know more about the course.
    Python Course in Gurgaon

    ReplyDelete