Use CSS Variable instead of Python Script

I want to bind a parameter from the return of a python script.
But my script does not get evaluated, as you can see, I put "1" on expression and use a script transform - I used to do this but just found that this is bad strategy.

How can I get the script evaluated, when each parameters used on the script changes.

Transforms are only evaluated if their parent is re-evaluated. A static expression value like 1 is only evaluated once at first load. You need to use an expression structure binding and bind to all of your props you want to monitor for this to evaluate when these props change

I think You can also include all your props in a group (object/array) and just monitor the group prop itself

3 Likes

That is what I observed.
Thanks.
Time to practice expression structure.
I watched it a few times but didn't learn it.

15 posts were split to a new topic: Expression structure binding woes

You could use asList() in an expression binding to produce a composite of several "monitored" tags/properties to pass to your transform as value. Something like this:

asList({path.to.prop1}, {path.to.prop2}, {path/to/tag1}, {path/to/tag2})

if you do this, I recommend exploding back to local variables in the first line of the script transform:

def transform(self, value, quality, timestamp):
	prop1, prop2, tag1, tag2 = value
	.... the rest of the transform ....

This approach can also be used with expression transforms to ensure source values or sub-expressions are evaluated only once before inclusion in a larger expression.

Hi,

I am playing with expression structure now..
On expression Editor. I added tags. But how do you make the tags dynamic such as below:
I want to make the motor index number to be dynamic:
image
Is that even possible?

That would be an indirect tag binding.

Putting curly braces into a string doesn't create a binding. Click on the chain link icon in the left gutter next to the property to open the binding editor.

{ And consider going through Inductive University, so you get all of these basics at a steady pace. }

I see. but there is no indirect binding in this option only expression binding..
I made a work around by doing this on a custom parameter, where I can use Indirect Binding.

Most likely I am looking at the wrong spot, but I cannot find asList() in the funtions on the Edit Binding popup.

That’s because the asList() expression function is part of @pturmel‘a free SimulationAids Module.

I see, is there any native way to accomplish similar thing? I dont opose to use the SimulationAids module, but would need to consult with customers if they apporve.

No there isn’t a native way to do that in the expression language.

I suppose that you could use runScript to accomplish something similar, but that isn’t really a pure expression.

Of course, and you should. It is too useful to not deploy everywhere. IMNSHO.

3 Likes

Back to the original post.

I want to bind a python script. such as below.

This I only set once and not tend to change. Thus this does not need to be triggered in runtime.
I set 1 in the expression.
But I ran into issues where the parameter will be set to null.

Is there a property I can monitor that change during startup, to use it to trigger this script?

If it doesn’t need to be triggered at runtime then why have a binding at all?? That just doesn’t even make since.

Also, for what you have shown here, there is no need for a script transform, just use an expression.

Why do you need a custom script in this way? This isn’t really what bindings are intended for.

Because, the data source I am binding to the parameter comes from python variable. That is shared through out the project.

What data source?

Even in the new image that you provided after the edit, the script can easily be duplicated in a pure expression.

if(toInt({parameter}) % 2,{view.custom.theme.tableOdd},{view.custom.theme.tableEven})

If what you’re attempting to do is apply a theme style, I feel like you’re making it much more difficult than it needs to be.

You should be creating a theme file, style classes, or custom classes in a stylesheet, and then applying those to your components.

1 Like

@Eugene49 Just to be clear, you generally should not be assigning "magic" values like colour hex's directly to style properties like backgroundColo(u)r if you want to have any hope of maintaining a theme throughout your project. Ditch these in favour of Perspective Styles, as @lrose suggested

do you guys mean. If you want to set label's font color to green. you would rather create a style, set font color to green and apply the style?

Hypothetically, Say I have 4 layers of nested flex container.
Each layer uses different background color.
You would rather create 4 styles. Layer1 .. Layer4 styles. Apply these style to each flex container respectively.

What I did, I created 4 global variables in python, and pass those variables to background of each flex? To remain consistent project wide.
What is wrong with this?

Also, if I decided to create a css theme file.
Would I able to distinguishly identify the 4 flex container above and set different color to them?
Or theme can only be applied as per component type.

Anyway, my problem is not about the strategy above nor the choice to use expression vs python script.
Python is more readable for me. Is there an advantage favoring the use of expression?

My problem is, for some reason, the parameter forgets the value that was set to it, during saving/closing/reopening of view.. and since the binding does not get triggered, it returns an error.

Expressions are more performant than script transforms and will reduce the load on your gateway. This is especially important for larger systems

The whole scripting engine restarts every time you save the project.

Using Perspective Styles are far more performant than reading variables from script libraries within a script transform, they're also far more obvious and user friendly for other engineers when maintaining or extending a project. Design for performance and user experience first; script transforms should only ever be the last option if you can't achieve the same thing via other methods (e.g. tag / expression bindings)

Yes, I would create 4 styles in that case, assuming they're used in more than one place.

1 Like