So I have project script and a style in the Styles in the project. When I try to use the style I get the global name ‘status_red’ is not defined error. I used the path from the project, is there something special I must do to reference these?Thx, jake
How are you 'trying to use' it? Screenshots would be helpful.
What is the project script doing and how does it relate to using Perspective Styles? All you need to do to use a Style is to copy the path to it into a component's style.classes (there could be many of these at different levels in a component's prop depending on component but applying the style in the props.style.classes will apply for the *whole component) value. If course you can assign this dynamically via a binding or a script, but essentially they will just be writing the style path or paths into this
As a side note, consider creating 'status' as a folder in the styles, as in sure once you start using styles that you'll end up with many of them
There’s the script, it’s global to the project. Pretty much the only thing I use the styles for is bg color, so it didn’t seem useful to add a folder for them.Thx, jake
In the script, you’re trying to return variables called eg status_white
which you haven’t defined within your function. What the style classes property is expecting is a string with the style path in it. You need to return a string, e.g.
return 'status_white'
Ah, so in script if you set the style to a string it will look for the style in the project, I’ll try this now.Thx, jake
What calls the script? I assumed that it was somehow writing its return value into a style.classes property
A button calls onClick the Load(self) function, it passes itself so the session comes across.
You’re calling the script, but the return value is lost as you’re not assigning it to anything.
You should be assigning it to the style.classes property of whatever it is that you want to set the style for. E.g.
self.getSibling("Label").props.style.classes = Units.Load(self)
the GetColor function is called by the load script to get the color for the line, then that style is applied to each cell as the table is filled in. The load doesn’t return anything as it’s filling in the table directly.
Also, it’d be far more efficient to scrap the script and instead do all of this in an expression binding. The blocking tag reads are going to kill your performance
Ignore that, I posted at the same time you did.
I probably need to see your full Units script
So I’m not sure I can do that. The status along with the statusnormal tags determine the color, the other stuff I suppose could do bindings. This script was carried over from vision on 7.8.5, and it wan’t fast, but did everything I needed. I’m trying not to run through through the table twice, also, tags can disappear and reappear. And which tags fill in the table is determined by the login.
Not sure if you saw this as i edited after you replied
Are you able to edit it and paste back your code inside of three back-ticks (```). This will keep the formatting intact. Or copy in your code, select it, and press the preformatted button
oh, ticks, thought it was 3 dots, here you go:
‘’’
import time
def GetColor(unit):
d = system.tag.readBlocking([’[default]’ + unit + ‘.enabled’])[0].value
if d == False:
return ‘status_darker_blue’
s = system.tag.readBlocking([’[default]’ + unit + ‘/Status’])[0]
if s.quality.isGood() == False:
return ‘status_light_grey’
else:
fp = ‘[default]’ + unit + ‘/Faulted’
if False:
#if system.tag.exists(fp) == True:
f = system.tag.readBlocking(fp)[0]
if f.value == True:
color = ‘status_light_grey’
else:
color = ‘status_white’
else:
sn = system.tag.readBlocking([’[default]’ + unit + ‘/StatusNormal’])[0]
norms = sn.value.split(’,’)
color = ‘status_red’
for norm in norms:
if s.value == int(norm):
color = ‘status_white’
break
return color
def Load(caller):
units_tag = caller.session.props.auth.user.userName
hide_customer = False;
if units_tag == ‘TOPSDemo’:
hide_customer = True
units = system.tag.readBlocking(['[default]' + units_tag + '/Units'])[0].value.split(',')
unitList = caller.getSibling("Units")
#ds = system.dataset.clearDataset(unitList.props.data)
ds = []
for unit in units:
#system.gui.messageBox(unit)
color = GetColor(unit)
company = ""
location = ""
volts = ""
d = system.tag.readBlocking(['[default]' + unit + '.enabled'])[0].value
if d == False:
comploc = system.tag.readBlocking(['[default]Disabled-' + unit])[0].value.split(',')
company = comploc[0]
location = comploc[1]
volts = "NA"
else:
if hide_customer == True:
company = 'NA'
location = 'NA'
else:
company = system.tag.readBlocking(['[default]' + unit + '/Company'])[0].value
location = system.tag.readBlocking(['[default]' + unit + '/Location'])[0].value
compv = system.tag.readBlocking(['[default]' + unit + '/CompMainsVoltage'])[0]
if compv and compv.quality.isGood() == True:
volts = "%4.1f" % compv.value
else:
volts = "NA"
data = [{"value": unit, "style": color}
,{"value": company, "style": color}
,{"value": location, "style": color}
,{"value": volts, "style": color}]
ds.append(data)
unitList.props.data = ds
‘’’
import time
def GetColor(unit):
d = system.tag.readBlocking(['[default]' + unit + '.enabled'])[0].value
if d == False:
return 'status_darker_blue'
s = system.tag.readBlocking(['[default]' + unit + '/Status'])[0]
if s.quality.isGood() == False:
return 'status_light_grey'
else:
fp = '[default]' + unit + '/Faulted'
if False:
#if system.tag.exists(fp) == True:
f = system.tag.readBlocking(fp)[0]
if f.value == True:
color = 'status_light_grey'
else:
color = 'status_white'
else:
sn = system.tag.readBlocking(['[default]' + unit + '/StatusNormal'])[0]
norms = sn.value.split(',')
color = 'status_red'
for norm in norms:
if s.value == int(norm):
color = 'status_white'
break
return color
def Load(caller):
units_tag = caller.session.props.auth.user.userName
hide_customer = False;
if units_tag == 'TOPSDemo':
hide_customer = True
units = system.tag.readBlocking(['[default]' + units_tag + '/Units'])[0].value.split(',')
unitList = caller.getSibling("Units")
#ds = system.dataset.clearDataset(unitList.props.data)
ds = []
for unit in units:
#system.gui.messageBox(unit)
color = GetColor(unit)
company = ""
location = ""
volts = ""
d = system.tag.readBlocking(['[default]' + unit + '.enabled'])[0].value
if d == False:
comploc = system.tag.readBlocking(['[default]Disabled-' + unit])[0].value.split(',')
company = comploc[0]
location = comploc[1]
volts = "NA"
else:
if hide_customer == True:
company = 'NA'
location = 'NA'
else:
company = system.tag.readBlocking(['[default]' + unit + '/Company'])[0].value
location = system.tag.readBlocking(['[default]' + unit + '/Location'])[0].value
compv = system.tag.readBlocking(['[default]' + unit + '/CompMainsVoltage'])[0]
if compv and compv.quality.isGood() == True:
volts = "%4.1f" % compv.value
else:
volts = "NA"
data = [{"value": unit, "style": color}
,{"value": company, "style": color}
,{"value": location, "style": color}
,{"value": volts, "style": color}]
ds.append(data)
unitList.props.data = ds
I think you used apostrophes. Back ticks are the character on the tilde (~) button
the bottom character