Version:

This lesson is part of the Scripting in Ignition course. You can browse the rest of the lessons below.

Autoplay Off

LESSON

## Working with PyDatasets

Length: 9:09 min

Version:

#### Description

Learn how to use the Pydataset object.

Video recorded using: Ignition 8.1

#### Transcript

(open in window)

[00:00] In this lesson we'll examine how to manipulate PyDatasets through scripting. PyDatasets are very similar to regular datasets so we'll only emphasize some key differences between the two in this lesson. Datasets are built into ignition for their usefulness in handling data from databases as well as various parameter settings and components. The PyDataset is a wrapper type, more of a well made Python like data structure that can be used to make such datasets even more accessible in Python scripting. The key differences between PyDatasets and datasets have to do with looping, indexing and sizing methods. A general rule of thumb might be, if you're using lots of looping or iteration over something a PyDataset may make your life easier, otherwise you can just stick with a plain old dataset. The example shown here in the designer were already discussed in some detail in a prior lesson using datasets so we won't belabor all the details again here, instead we'll mostly emphasize the differences that arise from using PyDatasets instead.

[01:07] Much of what we'll see here will be similar to or the same as last time so we'll assume the viewer has already reviewed that other lesson first. However, in most cases we'll leave the prior code present but just commented out so it's easier to compare and contrast the newer PyDatasets syntax versus the dataset syntax. As a quick summary, this example contains a table of random data stored as a dataset along with six buttons, each of which carry out some typical scripting actions as noted on the buttons. Finally, as in the other lesson the approach taken here will be to first demonstrate each buttons action, then show how it's carried out using scripting. To expedite the demo flow note that we've already got each of the scripting windows opened but minimized down here in the task bar. So, we'll remain in the preview mode for the duration of this lesson. Okay, let's get to it.

[02:02] For this first example, we'll consider the show params button. As before when we run it, we see sizing info, column names, and data types displayed down here in the console. Now, if we open the updated script from down here in the task bar, we see one key difference and this will be common to all remaining examples so we'll examine it in detail only for this initial example. Here on line five, we extract the table's data as a dataset just as before but now, line six has been added and this time we convert that dataset into a PyDataset object using system.dataset.twopydataset but everything else about this script remains exactly the same and note that all of the API methods apply identically well to the data object here which is now a PyDataset object and no longer just a dataset. So the column and row counts, the column names and the data types all identical to before.

[03:06] So even though now we're working with a PyDataset object, the script has remained nearly identical. For the next example let's consider the show specific button. As before, if we set this to say index three, two, the third column, second row so this value right here, and we run the script we see this selected value displayed down here in the console. So then when we open up the revised script from the task bar, again now we're working with a PyDataset object from line six onward and the spinner indices on these two lines remain the same as before but on this last line, note how much simpler element indexing has become using a PyDataset object. We're just using simple bracketed row column indices as opposed to the original get value add method call we had to use before.

[04:02] This is one of the nice things about using PyDatasets, it allows for much simpler element indexing. For the next example, we'll look at the show all button and as in a prior lesson we see all the values printed out one per line. So when we open up the updated script, once again we're now working with a PyDataset object data after line six. But for the nested looping here at the bottom, look how much simpler the syntax has become using PyDatasets. We've got two very simple intuitive statements, line 15, four row in data and line 16 four value in row. Then we can simply print that value by name here on line 18 rather than having to use get value at in the row column indices as we did before on line 12. The syntax becomes much, much simpler using PyDatasets if we are looping. So that again is a nice advantage.

[05:02] For the next example we'll look at the average button and again, as in a prior lesson we see the two average values for columns one and three printed out here in the console. When we open up the updated script, once again we're now working with a PyDataset object data after line six, and we initialized the two counters as before one int and one double but this time with a PyDataset object data notice how much simpler that's made the looping. We no longer need to use the range or get value at functions as before, the looping is now done with this one simple statement on line 20, for row in data and the data access has become much simpler. We can now use bracketed index notation into each successive row object when incrementing. And at the end we have replaced the former get row count here on lines 26 and 27 with a much simpler len data on lines 30 and 31.

[06:11] For the next example let's consider this delete selected button. So recall from the prior lesson that we could take note of the current size of the dataset bound to this table which is right now 150 rows, then select some random row in the table and click delete, select it a couple times, I'll do that once, twice, three times and see that we have apparently deleted rows from the table and our dataset has in fact reduced down to 147 rows. So when we open up the updated script from the task bar, recall that because datasets are immutable what we had to do was delete a row from the original data to create a new dataset here on line 12, and then update the component with that smaller new dataset here on line 15. Well, we're doing all the same things as before this time, but notice that here on line 12 we're now using a PyDataset object data as interchangeable with the dataset that's really the main difference here, all else after line six is just the same.

[07:20] And for the final example we'll take a look at this copy selected button. Again, recall from a prior lesson that we could take note of the updated size of this dataset that's bound to this table which is now 147 rows then select some other random row in the table and click the copy selected button a couple times. I'll do that once, twice, three times, and see we have apparently copied the selected row into the table which we can verify by seeing that we're now back up at 150 rows in the dataset. So when we open the updated script from the task bar, again because datasets are immutable we need to create a new row from the selected row as we're doing on line 15 now and then we add a new row to create a new dataset as we're doing here on line 18 just as before, before we update the component with that augmented dataset here on line 19.

[08:19] The main difference now is here on line 15. Notice how much simpler the data access syntax is now. We can index into the PyDataset object data directly now using row and column indices no longer the get value add method as we had to do on line 12 before. And notice online 18 how we can use data as a PyDataset object interchangeably as we used a dataset before. So in this lesson we've seen how a dataset can be transformed into a PyDataset which is like a well made Python data structure. We've also seen how PyDatasets can streamline our scripting code considerably when using looping or indexing into data and how they can often be used interchangeably with datasets.

##### You are editing this transcript.

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

close
{"ft_lang_label":"__label__en","ft_lang_prob":0.9265222,"math_prob":0.8314764,"size":8613,"snap":"2022-40-2023-06","text_gpt3_token_len":1861,"char_repetition_ratio":0.1446161,"word_repetition_ratio":0.06411917,"special_character_ratio":0.21537212,"punctuation_ratio":0.0807783,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9603691,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-27T21:58:13Z\",\"WARC-Record-ID\":\"<urn:uuid:948cab95-b049-4c65-884d-d4a150d42d80>\",\"Content-Length\":\"86226\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:990176ed-fedb-41b8-b714-a78b593d5443>\",\"WARC-Concurrent-To\":\"<urn:uuid:cb7b7a4a-5571-4937-ba64-363c9e501cfd>\",\"WARC-IP-Address\":\"54.183.92.61\",\"WARC-Target-URI\":\"https://inductiveuniversity.com/videos/working-with-pydatasets/8.1\",\"WARC-Payload-Digest\":\"sha1:2GJFO4RSPSUMBR6LRMTSPZGXTHEOZDRG\",\"WARC-Block-Digest\":\"sha1:3VLSTJSPYZL63MJPPSPTYFTQ4E6BQCJV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764495012.84_warc_CC-MAIN-20230127195946-20230127225946-00561.warc.gz\"}"}