How can I pass an Object from Javascript to Python

I have a JavaScript component that receives input from the user.

When the user clicks on the component, it triggers an event where I capture that event and I want to pass the event arguments to my Python method. I am calling my python method as such:
anvil.call(linkElement, "my_method", args)

Args is a JavaScript object with a bunch of properties and I keep getting the exception below. It appears that I have to extract individual properties from this object and pass it to my Python method. Is there anyway I can pass the whole object to my Python method?

I am getting the following exception:
Exception: Could not convert argument 0 (type ‘object’) to Python when calling ‘my_method’ from JavaScript.
at , line

Thanks
SS

UPDATE: You can now pass objects freely from Javascript to Python! You can even call methods and access attributes on your JS objects from Python - check out the updated docs for Using Javascript with Anvil.

(original, out of date post follows)
Anvil’s JS interop mostly expects JSON-format data (which it converts into dict/list/number/str/None objects). If you passed a JS object into Python code, the Python wouldn’t know what was safe to do with it!

So, yes, I’m afraid you’ll want to decompose the object into something JSON-friendly containing the information you wanted to pass into Python!

2 Likes