# Exporting Perspective Table data to CSV

**URL:** <https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354>\
**Category:** Ignition\
**Tags:** perspective, scripting, ignition81\
**Created:** [April 24, 2024, 12:55pm UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354 "2024-04-24T12:55:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![devty](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@devty](https://forum.inductiveautomation.com/u/devty)\
**Post date:** [April 24, 2024, 12:55pm UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/1 "2024-04-24T12:55:49Z")

</div>

Hello,

I have been reviewing the forum posts on csv export from ignition and cant seem to find my use case so I figured I would open a topic.

I have a page with a power table and a little dock that pops out on right side to help the user toggle on/off which columns they want on the table. On that dock they also have an export button.

The export button invokes a perspective message called `ExportData` which is handled in the root of the page.

this is my ExportData handler code.

```python
# root > table-flex > body > table
	table = self.getChild("Table Flex").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
	colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
#	first row of data is headers
	data = [colsToKeep]
	for el in table.props.data:
		rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
		data.append(rowData)
	
	newCsv = system.dataset.toCSV(data)	
	system.perspective.download("Export.csv",newCsv)

```

for some reason when i export to csv i get 2 rows of data.

I was under the impression that if I feed toCSV() a list of lists with first row as header that i would get a csv output?

Any idea what i might be missing here?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 24, 2024, 1:00pm UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/2 "2024-04-24T13:00:43Z")

</div>

Perspective doesn't have a Power Table component. Vision does.  
In Perspective the component is called "Table".

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 24, 2024, 1:03pm UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/3 "2024-04-24T13:03:55Z")

</div>

[system.dataset.toCSV()](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-dataset/system-dataset-toCSV#keywords) requires a dataset.

```python
# root > table-flex > body > table
	table = self.getChild("Table Flex").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
	colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]

	data=[]
	for el in table.props.data:
		rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
		data.append(rowData)
	
	newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)	
	system.perspective.download("Export.csv",newCsv)

```

---

<div class="post-metadata">

**Author:** ![devty](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@devty](https://forum.inductiveautomation.com/u/devty)\
**Post date:** [April 24, 2024, 4:14pm UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/4 "2024-04-24T16:14:04Z")

</div>

my bad; i'm definitely working with perspective; been in vision land for past 3 years.

---

<div class="post-metadata">

**Author:** ![devty](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@devty](https://forum.inductiveautomation.com/u/devty)\
**Post date:** [April 24, 2024, 4:22pm UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/5 "2024-04-24T16:22:34Z")

</div>

shizam!

This is exactly what I was missing. Thanks, my app just leveled up!

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 6, 2024, 7:51am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/6 "2024-05-06T07:51:37Z")

</div>

I got a table binded to a Named Query through a Custom dat property:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/e/2e1e1bd52bb1a705f953fcd56f298a0d1688f864.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/b/2b686dac03d70f593e3eaf2a5972ef05e792ecac.png)

The name of the table is tblWorkRequest

And I trying to export it as csv and download it using:

```python
def runAction(self, event):

    table = self.getChild("tblWorkRequest").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
	colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]

	data=[]
	for el in table.props.data:
		rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
		data.append(rowData)
	
	newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)	
	system.perspective.download("Export.csv",newCsv)

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/2/b27510367aafe0bf2445baf3a8cda2dc9bfcc19b.png)

but doesn´t seem to work

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 6, 2024, 7:58am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/7 "2024-05-06T07:58:35Z")

</div>

What exactly doesn't work ?

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 6, 2024, 8:02am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/8 "2024-05-06T08:02:41Z")

</div>

I don´t see any downloading files.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 6, 2024, 8:03am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/9 "2024-05-06T08:03:17Z")

</div>

Anything in the logs ?

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 6, 2024, 8:06am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/10 "2024-05-06T08:06:35Z")

</div>

Unable to run action 'dom.onClick' on Page/AspectorPRL@D/Root Container/TabContainer/Page2/Button\_3: code could not be compiled.

org.python.core.PySyntaxError  
File "function:runAction", line 5  
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]  
^  
SyntaxError: mismatched input '' expecting DEDENT

Ignition v8.1.38 (b2024030513)  
Java: Azul Systems, Inc. 17.0.9

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 6, 2024, 8:09am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/11 "2024-05-06T08:09:54Z")

</div>

Well there you go. Fix that and try again.

If you need help fixing it, post the code instead of a screenshot.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 6, 2024, 8:10am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/12 "2024-05-06T08:10:48Z")

</div>

My fault, added the code

```python
def runAction(self, event):

    table = self.getChild("tblWorkRequest").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
	colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]

	data=[]
	for el in table.props.data:
		rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
		data.append(rowData)
	
	newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)	
	system.perspective.download("Export.csv",newCsv)

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 6, 2024, 8:11am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/13 "2024-05-06T08:11:47Z")

</div>

the first line of your script (`table = ...`) is indented using spaces, and the rest of the script uses tabs.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 6, 2024, 8:12am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/14 "2024-05-06T08:12:22Z")

</div>

```python
def runAction(self, event):

    table = self.getChild("tblWorkRequest").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
	colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]

	data=[]
	for el in table.props.data:
		rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
		data.append(rowData)
	
	newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)	
	system.perspective.download("Export.csv",newCsv)

```

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 6, 2024, 8:18am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/15 "2024-05-06T08:18:45Z")

</div>

```python
def runAction(self, event):

	table = self.getChild("tblWorkRequest").getChild("Body").getChild("Table")

	colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]

	data=[]
	for el in table.props.data:
		rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
		data.append(rowData)
	
	newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)	
	system.perspective.download("Export.csv",newCsv)

```

got this error detail:  
org.python.core.PySyntaxError  
File "function:runAction", line 13  
system.perspective.download("Export.csv",newCsv)  
^  
SyntaxError: no viable alternative at input 'system'

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 6, 2024, 8:21am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/16 "2024-05-06T08:21:14Z")

</div>

You're missing a closing parenthesis at the end of this line:  
`newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)`

You need to learn to debug this on your own, this is very basic and we can't keep doing it for you.  
And you won't ever learn if you don't try before asking.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 7, 2024, 8:54am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/17 "2024-05-07T08:54:11Z")

</div>

```python
def runAction(self, event):

	
	component = self.getSibling("tblWorkRequest")

	# Convert the dataset to CSV
	csv = system.dataset.toCSV(component.props.data)
	
	# Initiate a file download
	system.perspective.download("solicitudesOT.csv", csv)

```

This is working for me. Much simpler.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 7, 2024, 9:06am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/18 "2024-05-07T09:06:13Z")

</div>

you can use [system.dataset.filterColumns | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-dataset/system-dataset-filterColumns)

to filter the visible columns only.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 7, 2024, 9:07am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/19 "2024-05-07T09:07:15Z")

</div>

Oh, great input, thanks a lot.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [May 7, 2024, 9:10am UTC](https://forum.inductiveautomation.com/t/exporting-perspective-table-data-to-csv/89354/21 "2024-05-07T09:10:28Z")

</div>

Ok, fixed!
