imjoy-team / imjoy-rpc

The RPC library used in ImJoy.
MIT License
23 stars 5 forks source link

Pyodide `js.sendMessage` not defined #505

Closed thewtex closed 1 year ago

thewtex commented 1 year ago

When updating to use itkwidgets to use imjoy/pyodide in jupyterlite 0.1.0b17,

https://github.com/InsightSoftwareConsortium/itkwidgets/pull/612

the backtrace results:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 1
----> 1 from itkwidgets import view
      2 import imageio.v2 as imageio

File /lib/python3.10/site-packages/itkwidgets/__init__.py:4
      1 """itkwidgets: Interactive widgets to visualize images, point sets, and 3D geometry on the web."""
      3 from imjoy_rpc import register_default_codecs
----> 4 register_default_codecs()
      6 from .imjoy import register_itkwasm_imjoy_codecs
      7 register_itkwasm_imjoy_codecs()

File /lib/python3.10/site-packages/imjoy_rpc/utils.py:347, in register_default_codecs(options)
    344 if options is None or "zarr-array" in options:
    345     import zarr
--> 347     api.registerCodec(
    348         {"name": "zarr-array", "type": zarr.Array, "encoder": encode_zarr_store}
    349     )
    351 if options is None or "zarr-group" in options:
    352     import zarr

File /lib/python3.10/site-packages/imjoy_rpc/werkzeug/local.py:349, in LocalProxy.__getattr__(self, name)
    347 if name == "__members__":
    348     return dir(self._get_current_object())
--> 349 return getattr(self._get_current_object(), name)

File /lib/python3.10/site-packages/imjoy_rpc/__init__.py:46, in ApiWrapper.__getattr__(self, attr)
     44 if not self.__initialized:
     45     connection_type = os.environ.get("IMJOY_RPC_CONNECTION") or type_of_script()
---> 46     setup_connection(_rpc_context, connection_type, logger)
     47     self.__initialized = True
     48 return _rpc_context.api[attr]

File /lib/python3.10/site-packages/imjoy_rpc/utils.py:581, in setup_connection(_rpc_context, connection_type, logger, on_ready_callback, on_error_callback)
    574     manager = PyodideConnectionManager(_rpc_context)
    575     _rpc_context.api = dotdict(
    576         init=manager.init,
    577         export=manager.set_interface,
    578         registerCodec=manager.register_codec,
    579         register_codec=manager.register_codec,
    580     )
--> 581     manager.start(
    582         None,
    583         on_ready_callback=on_ready_callback,
    584         on_error_callback=on_error_callback,
    585     )
    586 else:
    587     if logger:

File /lib/python3.10/site-packages/imjoy_rpc/connection/pyodide_connection.py:79, in PyodideConnectionManager.start(self, target, on_ready_callback, on_error_callback)
     77 if on_error_callback:
     78     on_error_callback(ex)
---> 79 raise ex

File /lib/python3.10/site-packages/imjoy_rpc/connection/pyodide_connection.py:75, in PyodideConnectionManager.start(self, target, on_ready_callback, on_error_callback)
     73 def start(self, target="imjoy_rpc", on_ready_callback=None, on_error_callback=None):
     74     try:
---> 75         self._create_new_connection(target, on_ready_callback, on_error_callback)
     76     except Exception as ex:
     77         if on_error_callback:

File /lib/python3.10/site-packages/imjoy_rpc/connection/pyodide_connection.py:91, in PyodideConnectionManager._create_new_connection(self, target, on_ready_callback, on_error_callback)
     89 client_id = str(uuid.uuid4())
     90 connection_id.set(client_id)
---> 91 connection = PyodideConnection(self.default_config)
     93 def initialize(data):
     94     self.clients[client_id] = dotdict()

File /lib/python3.10/site-packages/imjoy_rpc/connection/pyodide_connection.py:182, in PyodideConnection.__init__(self, config)
    180 self.peer_id = str(uuid.uuid4())
    181 self.debug = True
--> 182 self._post_message = js.sendMessage
    184 def msg_cb(msg):
    185     data = msg.to_py()

AttributeError: sendMessage
thewtex commented 1 year ago

It turns out this was due to the imjoy extension note being loaded because of https://github.com/InsightSoftwareConsortium/itkwidgets/pull/628/commits/2793e1a67d283d9c9d56d57d5e11533d2ee1046d