How to create dataset in a script and pass it to a Flex Repeater?

Hello,

I’ve created this in Vision before, but cannot figure out how to get Perspective to do the same.
When clicking on a template in a window, a script would create a dataset and pass it as a parameter to another window when opening it. On the opened window a Template Repeater was bound to that dataset to populate the templates within it. Here is the script I used in Vision:

rackName = event.source.RackName
deviceName = event.source.DeviceName
fastNumber = event.source.FastNumber
gigabitNumber = event.source.GigabitNumber
header = [“PortName”,“RackName”,“DeviceName”]
data = []
i = 1
while (i <= fastNumber):
data.append(["Fast "+str(i), rackName, deviceName])
i+=1
i = 1
while ( i <= gigabitNumber):
data.append(["Gigabit "+str(i), rackName, deviceName])
i+=1
portData = system.dataset.toDataSet(header, data)

window = system.nav.openWindow(‘Device’, {‘DeviceName’ : deviceName, ‘RackName’ : rackName, ‘PortData’ : portData})
system.nav.centerWindow(window)
system.nav.closeParentWindow(event)

The above script worked just fine, but we are switching to perspective. I’ve used the Flex Repeater and it seems like it cannot take datasets, but only jsons. I’ve tried to get this to work with json, but I can’t figure out how to pass it to a parameter on a view and get the Flex Repeater to use it. Below is the (not working) script I came up with:

import json
rackName = event.source.RackName
deviceName = event.source.DeviceName
fastNumber = event.source.FastNumber
gigabitNumber = event.source.GigabitNumber

portData = “[”
i = 1
var = {}
while i <= fastNumber:
portName = "Fast " + str(i)
var.update({“RackName”: rackName})
var.update({“DeviceName”: deviceName})
var.update({“PortName”: portName})
portData += json.dumps(var) + “,”
i+=1
i = 1
while i <= gigabitNumber:
portName = "Gigabit " + str(i)
var.update({“RackName”: rackName})
var.update({“DeviceName”: deviceName})
var.update({“PortName”: portName})
portData += json.dumps(var) + “,”
i+=1

portData = s[0:len(portData) - 1]
portData += “]”

system.perspective.closePopup(’’)
system.perspective.openPopup(“DevicePopupID”,“Device”,params = {‘RackName’:rackName,‘DeviceName’: deviceName,‘PortData’: portData})

The Flex Repeater will populate itself if I copy and paste the json string (from the script) into the parameter by hand, but I can’t figure out how to pass the data from this script into the Flex Repeater automatically.
I would appreciate some advice.

I updated your scripts to be a little more concise. I haven’t used perspective much, so you may have to adjust the json a bit to fit the bill.

Vision Dataset:

rackName = "Rack1"
deviceName = "Device1"
fastNumber = 5
gigabitNumber = 5
counts = [fastNumber,gigabitNumber]
portNames = ["Fast","Gigabit"]
portInfo = zip(portNames,counts)
headers = ["PortName","RackName","DeviceName"]
data = []

for portName,count in portInfo:
	for i in range(1, count + 1):
		data.append(["%s %i" % (portName,i), rackName, deviceName])

portData = system.dataset.toDataSet(headers, data)

Perspective JSON:

rackName = "Rack1"
deviceName = "Device1"
fastNumber = 5
gigabitNumber = 5
counts = [fastNumber,gigabitNumber]
portNames = ["Fast","Gigabit"]
portInfo = zip(portNames,counts)
data = []
portDict = {}

for portName,count in portInfo:
	for i in range(1, count + 1):
		tempDict = {}
		tempDict["PortName"] =  "%s %i" % (portName,i)
		tempDict["RackName"] = rackName
		tempDict["DeviceName"] = deviceName
		data.append(tempDict)

portDict["Ports"] = data

json = system.util.jsonEncode(portDict)
print json
1 Like

Thanks for the answer, seems like I have a lot to learn yet with Python.

I finally got this to work properly by passing the “data” list to the component property rather than the json. Seems like ignition supports pasting json to an array by hand, but from a script it has to be a python list/dictionary.