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)