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?
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.
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.
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)