LESSON

Scripting Data Source

Description

The Scripting Data Source is unique in that it can generate it's own dataset, or modifyi pre-configured data sources and parameters.

Video recorded using: Ignition 8.0

Transcript

(open in window)

[00:00] In this lesson we'll take a look at the scripting data source. Starting off in our Data tab of the report. I'll hit the little plus icon here and create a new Script data source. The Script data source allows you to return whatever data you want using a python script. Now, when we generate the report, we always call your scripting data source as a function here which means all of your code should be indented over. There are two built in arguments for the Script data source. We'll spend most of our time looking at the data argument. We see that there is a commented line down below. To begin, I'll just uncomment this line and we'll take a look at what this is doing. So the description up above states that data as a map, but functionally speaking you can treat it like a dictionary. This example here is creating a new data source called myKey and it's setting its value to the string Example. Let's head over to the Design tab so we can take a look at this new key. If I head on down to the Key Browser here we can take a look inside of our Data Sources folder and we see myKey, which of course I can drag and drop onto my page. This gives us a text shape because it's only returning a single value. And of course, if we go to preview it should say Example. Now, let's head back to the Data tab here and say we wanted to change the value of one of our pre-existing parameters up here. I'll add some new lines. So, we can type data, to reference the data argument, and then using normal dictionary notation we can reference the StartDate key. So each of our parameters and data sources are keys that are present inside of the data argument. If I want to change the value, I can simply assign a new value to this parameter. So in this case, I'm taking the value of our StartDate parameter, which by default is a date, and I'm replacing it with the integer value of 10. Let's head back to the Design tab and back in the Key Browser we know StartDate is a parameter so I'll go to the Parameters folder. I can drag and drop this key onto my page. And if we switch over to the preview tab we can see that its value has been replaced. I'll head back to the Data tab, and for this next example, I'm going to copy and paste a code snippet from the user manual. This example will be in the user manual under the system, dataset, toDataSet function in the appendix. But we're basically just creating a very simple one column data set with a couple of rows. Now, if I want to make this data source available as a key, I can simply reference the data argument. I'll create a new key, I'll call it myDataset. Then we'll pass in the variable we created on line 10. And you'll see in a moment that when we take a data set and assign it to a key in the data argument, that will create a result set for us instead of us a scalar value. Now if we head back to the Design tab here. Again, we can go back to the Key Browser, take a look under Data Sources, we'll see myDataset is listed here as a result set. So I can take this, drag, drop, which will create a table. Of course, we can do our normal column assignment here. Switch over to preview and it should show us one and two. Now for the last example here, let's head back to the Data tab. I want to create another data source and have my script data source reference the new data source in some way. So I'll simply hit the little plus icon here. I'll create a SQL Query. Now I'm going to type a quick little query here. I'm just going to have this query return the number 200 to us. So from my query data source here, I want to return 200 into my script data source. So let's switch back to the script data source. As a reminder for this whole section here, any sort of referencing from one data source to another or one parameter to another, always looks up. So in this case here, if I want my script data source to reference something under my query, I need to switch the position of these. The script data source needs to be at the bottom. So, I'll reorder, and now I can start referencing stuff inside of this query data source. So, we can come down below here. I'll add a couple lines. And again, we can type data. I'll create a new key, I'll call it fromQuery. Then let's reference our query data source. So I'll type data, and then the name query. Now again, normal dictionary rules apply here. So the capitalization here is important. The name of query here has to match the name of that data source. Now when you're trying to reference the value of a query data source, so if I look at my options here, all of those that end with the word query, they have a special little function you need to use to be able to get their underlying results set. I'll head back to that script we're working on, and the function is getCoreResults. This returns an Ignition data set. So now I can use my normal data set notation. So I could call getValueAt. Grab the first row and the first column. So now, we should have a new key called fromQuery which should have a value of 200. So we'll go back to the Design tab here. We see fromQuery which I'll just drag and drop onto the page, and if I go to preview it should give us 200. So, heading back to the Data tab here, see a couple different examples, but the main take away is that the scripting data source allows you to interact with your other data sources and parameters as well as create your own.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.