Editing or Creating a Perspective View via Script

In case this helps anyone, here’s how you can modify (or create) a Perspective View (or any non-binary resource really) via script. You can call this from any gateway scope, including from a Perspective Button action.

Note: the gateway only checks for file updates to resources every 5mins, so you won’t see changes in your client until then, but they will come through automatically.

projectName = '' # e.g. 'AwesomeProject'
viewPath = '' # e.g. 'Testing/View to Modify'

from com.inductiveautomation.ignition.gateway import IgnitionGateway
context = IgnitionGateway.get()
projectParentFolder = str(context.systemManager.dataDir.absoluteFile).replace('\\','/') + '/projects'	
projectFolder = projectParentFolder + '/' + projectName
viewFolder = projectFolder + '/com.inductiveautomation.perspective/views/' + viewPath
viewJsonFile = viewFolder + '/view.json'

# read the view resource file as a string
viewJsonStr = system.file.readFileAsString(viewJsonFile)
# convert it to Python objects (lists and dicts)
viewJsonObj = system.util.jsonDecode(viewJsonStr)

##### make edits to the view. #####
# change the text of a Label component on the page
viewJsonObj['root']['children'][0]['props']['text'] = self.getSibling("TextField").props.text

# add a tag binding to the label's display property
propConfig = {
				"position.display":
				{
					"binding":
					{
						"config":
						{
							"fallbackDelay": 2.5,
							"mode": "direct",
							"tagPath": "[default]Test/Flow PV"
						},
						"type": "tag"
					}
				}
			}

labelObj = viewJsonObj['root']['children'][0]
if 'propConfig' in labelObj.keys():
	labelObj.update(propConfig)
else:
	labelObj['propConfig'] = propConfig

##########

viewJsonStr = system.util.jsonEncode(viewJsonObj)
system.file.writeFile(viewJsonFile, viewJsonStr)
5 Likes

ive used something similar to read data from all the project files, but i didnt think to write to it aswell, could be usefull to add in a bunch of bindings

Yeah, I did something to walk through all of the CSS files and then pull out all CSS variables and display them in a flex repeater as a crude style sheet. Definitely handy to be able to write binding config especially if you have a large number for example bindings on SVG elements, Map component layer Views (e.g. passing the current map zoom level to the Views), etc.

1 Like

i used it to read the props.text of all components in all the views to fill up my translation manager xd

2 Likes

Not bad, I think I’ll do that.