Export to .csv using perspective 8.0

I have set up a transaction group and a SQL query along with export button. This currently saves a file on the server to a folder. How do I export the whole query straight from the browser to save as .csv right to my PC? This is what i am currently using to export.

You must use system.perspective.download() instead of system.file.writeFile.

Does not work. I tried with a name and without


Here is what I use on one of my buttons, may point you in the right direction.

	selection = self.getSibling("Type Dropdown").custom.Selected
	table = self.parent.parent.parent.parent.getChild("Right Container").getChild("Table").props.data
	data = system.dataset.toExcel(True, [table])
	filename = "%s Asset List.xlsx" % selection
	system.perspective.download(filename,data)	
1 Like

It is a browser download. You don't get to specify a folder, and the filename you supply is only a suggestion, not enforced. The browser will pop up a window asking the user where to save the file.

It is a browser download. You cannot control the final placement or name.

2 Likes

Tip: Post code - not pictures of code - and use the </> code formatting button to preserve code indentation and apply syntax highlighting. That way it will be much more legible and we can copy and paste the code into our answers.

1 Like

That is all I need at this point. I will try again tomorrow when I’m at the plant. My time working on this is limited unfortunately.

I had something similar I needed to do myself later on, so just went ahead and done it now. Hash out the parts you don't need and change the path and parameters for the named query. Right click on the named query in your list and select copy path, then paste that between the quotes. If you not using parameters you can delete them from the named query and select the appropriate line below.

	##Code to download a xlsx or csv file
	path = "Equipment/TestQuery"
	#If more params, comma seperate them inside the curley brackets or hash out if not using
	params = {'location':"USA"}
	ds = system.db.runNamedQuery(path, params)
    #if you are not using parameters use the below
    #ds =  system.db.runNamedQuery(path, {})
	#Select CSV or Xlsx code
	data = system.dataset.toCSV(ds)
	#data = system.dataset.toExcel(True, [ds])
	#Select csv or xlsx path
	#filename = "Downloaded File.xlsx"
	filename = "Downloaded File.csv"
	system.perspective.download(filename,data)
	#Write dataset to custom property to make sure named query is returning
	self.custom.queryresult = ds

https://docs.inductiveautomation.com/display/DOC80/system.db.runNamedQuery

1 Like