|
| 1 | +Data Item Object |
| 2 | +================ |
| 3 | + |
| 4 | +.. _ItemREST: |
| 5 | + |
| 6 | +report_objects.ItemREST object |
| 7 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | + |
| 9 | +This object is a Python representation of an Ansys Dynamic |
| 10 | +Reporting data item object. When |
| 11 | +this object is created, a GUID will automatically be generated for the |
| 12 | +object and the date is set to the current time/date. |
| 13 | + |
| 14 | +Data members |
| 15 | +^^^^^^^^^^^^ |
| 16 | + |
| 17 | +The following attributes are available on an ItemREST object: |
| 18 | + |
| 19 | +- guid - string GUID. The default is ``str(uuid.uuid1())`` |
| 20 | +- tags - The user-defined tags string for this object. Multiple tags |
| 21 | + are space-separated. |
| 22 | +- sequence - An integer sequence number that can be used for |
| 23 | + sorting/indexing in a report |
| 24 | +- date - The time & date of the creation of this object. The default |
| 25 | + is: ``datetime.datetime.now(pytz.utc)`` |
| 26 | +- name - The name of the data object, a string |
| 27 | +- source - The source of the data object, a string |
| 28 | +- session - string GUID of a SessionREST object that already exists in |
| 29 | + the database |
| 30 | +- dataset - string GUID of a DatasetREST object that already exists in |
| 31 | + the database |
| 32 | + |
| 33 | + |
| 34 | +Methods |
| 35 | +^^^^^^^ |
| 36 | + |
| 37 | +**item.set_tags(tagstring)** |
| 38 | + |
| 39 | +Set the tags for the item to the passed string. Multiple tags are |
| 40 | +space-separated. |
| 41 | + |
| 42 | +**item.get_tags()** |
| 43 | + |
| 44 | +Returns the tags string for this object. Multiple tags are |
| 45 | +space-separated. |
| 46 | + |
| 47 | +**item.add_tag(tag, value=None)** |
| 48 | + |
| 49 | +Adds a tag to the current tag string. If no value is passed, the simple |
| 50 | +tag string is added to the tags string. If a value is specified, a |
| 51 | +string of the form tag=value will be added to the tag string. |
| 52 | + |
| 53 | +**item.rem_tag(tag)** |
| 54 | + |
| 55 | +Remove the tag (and any potential associated value) from the current tag |
| 56 | +string. |
| 57 | + |
| 58 | +**has_file = item.is_file_protocol()** |
| 59 | + |
| 60 | +This method returns True if the data item refers to an actual file on |
| 61 | +the server. Currently the ItemRest.type values of ItemREST.type_img, |
| 62 | +ItemREST.type_scn, ItemREST.type_anim and ItemREST.type_file all refer |
| 63 | +to files. |
| 64 | + |
| 65 | +Once all of the metadata attributes listed above are set, an actual data |
| 66 | +payload needs to be set for the data item. There are convenience methods |
| 67 | +to set the item type and fill in the payload data. |
| 68 | + |
| 69 | +**content = item.get_payload_content()** |
| 70 | + |
| 71 | +For Items that have been fetched using the Server object, this method |
| 72 | +allows you to get the payload without having to manually decode the |
| 73 | +payload data. |
| 74 | + |
| 75 | +An example of the use of this method is shown below: |
| 76 | + |
| 77 | +.. code-block:: python |
| 78 | +
|
| 79 | + from ansys.dynamicreporting.core.utils import report_remote_server, report_objects |
| 80 | +
|
| 81 | + serverobj = report_remote_server.Server("http://localhost:8000/", "nexus", "cei") |
| 82 | + obj_list = serverobj.get_objects( |
| 83 | + objtype=report_objects.ItemREST, query="A|i_type|cont|string;" |
| 84 | + ) |
| 85 | +
|
| 86 | + # Previously you had to do this to get the data of an item and then decode it to view human readable content |
| 87 | +
|
| 88 | + # import pickle** |
| 89 | + # data = pickle.loads(obj_list[0].payloaddata) |
| 90 | +
|
| 91 | + # This method gives you the human readable content directly (handles decoding internally.) |
| 92 | + data = obj_list[0].get_payload_content() |
| 93 | +
|
| 94 | +
|
| 95 | +Animation Item |
| 96 | +'''''''''''''' |
| 97 | + |
| 98 | +**item.set_payload_animation(mp4_filename)** |
| 99 | + |
| 100 | +This method sets the item payload to an animation. The "mp4_filename" |
| 101 | +argument should be the name of a .mp4 encoded video file. Note: the file |
| 102 | +must exist on disk before this call is made and must stay on disk until |
| 103 | +the item is pushed to the ADR Nexus server. |
| 104 | + |
| 105 | +File Item |
| 106 | +''''''''' |
| 107 | + |
| 108 | +**item.set_payload_file(filename)** |
| 109 | + |
| 110 | +This method sets the item payload to the content of an arbitrary file on |
| 111 | +disk. The argument should be the name of a file to be uploaded. Note: |
| 112 | +the file must exist on disk before this call is made and must stay on |
| 113 | +disk until the item is pushed to the ADR Nexus server. |
| 114 | + |
| 115 | +HTML Item |
| 116 | +''''''''' |
| 117 | + |
| 118 | +**item.set_payload_html(html_text)** |
| 119 | + |
| 120 | +This will set the item payload to HTML formatted text. |
| 121 | + |
| 122 | +Image Item |
| 123 | +'''''''''' |
| 124 | + |
| 125 | +**item.set_payload_image(image)** |
| 126 | + |
| 127 | +This method sets the item payload to an image. The argument can be one |
| 128 | +of three things: the binary representation of a .png file on disk as a |
| 129 | +string, a QImage object or an enve.image object. Examples are shown |
| 130 | +below: |
| 131 | + |
| 132 | +- A string which is the binary data representation of the image. Note: |
| 133 | + this is the only format supported in a Python interpreter that lacks |
| 134 | + the PyQt and enve modules. |
| 135 | + |
| 136 | + .. code-block:: python |
| 137 | +
|
| 138 | + with open("example.png", "rb") as fp: |
| 139 | + img = fp.read() |
| 140 | + item.set_payload_image(img) |
| 141 | +
|
| 142 | +
|
| 143 | +- A Qt QImage object instance |
| 144 | + |
| 145 | + .. code-block:: python |
| 146 | +
|
| 147 | + from PyQt4 import QtGui |
| 148 | +
|
| 149 | + img = QtGui.QImage("example.png") |
| 150 | + item.set_payload_image(img) |
| 151 | +
|
| 152 | +
|
| 153 | +- An enve image object instance |
| 154 | + |
| 155 | + .. code-block:: python |
| 156 | +
|
| 157 | + import enve |
| 158 | +
|
| 159 | + img = enve.image() |
| 160 | + if img.load("example.png") == 0: |
| 161 | + item.set_payload_image(img) |
| 162 | +
|
| 163 | +
|
| 164 | +None Item |
| 165 | +''''''''' |
| 166 | + |
| 167 | +**item.set_payload_none()** |
| 168 | + |
| 169 | +By default an item has no payload. This method will reset the item to |
| 170 | +that state. It is legal to push an item without a data payload into the |
| 171 | +server. |
| 172 | + |
| 173 | +Scene Item |
| 174 | +'''''''''' |
| 175 | + |
| 176 | +**item.set_payload_scene(filename)** |
| 177 | + |
| 178 | +This method sets the item payload to the 3D geometry found in the passed |
| 179 | +filename. Supported geometry formats include: EnSight CSF, STL, PLY, |
| 180 | +SCDOC and AVZ format files. |
| 181 | + |
| 182 | +String Item |
| 183 | +''''''''''' |
| 184 | + |
| 185 | +**item.set_payload_string(string)** |
| 186 | + |
| 187 | +This will set the item payload to an ASCII string. |
| 188 | + |
| 189 | +Table Item |
| 190 | +'''''''''' |
| 191 | + |
| 192 | +**item.set_payload_table(dictionary)** |
| 193 | + |
| 194 | +This will set the item payload to be a table, the table being specified |
| 195 | +in a dictionary. Minimally, the dictionary must contain a single numpy |
| 196 | +array with the 'array' key. There are a few restrictions on this array. |
| 197 | +First, it must be 2D. Second, the dtype of the array should be |
| 198 | +numpy.float32, numpy.double or a string (dtype="\|S20"). |
| 199 | + |
| 200 | +Other table properties (e.g. row/column labels, text formatting, etc) |
| 201 | +can also be set in this dictionary. A simple example: |
| 202 | + |
| 203 | +.. code-block:: python |
| 204 | +
|
| 205 | + import numpy |
| 206 | +
|
| 207 | + d = dict( |
| 208 | + array=numpy.zeros((3, 2), numpy.double), |
| 209 | + rowlbls=["Row 1", "Row 2", "Row 3"], |
| 210 | + collbls=["Column A", "Column B"], |
| 211 | + title="Simple table", |
| 212 | + ) |
| 213 | + item.set_payload_table(d) |
| 214 | +
|
| 215 | +
|
| 216 | +If the external Python API is being used from within EnSight, it is also |
| 217 | +possible to pass an ENS_PLOTTER object to the set_payload_table() |
| 218 | +method. It will capture not only the data in the plots, but many of the |
| 219 | +plotter attributes. One example might be: |
| 220 | + |
| 221 | +.. code-block:: python |
| 222 | +
|
| 223 | + plot = ensight.objs.core.PLOTS[0] # get the first ENS_PLOTTER object |
| 224 | + item.set_payload_table(plot) |
| 225 | +
|
| 226 | +
|
| 227 | +Many more table properties exist and can be set as the default values |
| 228 | +for a table by setting same-named keys in the dictionary. The properties |
| 229 | +are documented in the item properties section at `this`_ page. |
| 230 | + |
| 231 | +.. _this: https://nexusdemo.ensight.com/docs/en/html/Nexus.html?TableItem.html |
| 232 | + |
| 233 | +A short-cut APIs exists for a common case: |
| 234 | + |
| 235 | +.. code-block:: python |
| 236 | +
|
| 237 | + item.set_payload_table_values(array, rowlbls=None, collbls=None, title=None) |
| 238 | +
|
| 239 | +
|
| 240 | +This is a shortcut for the following two lines of python: |
| 241 | + |
| 242 | +.. code-block:: python |
| 243 | +
|
| 244 | + d = dict( |
| 245 | + array=numpy.array(array, numpy.double), |
| 246 | + rowlbls=rowlbls, |
| 247 | + collbls=collbls, |
| 248 | + title=title, |
| 249 | + ) |
| 250 | + item.set_payload_table(d) |
| 251 | +
|
| 252 | +
|
| 253 | +Note this can be handy for cases like: |
| 254 | + |
| 255 | +.. code-block:: python |
| 256 | +
|
| 257 | + item.set_payload_table_values([[1, 2, 3], [4, 5, 6]]) |
| 258 | +
|
| 259 | +
|
| 260 | +where one does not want to work with numpy and prefers to pass lists of |
| 261 | +lists. The core API will convert the list of lists into a 2D numpy array |
| 262 | +for the caller. |
| 263 | + |
| 264 | +It is possible to use a table of strings. To create a 2 row, 3 column |
| 265 | +array of strings (up to 20 characters), one might use code like this: |
| 266 | + |
| 267 | +.. code-block:: python |
| 268 | +
|
| 269 | + import numpy |
| 270 | +
|
| 271 | + array = numpy.array([["A", "B", "C"], [1, 2, 3]], dtype="\|S20") |
| 272 | + d = dict( |
| 273 | + array=array, |
| 274 | + rowlbls=["Row 1", "Row 2"], |
| 275 | + collbls=["Column A", "Column B", "Column C"], |
| 276 | + title="Simple ASCII table", |
| 277 | + ) |
| 278 | + item.set_payload_table(d) |
| 279 | +
|
| 280 | +
|
| 281 | +A numpy array of strings contains strings of all the same length. The |
| 282 | +maximum length must be specified using the 'dtype=' named argument when |
| 283 | +the array is created. |
| 284 | + |
| 285 | +.. _TreeItemDetails: |
| 286 | + |
| 287 | + |
| 288 | +Tree Item |
| 289 | +''''''''' |
| 290 | + |
| 291 | +**item.set_payload_tree(tree)** |
| 292 | + |
| 293 | +A tree payload consists of a list of "entities". Each entity is a |
| 294 | +dictionary with several required keys and potentially some optional |
| 295 | +ones. The required dictionary keys are: |
| 296 | + |
| 297 | +- 'name' - the text string that will be displayed in the tree view. |
| 298 | +- 'key' - a simple text string that can be used to specify the type of |
| 299 | + the entity. This value can be used to enforce a schema on the |
| 300 | + entities. This value is not displayed. |
| 301 | +- 'value' - the data item value for the entity. This can be the None |
| 302 | + object or an object of any of the following types: bool, int, float, |
| 303 | + str, datetime.datetime, uuid.UUID. |
| 304 | + |
| 305 | +optional keys include: |
| 306 | + |
| 307 | +- 'children' - this key can be set to another list of entities. These |
| 308 | + entities are 'children' of the entity with this key and their |
| 309 | + visibility is controlled by the visible state of this entity. |
| 310 | +- 'state' - if present, this key hints the generation engine that this |
| 311 | + entity node (or the nodes below it) should be initially displayed |
| 312 | + expanded or collapsed. Valid values include the strings: "expanded", |
| 313 | + "collapsed", "collapseRecursive" and "expandRecursive". |
| 314 | +- 'header' - this key may be set to a boolean and defaults to False. If |
| 315 | + it is present and set to True, the rendered row associated with this |
| 316 | + item will be displayed as bold text and with an enhanced bottom |
| 317 | + border line. |
| 318 | + |
| 319 | +The following example includes examples of all of the various options: |
| 320 | + |
| 321 | +.. code-block:: python |
| 322 | +
|
| 323 | + import datetime |
| 324 | + import enve |
| 325 | + import uuid |
| 326 | +
|
| 327 | + image_item = server.create_item(name="An Image", source="externalAPI", sequence=0) |
| 328 | + img = enve.image() |
| 329 | + if img.load("example.png") == 0: |
| 330 | + image_item.set_payload_image(img) |
| 331 | +
|
| 332 | + leaves = list() |
| 333 | + for i in range(10): |
| 334 | + leaves.append(dict(key="leaves", name="Leaf {}".format(i), value=i)) |
| 335 | +
|
| 336 | + children = list() |
| 337 | + children.append(dict(key="child", name="Boolean example", value=True)) |
| 338 | + children.append(dict(key="child", name="Integer example", value=10)) |
| 339 | + children.append(dict(key="child", name="Float example", value=99.99)) |
| 340 | + children.append(dict(key="child", name="Simple string", value="Hello world!!!")) |
| 341 | + children.append( |
| 342 | + dict(key="child", name="The current date", value=datetime.datetime.now()) |
| 343 | + ) |
| 344 | +
|
| 345 | + # this entity will display the image item (or a link to it) created above |
| 346 | + children.append( |
| 347 | + dict(key="child", name="A data item guid", value=uuid.UUID(image_item.guid)) |
| 348 | + ) |
| 349 | + children.append( |
| 350 | + dict( |
| 351 | + key="child_parent", |
| 352 | + name="A child parent", |
| 353 | + value="Parents can have values", |
| 354 | + children=leaves, |
| 355 | + state="expanded", |
| 356 | + ) |
| 357 | + ) |
| 358 | +
|
| 359 | + tree = list() |
| 360 | + tree.append( |
| 361 | + dict(key="root", name="Top Level", value=None, children=children, state="collapsed") |
| 362 | + ) |
| 363 | + item = server.create_item(name="Tree List Example", source="externalAPI", sequence=0) |
| 364 | + item.set_payload_tree(tree) |
| 365 | +
|
0 commit comments