Jump to content

How to automatically run the Ironpython script without using a button?


PBR
Go to solution Solved by Jose Leviaguirre,

Recommended Posts

Hello,
I had an earlier post regarding writing a IronPython script to show the corresponding line chart of a feature layer shape when it is triggered. 

https://community.spotfire.com/forums/topic/18365-how-to-make-a-connection-between-mapchart-feature-layer-and-line-chart/#comment-53097

Currently I am using a button to run the Ironpython script after the shape is selected. Is there a solution to automatically trigger the script whenever the shape is selected or changes?

Edited by PBR
Link to comment
Share on other sites

hi PBR

the simplest way to achieve this is with a Data Function linked to a Document Property.

first, create a Document Property of type String -- i'll refer to it as "ScriptTrigger". configure ScriptTrigger to execute your IronPython script when its value changes.

next, create a Data Function that uses R script with the below code. on the Output tab, create an output parameter named "output" (to match the `output` variable in the script).

output <- Sys.time()

finally, go to the "Edit Parameters" dialog for the Data Function and on the Output tab, wire the output of the script to ScriptTrigger. make sure "Refresh Automatically" is ticked and save your changes.

now when you open the analysis, the Data Function will be executed, which will produce a new unique value, which will update the Document Property, which will trigger your script :)

Link to comment
Share on other sites

Posted (edited)

Hi @Niko Maresco
Thank you for your reply.
I followed the instruction, but I get this error:

"Could not execute function call 'AutoTrigger' (3)
Type mismatch. The value '2024-08-19 4:45:57 PM' (value type 'DateTime') cannot be assigned to the property 'ScriptTrigger' (data type 'String')."

I named the Document Property as ScriptTrigger with type string, and my data function is named AutoTrigger, and the output is a Value.

It should be noted that my IronPython script returns the calculated averages and shows them on the Y-axis of a line chart (x-axis is Time). 

Edited by PBR
Link to comment
Share on other sites

Thank you @Gaia Paolini

The error is not showing up anymore, but I don't understand why it does not trigger the ironpython script. The "refresh function automatically" is also checked. 

When I introduce the script to a button it works, but not through the data function.

Link to comment
Share on other sites

The cashed is unchecked.
I have a Mapchart with some shapes, and an Ironpython script to show the corresponding data for that shape on a line chart when the shape is selected. the changes are only the selection of the shapes on Mapchart. 

Link to comment
Share on other sites

Posted (edited)

This is a sample dxp of my problem. I know there are other ways to establish a connection between map chart and Line chart using the markings, but they are not my purpose at the moment.

Thank you very much

Sample.dxp

Edited by PBR
Link to comment
Share on other sites

  • Solution
Posted (edited)

Hello PBR, 

To be able to trigger your IronPython script, you also need an input parameter with an aggregating custom expression to be able to limit by marking. That way the data function will trigger when you mark a shape on your map. 

trigger_script_by_marking.thumb.gif.fba405e63571c1e352c0d37ee597324e.gif
 

  1. Create a Document property called "currentTime" or "ScriptTrigger" of any data type. Whatever makes more sense to you
  2. Create an R data function defined as Nico and Gaya described: "output <- toString(Sys.time())"
    • uncheck allow caching
    • define the input parameter to be 'output' as any data type
    • define the output parameter to also be 'output' as a "Value" type
  3. Run the data function and configure the script parameters and set it to run automatically
    • check the Refresh function automatically option
    • set the input parameter "output" to be an Expression to form a unique selection of records limited by marking: 
      note: to ensure the data function execution it requires an aggregation, but the data function will always output the current time as defined on the data function  script. The "DateTimeNow()" statement alone might not trigger the data function limited by marking.
      • expression:  UniqueConcatenate([Shapes].[id]) & DateTimeNow() 
      • check the Refresh function automatically option
      • limit by marked rows (use the same marking you use on your map)
      • optionally limit also by filtered rows using one of the filtering schemes from the dropdown
    • set the output parameter to be a Document property and select the "ScriptTrigger" or Document property

The ScriptTrigger Document property will change its value every time you make a selection with marking, so now you can set that Document property to trigger your IronPython script.
 

Sample-auto.dxp

Edited by Jose Leviaguirre
  • Like 3
Link to comment
Share on other sites

15 hours ago, Jose Leviaguirre said:

Hello PBR, 

To be able to trigger your IronPython script, you also need an input parameter with an aggregating custom expression to be able to limit by marking. That way the data function will trigger when you mark a shape on your map. 

trigger_script_by_marking.thumb.gif.fba405e63571c1e352c0d37ee597324e.gif
 

  1. Create a Document property called "currentTime" or "ScriptTrigger" of any data type. Whatever makes more sense to you
  2. Create an R data function defined as Nico and Gaya described: "output <- toString(Sys.time())"
    • uncheck allow caching
    • define the input parameter to be 'output' as any data type
    • define the output parameter to also be 'output' as a "Value" type
  3. Run the data function and configure the script parameters and set it to run automatically
    • check the Refresh function automatically option
    • set the input parameter "output" to be an Expression to form a unique selection of records limited by marking: 
      note: to ensure the data function execution it requires an aggregation, but the data function will always output the current time as defined on the data function  script. The "DateTimeNow()" statement alone might not trigger the data function limited by marking.
      • expression:  UniqueConcatenate([Shapes].[id]) & DateTimeNow() 
      • check the Refresh function automatically option
      • limit by marked rows (use the same marking you use on your map)
      • optionally limit also by filtered rows using one of the filtering schemes from the dropdown
    • set the output parameter to be a Document property and select the "ScriptTrigger" or Document property

The ScriptTrigger Document property will change its value every time you make a selection with marking, so now you can set that Document property to trigger your IronPython script.
 

Sample-auto.dxp 2.13 MB · 0 downloads

Hi Jose. That was a very wise solution. It worked well. 
Thank you very much.

  • Like 1
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...