User Importing and Exporting an Object From Perspective Project With 'Pretty' Formatting

Hello,

I have an object that is stored in a custom property in perspective that I want the user to be able to import and export as json formatted text. Correct me if I'm wrong, but as far as I know, there's no good way to download and upload a .txt file or pull from and push to the clipboard in Perspective. Because of that, I'm using a text area to copy and paste text to and from.

My problem is I'd like the text to have nice line breaks for easy reading. I currently have the text area text property bound to the desired object property and have it do a script transform return system.util.jsonEncode(value, 4), but there are no newlines in the text area. I see from other forum posts that the text area supports html line breaks, but I'm guessing that's not what jsonEncode is doing.

Is there a good way to get pretty formatting of an object in a text area, or more broadly, is there a better way to have the user import and export an object represented as text for easy editing?

If you want the string I think you can use formattedString = pp.pformat(mydict) to get the string.

Having said that there is also system.perspective.download - Ignition User Manual 8.1 - Ignition Documentation

1 Like

I tried pprint in the script console and in the tag binding and it's not creating new lines when I follow the usage shown in the documentation.

I thought to use system.perspective.download as well, but I can't import as there is no system.perspective.upload

I would probably recommend using the markdown component for this, then you can use HTML to format the text, including some styling for colors and other things, as you wish.

You must use the file upload component to upload files in perspective.

2 Likes

Also if you mean you can't import system.perspective in the script console that is because the script console is running on a variant of the vision scope, which doesn't have system.perspective.

No, I meant that there is no system.perspective.upload.

Irose pointed out that there is a file upload component thought, which I did not know.

1 Like

Here is the solution I landed on. It seems that system.util.jsonEncode struggles to properly format the perspective tree object type, so you can first convert it to normal python types and then call system.util.jsonEncode

def sanitize_tree(element):
    if hasattr(element, '__iter__'):
        if hasattr(element, 'keys'):
            return dict((k, sanitize_tree(element[k])) for k in element.keys())
        else:
            return list(sanitize_tree(x) for x in element)
    return element
	    
return system.util.jsonEncode(sanitize_tree(value),4)