Replies: 1 comment
-
I have more observations, but first a question for @PierreQuentel: QuestionIs there an explicit way to convert a brython object to a js object? We have 'to_dict' for converting js objects to python objects. Can we manually do the reverse? The only way mentioned in the docs is automatic conversion when calling a js function. I think my problem comes down to this: apparently brython doesn't evaluate nested objects when calling javascript functions. When a brython object is nested inside a js object, and js object is passed to a js function, brython doesn't do any type conversions. Is that accurate Pierre? If so, something like this would not be converted:
Perhaps above example is too simple; it seems to produce correct results. But more complex objects in observations below seem to suggest this happens. Read on if you want details... ObservationsIn line with my post above, it seems the best way to make a js-compatible array is the obvious one: python list. A simple However that doesn't solve my original issue. There's hidden magic here. Per the docs, Brython converts python objects to js objects when calling js functions. Brython knows to convert a list to an Array when calling console.log. Docs say this:
Problem revisedReturning to my root problem (see original post), here's my current working theory. The problem is not the array of tabs itself. The problem is assigning any non-primitive object to a property of a js object in brython. My evidence is this:
My GuessAltering the win.tabs array in brython makes win.tabs into a brython list object. The structure is then a js object (win) containing a brython object (tabs). When This explains why replacing tabs works when tabs array/list is returned directly, and fails when returning
ConclusionMy conclusion is: apparently brython doesn't evaluate nested objects when calling javascript functions. When a brython object is nested inside a js object, and js object is passed to a js function, brython doesn't do any type conversions. Maybe brython's behavior is the right approach, I don't know. All I can say is that it's hidden. Because brython-to-js type conversions are handled implicitly, it's hard to figure out what's going on. Would be nice if this behavior was documented somewhere, and if manual conversions to js were possible (hence question to Pierre at the top). Footnotes
|
Beta Was this translation helpful? Give feedback.
-
This one has me stuck, wondering if anyone can help.
I need to create a real js array in my brython code (reasons below). Let's say blist is my brython list object. Here's what I tried :
None of these work. The array object is always subtly different from a native js array. In particular, it has an attribute
__class__
that native js arrays lack. This__class__
attribute causes my deserialization problems.Does anyone have ideas for another way to make a js-native array in brython?? One without the
__class__
attribute.Demo
Here's the best way to see the difference. Go to brython console and enter:
Then open the js console and enter:
You'll immediately see the difference in javascript console:
Why do this?
It's not important but if you're curious...
I'm passing messages between a web page and an extension. The extension returns data via callbacks. Data gets serialized with JSON.stringify when it's passed across the extension boundary. Brython lists don't deserialize correctly.
I tried creating a real js array instead. Native js arrays do deserialize correctly. I have a brython script sending messages with callbacks, and a brython listener in the extension sending responses. Response data (array of tabs) is provided by callback from chrome.tabs.query.
As long I don't alter the tabs array and just pass it back verbatim, everything works fine (more or less). However, I need to filter the array a bit and only return matching items. This is where I run into problems.
If I do
tabs = [ t for t in tabs if condition (t) ]
then tabs is a brython list and deserialization fails. So instead I tried making a real js array as above. But the resulting array is not quite the same as a real js array (see class element) and doesn't deserialize correctly.Beta Was this translation helpful? Give feedback.
All reactions