Saving a dataset to csv

Hi all,

I'm running the following code. everything works until it gets to the csv portion. No error or messages just doesn't update the file. File exists on perspective session machine.

Any help would be appreciated.

def runAction(self, event):
	from random import random, randint
	
	# Get current dataset from equipment component
	currentData = self.parent.parent.parent.getChild("EquipmentSchedule").props.scheduledEvents
	
	# Get all data from the form
	eventLabel = self.parent.parent.getChild("cflexName").getChild("TextField").props.text
	itemId = self.parent.parent.getChild("cflexAsset").getChild("Dropdown").props.value
	startDate = self.parent.parent.getChild("cflexSdate").getChild("DateTimeInput").props.value
	endDate = self.parent.parent.getChild("cflexEdate").getChild("DateTimeInput").props.value
	
	# Create new event object
	item = {
		"label": eventLabel,
		"itemId": itemId,
		"eventId": str(randint(1000, 10000)),
		"startDate": startDate,
		"endDate": endDate
	}
	
	# Append event data to new object
	currentData.append(item)
	
	# Prepare for CSV export
	csv = system.dataset.toCSV(currentData, True, True)
	filePath = "C:\Users\<user_name>\Downloads\es_data.csv"
	system.file.writeFile(filePath, csv)

Perspective scripts run on the gateway so that path points to the C:\ drive on the gateway.

3 Likes

Since this is Perspective, that file will be saved to the gateway, as Perspective scripts run on the gateway and not the client machine.

You will need to use system.perspective.download | Ignition User Manual to deliver that file to the user.

2 Likes