add_callback#

Session.add_callback(target: Any, tag: str, attr_list: list, method: Callable, compress: bool = True) None#

Register a callback with an event tuple.

For a given target object (such as "ensight.objs.core") and a list of attributes (such as ["PARTS", "VARIABLES"]), this method sets up a callback to be made when any of those attribute change on the target object. The target can also be an EnSight (not PyEnSight) class name, for example “ENS_PART”. In this latter form, all objects of that type are watched for specified attribute changes.

The callback is made with a single argument, a string encoded in URL format with the supplied tag, the name of the attribute that changed and the UID of the object that changed. The string passed to the callback is in this form: grpc://{sessionguid}/{tag}?enum={attribute}&uid={objectid}.

Only one callback with the noted tag can be used in the session.

Parameters:
targetobj, str

Name of the target object or name of a class as a string to match all objects of that class. A proxy class reference is also allowed. For example, session.ensight.objs.core.

tagstr

Unique name for the callback. A tag can end with macros of the form {{attrname}} to return the value of an attribute of the target object. The macros should take the form of URI queries to simplify parsing.

attr_listlist

List of attributes of the target that are to result in the callback being called if changed.

methodCallable

Callable that is called with the returned URL.

compressbool, optional

Whether to call only the last event if a repeated event is generated as a result of an action. The default is True. If False, every event results in a callback.

Examples

A string similar to this is printed when the dataset is loaded and the part list changes:

`` Event : grpc://f6f74dae-f0ed-11ec-aa58-381428170733/partlist?enum=PARTS&uid=221``

>>> from ansys.pyensight.core import LocalLauncher
>>> s = LocalLauncher().start()
>>> def cb(v: str):
>>>     print("Event:", v)
>>> s.add_callback("ensight.objs.core", "partlist", ["PARTS"], cb)
>>> s.load_data(r"D:\ANSYSDev\data\CFX\HeatingCoil_001.res")