Exporting Perspective Table data to CSV

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.

#      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?

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

system.dataset.toCSV() requires a dataset.

#      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)

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

1 Like

shizam!

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

1 Like

I got a table binded to a Named Query through a Custom dat property:
image

image

The name of the table is tblWorkRequest

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

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)

but doesn´t seem to work

What exactly doesn't work ?

I don´t see any downloading files.

Anything in the logs ?

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

Well there you go. Fix that and try again.

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

My fault, added the code

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)

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

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)
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'

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.

5 Likes
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.

you can use system.dataset.filterColumns | Ignition User Manual

to filter the visible columns only.

1 Like

Oh, great input, thanks a lot.

Ok, fixed!