Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 BUG: pyodide needs updating to support to_py for JS -> python conversion #3322

Open
thecapacity opened this issue Jan 3, 2025 · 4 comments
Labels
bug Something isn't working python Issues/PRs relating to Python Workers

Comments

@thecapacity
Copy link

Which Cloudflare product(s) does this pertain to?

Workers Runtime

What versions are you using?

wrangler 3.99.0

What operating system and version are you using?

Mac Sonoma 14.6.1

Please provide a link to a minimal reproduction

No response

Describe the Bug

This is all from an environment I built fresh on Jan 1 2025 - e.g. brew install node etc.

It appears pyodide is an alpha version:
pyodide Version: 0.26.0a2

print( f"{dir(pyodide)}") results in:

['ConversionError', 'IN_BROWSER', 'InternalError', 'JsArray', 'JsAsyncGenerator', 'JsAsyncIterable', 'JsAsyncIterator', 'JsBuffer', 'JsCallable', 'JsDomElement', 'JsDoubleProxy', 'JsException', 'JsFetchResponse', 'JsGenerator', 'JsIterable', 'JsIterator', 'JsMap', 'JsMutableMap', 'JsPromise', 'JsProxy', 'JsTypedArray', 'all', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', '_pyodide', '_pyodide_core', 'create_once_callable', 'create_proxy', 'destroy_proxies', 'register_js_module', 'sys', 't', 'to_js', 'unregister_js_module']

More specifically I'm looking for the to_py(...) function, so I can convert the data = await request.json() object from Javascript to Python. Supposedly, per ChatGPT, this function was introduced in pyodide version 0.21.0. But regardless it's in the latest version on their site.

https://pyodide.org/en/stable/usage/api/python-api/ffi.html#pyodide.ffi.JsArray.to_py

Please provide any relevant error logs

Error: PythonError: Traceback (most recent call last):
File "/lib/python312.zip/_pyodide/_base.py", line 629, in pyimport_impl
res = import(stem, fromlist=fromlist)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/session/metadata/entry.py", line 9, in
from pyodide.ffi import to_py
ImportError: cannot import name 'to_py' from 'pyodide.ffi' (/lib/python312.zip/pyodide/ffi/init.py)

at async Object.fetch (file:///Users/.../node_modules/miniflare/dist/src/workers/core/entry.worker.js:1029:22)
@thecapacity thecapacity added the bug Something isn't working label Jan 3, 2025
@thecapacity
Copy link
Author

I still think this is a valid bug but I think I found the way to do this:

        elif request.method == "POST":
            data = await request.json()
            data = data.to_py()

@thecapacity
Copy link
Author

Sorry meant to comment not close w/ comment

@emily-shen emily-shen added the python Issues/PRs relating to Python Workers label Jan 6, 2025
@penalosa penalosa transferred this issue from cloudflare/workers-sdk Jan 10, 2025
@dom96
Copy link
Collaborator

dom96 commented Jan 10, 2025

I've definitely used to_py with the version of Pyodide that is currently shipped in Python Workers. Can you share a more complete example of your worker code?

@hoodmane
Copy link
Contributor

to_py is a method on jsproxy objects, to_js is a function. I think everything is working as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Issues/PRs relating to Python Workers
Projects
Status: Untriaged
Development

No branches or pull requests

4 participants