Problem summing column's values of a table in the footer

Hello gentlemen, I have a problem with a table that I don't know how to solve:
I have a table that is populated by a sql query ==> return format as json:


I use this script in order to sum "Quantity" column on the footer (tnx to @pascal.fragnoud)

Here the results:

So far so good everything works fine!
I wanted to color the background of the rows based on a value.
I changed the return format of the query from json to dataSet, (I used a script by @ynejati).

Changing backgroundColor based on a value works, but now the sum "Quantity" in the footer column no longer works and I don't know where I'm going wrong.

Thanks for any suggestions

Why change the output to dataset to then re convert it to json ?
Keep the json as the base return, and add the style to it directly.
The sum doesn't work anymore because you changed the structure of the json.

I can't do much more right now, I have a sick kid to take care of, but I'll come back to it when I can.

Hi Pascal, good to hear from you.

  • "Why change the output to dataset to then re convert it to json ?"
    I'm trying to persuade my company to switch to Ignition from other scadas and then I have to show something charming :slightly_smiling_face:

  • "Keep the json as the base return, and add the style to it directly."
    Yes, you're right, I just need to figure out how to do it

  • "The sum doesn't work anymore because you changed the structure of the json."
    I imagined that was the problem, I just don't know how to point to the values of the new json structure.
    I'll focus on keeping the json as the base return and add the style directly.
    Can I possibly contact you if I can't?

I'm sorry that your kid is not well, I hope he gets well soon.

Bye.

Keep the conversation on the forum. That way everyone benefits!

1 Like

I recommend working through the examples in the Table documenation.
https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Table#PerspectiveTable-Examples

of course, I didn't mean in "pvt" :slightly_smiling_face:

Can you copy and paste what the data looks like ?
And I mean the actual data, not the binding. You can just right click on the property and select copy.

ok.
this one i "normal" json


this one instead is a dataSet converted to json

data transform script (with json source format):

return [
    {
        'style': {'backgroundColor': "#FF8A8A66" if row['quantita'] < 20000 else "none"},
        'value': row
    } for row in value
]

You might want to replace "none" by whatever default color you want.

footer's sum:

data = value.filtered or value.data
total = round(sum(row.value['quantita'] for row in value))
return "{:.9n} kg".format(total)

first one works great with return format json
footer's sum don't


it says "parse error in script transform"

Tip: while posting a screengrab is useful for seeing the context of your code, you should post the text of the code as well so that your readers can copy and edit it. See this topic if you're not familiar with code formatting on this site.

ok, I found an error...
should be:

  • total = round(sum(row.value['Quantita'] for row in data))

instead of:

  • total = round(sum(row.value['Quantita'] for row in value))
    in thist case I got the sum, but if I write somethind in the filter I got error

Hi, Pascal. Can you explain how this works? I've never seen it before!

yes, I'm not familiar with code formatting on this site.
I will try in next posts.
thanks

or is a boolean operator that acts as an expression, and automatically treats the inputs as booleans. So if the left hand expression is 'falsey' (such as an empty list, dict, or string), the right hand expression will be returned instead. It's equivalent to:
data = value.filtered if bool(value.filtered) else value.data
Or more imperatively:

if value.filtered:
  data = value.filtered
else:
  data = value.data
3 Likes

My bad, I went a bit too fast and didn't test it.
I can't spot the parse error from here, maybe an indentation mismatch ? make sure every line start with exactly the same indentation, It can be 13 spaces, 2 tabs, whatever you like, as long as it's consistent.
Also try without the format flags: "{}".format(total), see if there's not something wrong here...

But I can spot a logic error though: line 2 should end with for row in data instead of for row in value

It's basically coalesce. Lazy man null check ;p

Hi Pascal, I tried to modify your code al follow:

def transform(self, value, quality, timestamp):
	if value.filtered:
		data = value.filtered
		total = round(sum(row.value['Quantita'] for row in data))
	else:
		data = value.data
		total = round(sum(row.value['Quantita'] for row in data))
	return "{}".format(total)

and I got following message:
line 4, in Attribute Error: object has no attribure 'value'
So I modified the code as follow:

def transform(self, value, quality, timestamp):
	if value.filtered:
		data = value.filtered
		total = round(sum(v['Quantita'] for v in data), 0)		
	else:
		data = value.data
		total = round(sum(row.value['Quantita'] for row in data))
	return "{:.9n}".format(total)+" kg"

and finally everything works as expected.
wow, what a long day! :woozy_face:

thank you @pascal.fragnoud for your support
I hope your son is doing better

bye

3 Likes

Right, only one of those changed…
Glad you got it working.

Not a son, daughter, 20 months old and she’s crying right now at 4am :frowning:
It’s gonna be a very long day