Let’s say I have a large object on a components.custom property and want to display it, how would some of you do this? When I bind the object to a Label it’s quite hard to read.
I’d like to see the object in runtime in this format:
Any ideas or suggestions?
Markdown component paired with the jsonFormat
expression would be ideal, although I confess I’m not positive the expression will directly work with perspective objects at present. If that fails, you could do the same thing, but with Python’s builtin pprint
module instead; the script transform would just be:
import pprint
return pprint.pformat(value)
This is returning null for me in 8.1.1.
Oh, right. pprint
automatically dumps to stdout
. You want pprint.pformat
:
https://docs.python.org/2.7/library/pprint.html#pprint.pformat
Doens’t return null but returns a single line string
Using jsonFormat() expression seems to give me the format I want in the markdown.source viewer but onscreen it looks like a single line string.
I tried using json.dumps but that seems to create another set of issues in the Transform but works fine in the Script Console.
"```\n" + jsonFormat({value}) + "\n```"
in an expression transform works pretty well, although it’s reading the nested props as QVs…
2 Likes
That's odd but it beats anything else thus far. Thanks @PGriffith
For something you can use in a script transform (though you should probably put this into a project scoped script). Also, make sure you only attempt to use it from Perspective contexts:
from com.inductiveautomation.ignition.common import TypeUtilities
from com.inductiveautomation.perspective.common import PerspectiveModule
gson = PerspectiveModule.createPerspectiveCompatibleGson()
json = TypeUtilities.pyToGson(value, gson)
return "```\n{}\n```".format(gson.toJson(json))
The triple backtick surrounds are to tell the Markdown component that it’s preformatted text, and thus to preserve line breaks and render with a monospaced font - just like code blocks on the forum.
1 Like
I put it in the project library scripts and it works from the script console but when I call it from the markdown.source transform, I get ImportError: No module named perspective
A project library called format
, defined as:
from com.inductiveautomation.ignition.common import TypeUtilities
from com.inductiveautomation.perspective.common import PerspectiveModule
gson = PerspectiveModule.createPerspectiveCompatibleGson()
def json(value):
json = TypeUtilities.pyToGson(value, gson)
return "```\n{}\n```".format(gson.toJson(json))
Is working for me, in both the script console and a design session:
1 Like
Mimicked your setup but same import error… I’ll have to circle back around to this tomorrow and try again.
Wondering if I'm experiencing an issue with 8.1.1-STABLE that is not present in the version you're running?
Can't seem to get around the import error from this line:
Error reported:
ImportError: No module named perspective
Where are you running your code?
I am getting the same error about not being able to import perspective module in the property binding transform of the mark down component source.
I assume this only worked for @PGriffith because he was running Ignition in the IDE…
1 Like
Another solution, which involved more scripting, is to use the Tree component to display your JSON/Document data.
I went with the expression transform approach. Thanks.
Here is a way to do it without using an expression. @PGriffith can comment if there is anything bad here, but avoids the use of the other library that can’t be imported.
def toDict(perspective_object):
"""Formats a perspective object as Dict
Adapted from http://forum.inductiveautomation.com/t/ideal-way-to-display-objects-on-a-screen/41575
Args:
perspective_object: A perspective property object
"""
from com.inductiveautomation.ignition.common import TypeUtilities
# Convert the Perspective property object to Gson and back to a Py Object
return TypeUtilities.gsonToPy(TypeUtilities.pyToGson(perspective_object))
def toMarkdown(perspective_object, indent=3):
"""Formats a perspective object as Markdown
Args:
perspective_object: A perspective property object
"""
from json import dumps
json_string = dumps(toDict(perspective_object), indent=indent)
return "```\n{}\n```".format(json_string)
4 Likes