# Customizing Perspective Table and json scripting help

**URL:** <https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 27, 2022, 1:30pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348 "2022-07-27T13:30:56Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [August 24, 2022, 3:02pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/21 "2022-08-24T15:02:12Z")

</div>

[https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.18/index.html](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.18/index.html)

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 24, 2022, 3:28pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/22 "2022-08-24T15:28:13Z")

</div>

```auto
	def getStyle(row, col):
		if row%2==0:
			subStyle= {"backgroundColor": "white"}
		else:
			subStyle= {"backgroundColor": "blue"}
	
		if col=='Line':
					  subStyle= {"backgroundColor": "green"}
		return subStyle
	
		#"value": value.getValueAt(row, col),"style": getStyle(row, col)    
		#col: value.getValueAt(row, col),
	return [{col: value.getValueAt(row, col)	for col in value.getColumnNames()}	for row in range(value.rowCount)]

```

This shows my data, but it does not set the colors.

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [August 24, 2022, 3:33pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/23 "2022-08-24T15:33:36Z")

</div>

Why would it? You’re never calling `getStyle`

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 24, 2022, 3:38pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/24 "2022-08-24T15:38:44Z")

</div>

Sorry, I mean that no variation of trying to put the style into that comprehension is working for me, not that this specific script is not working.

I can iterate through the col and rows correctly, but I don’t understand how the JSON is supposed to look so that I can construct it.

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [August 24, 2022, 3:46pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/25 "2022-08-24T15:46:45Z")

</div>

the table expects this format:  
`{'value': 'your value here', 'style': 'your style here'}`

So just return something matching that format from your function and call the function in your list comprehension

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 24, 2022, 3:57pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/26 "2022-08-24T15:57:10Z")

</div>

[![Imgur](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/e/7e8c7b44bfad81605587c8b9f6dda85d9187bd0b.jpeg "imgur.com") ](https://imgur.com/mysxOSs)

I did something wrong.

```auto
value =[{'value': '20', 'style': {"fontSize": 36,"fontWeight": 500,"backgroundColor": "#a0bed8"}}]
return value

```

I also tried:

```auto

	value =[{'value': '20', 'style': 'fontSize": 36,"fontWeight": 500,"backgroundColor": "#a0bed8'}]
	return value

```

Still style became a column name, and string to the right became the value.  
Value became a column name, and 20 became the value in cell. They were in the reverse order though.

I appreciate the help, this is really confusing for me.

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [August 24, 2022, 3:58pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/27 "2022-08-24T15:58:49Z")

</div>

you need the column name added to your return

should be like:  
`value =[{'columnNameHere': {'value': '20', 'style': {'fontSize': 36,'fontWeight': 500,'backgroundColor': '#a0bed8'}}}]`

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 24, 2022, 4:02pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/28 "2022-08-24T16:02:27Z")

</div>

```auto
value =[{value.getColumnName(0):{'value': '20', 'style': 'fontSize": 36,"fontWeight": 500,"backgroundColor": "#a0bed8'}}]
return value

```

Table is empty. No error though, and data has a 0, which is named the column name, which has a value and a style.

Getting a component error though.

My idea is if I can get this simple format to work, then I can write code to write the other.  
I think that until I understand the json format, I won’t understand how the rest works.

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [August 24, 2022, 4:08pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/29 "2022-08-24T16:08:58Z")

</div>

I didn’t realize on my last reply, but your style property is still incorrect. It needs to be wrapped in a dictionary:  
`{'value': '20', 'style: {'fontSize': 36}}`

You also have a mix of single quotes and double quotes

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 24, 2022, 5:04pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/30 "2022-08-24T17:04:34Z")

</div>

`value =[{value.getColumnName(0):{'value': '20', 'style': {'fontSize": 36,"fontWeight": 500,"backgroundColor": "#a0bed8'}}}]`

This is correct formatting?  
I have a component error I think because I have created only one column.

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [August 24, 2022, 5:13pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/31 "2022-08-24T17:13:05Z")

</div>

> [@Matthew.gaitan](#):
>
> You also have a mix of single quotes and double quotes

hint hint hint

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 24, 2022, 5:32pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/32 "2022-08-24T17:32:59Z")

</div>

```auto
styleBluish = {'fontSize': 36,'fontWeight': 500,'backgroundColor': '#a0bed8'}
value = [{value.getColumnName(col):{'value': value.getValueAt(row,col), 'style': styleBluish} for col in range(value.getColumnCount())} for row in range(value.getRowCount())]
		
return value

```

I got them to all show up.

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [August 24, 2022, 5:37pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/33 "2022-08-24T17:37:24Z")

</div>

> [@zacharyw.larson](#):
>
> I got them to all show up.

👏  
Nice!

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 24, 2022, 5:59pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/34 "2022-08-24T17:59:30Z")

</div>

```auto
def getStyle(row, col):
			if row%2==0:
				subStyle= {'fontSize': 36,'fontWeight': 500,'backgroundColor': '#ffffff', 'borderStyle':'ridge'}
			else:
				subStyle= {'fontSize': 36,'fontWeight': 500,'backgroundColor': '#a0bed8', 'borderStyle':'ridge'}
		
			if value.getColumnName(col)=='Line':
						  subStyle= {'fontSize': 36,'fontWeight': 500,'backgroundColor': 'green', 'borderStyle':'ridge'}
			return subStyle

	
	value = [{value.getColumnName(col):{'value': value.getValueAt(row,col), 'style': getStyle(row, col) } for col in range(value.getColumnCount())} for row in range(value.getRowCount())]
		
	return value

```

I got the function to call as well for the style now.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 24, 2022, 6:17pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/35 "2022-08-24T18:17:06Z")

</div>

Whitespace is your friend for readability 🙂

```python
	def getStyle(row, col):
		baseStyle = {
			'fontSize': 36,
			'fontWeight': 500,
			'borderStyle': 'ridge' 
		}
		if value.getColumnName(col)=='Line':
			return dict(backgroundColor="green", **baseStyle)

		return dict(
			backgroundColor="#a0bed8" if row % 2 else "#ffffff", 
			**baseStyle
		)

	return [
		{
			value.getColumnName(col): {
				'value': value.getValueAt(row,col),
				'style': getStyle(row, col)
			}
			for col in range(value.getColumnCount())
		}
		for row in range(value.getRowCount())
	]

```

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [August 24, 2022, 6:30pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/36 "2022-08-24T18:30:01Z")

</div>

I struggle to comprehend list comprehensions when they are not in a single line.  
The other ways is easier for me to read.  
I don’t understand some of the changes too.

I was so happy 6 minutes ago. Also, I like to learn the things new to me too though.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 24, 2022, 6:36pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/37 "2022-08-24T18:36:49Z")

</div>

Regarding how to see `.getColumnNames()` as a possiblity, just run a `dir` or @pturmel 's inspect function on a dataset object and you would see all functions/attributes associated with the object type.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 24, 2022, 6:40pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/38 "2022-08-24T18:40:09Z")

</div>

Also mentioned in the manual:  
[https://docs.inductiveautomation.com/display/DOC81/Datasets#Datasets-AccessingDatainaDataset](https://docs.inductiveautomation.com/display/DOC81/Datasets#Datasets-AccessingDatainaDataset)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 24, 2022, 6:48pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/39 "2022-08-24T18:48:09Z")

</div>

Its a matter of writing DRY code.

In your original function you define three dictionaries that are basically the same. So @paul-griffith factored out the common things to a single dictionary `baseStyle`

Then he is using the dictionary constructor to return a new dictionary combining the background color key into the base style dictionary.

The `**` unpacks the `baseStyle` dictionary to be used as keyword arguments in the constructor.

So `dict(backgroundColor="green", **baseStyle)` essentially becomes

```auto
dict(backgroundColor="green",fontSize = 36, fontWeight=500, borderStyle = "ridge")

```

He also rearranged the if statements to short circuit (return early) if needed. For instance if the column name is `Line` it doesn’t matter if it’s an even or odd row, just return the “green” style.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 24, 2022, 6:51pm UTC](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348/40 "2022-08-24T18:51:22Z")

</div>

> [@zacharyw.larson](#):
>
> I struggle to comprehend list comprehensions when they are not in a single line.  
> The other ways is easier for me to read.

That's fair - obviously it's your code, so write it however you can read it now (and in six months 🙂).

Other than the whitespace, I really only did three things to your `getStyle` function.  
First, I extracted the common style settings; that prevents the duplication, which, again, _generally_ makes things easier to read and maintain down the road. Now you don't have to carefully read each condition to see if any of the other style properties are changing.  
That's this part:

```python
		baseStyle = {
			'fontSize': 36,
			'fontWeight': 500,
			'borderStyle': 'ridge' 
		}

```

Then, I put the `Line` branch _first_, and made it an early return. That's the same functionality as your existing code, just expressed in a different way.  
`return dict(backgroundColor="green", **baseStyle)`  
This line uses the Python builtin [`dict`](https://python.readthedocs.io/en/v2.7.2/library/stdtypes.html#dict) to create a _new_ dictionary. `dict` allows you to provide keyword arguments to construct a dictionary; so I'm passing in `backgroundColor` as a keyword argument, and then [unpacking](https://python.readthedocs.io/en/v2.7.2/tutorial/controlflow.html#unpacking-argument-lists) the `baseStyle` dictionary using `**`.

And finally, I changed the final `return` to use a ternary expression rather than a full if statement, because there's no difference between the two branches other than the color chosen:

```auto
		return dict(
			backgroundColor="#a0bed8" if row % 2 else "#ffffff", 
			**baseStyle
		)

```

This is also known as a [conditional expression](https://peps.python.org/pep-0308/).

[Previous page](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348.md?page=1)

[Next page](https://forum.inductiveautomation.com/t/customizing-perspective-table-and-json-scripting-help/62348.md?page=3)
