imjoy-team / imjoy-rpc

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

Not work in pyodide #553

Open Nanguage opened 11 months ago

Nanguage commented 11 months ago

I tried to run imjoy-rpc in the pyodide environment:

import micropip
await micropip.install("imjoy-rpc")
from imjoy_rpc import api

class Plugin:
    async def setup(self):
        print("setup")

    async def run(self, ctx):
        print("run")
        print(ctx)

api.export(Plugin())

but the following error occurred.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/lib/python3.11/site-packages/imjoy_rpc/werkzeug/local.py", line 349, in __getattr__
    return getattr(self._get_current_object(), name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/imjoy_rpc/__init__.py", line 46, in __getattr__
    setup_connection(_rpc_context, connection_type, logger)
  File "/lib/python3.11/site-packages/imjoy_rpc/utils.py", line 583, in setup_connection
    manager.start(
  File "/lib/python3.11/site-packages/imjoy_rpc/connection/pyodide_connection.py", line 79, i
n start
    raise ex
  File "/lib/python3.11/site-packages/imjoy_rpc/connection/pyodide_connection.py", line 75, i
n start
    self._create_new_connection(target, on_ready_callback, on_error_callback)
  File "/lib/python3.11/site-packages/imjoy_rpc/connection/pyodide_connection.py", line 91, i
n _create_new_connection
    connection = PyodideConnection(self.default_config)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/imjoy_rpc/connection/pyodide_connection.py", line 182, 
in __init__
    self._post_message = js.sendMessage
                         ^^^^^^^^^^^^^^
AttributeError: sendMessage

Do I need to make some settings in the JS environment? If so, how should I do it?

Environment: https://pyodide.org/en/stable/console.html

oeway commented 11 months ago

You will need to load the imjoy core in the web page, so imjoy-rpc can connect to it. In that case, you can just use imjoy-core and load a plugin with type=web-python, this will automatically run Python plugin in pyodide.

If you don't want to do anything in the pyodide env, it's easier to just use imjoy_rpc.hypha to connect to the hypha server. Then you will have a system similar to imjoy.