You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, I cannot call toString() from pythonmonkey. I would like to do this, so I could access the function definition from inside python. If there is another way to access the function definition as a string that would be acceptable as well.
It is for the Folium library. Folium is a python library wrapping the javascript Leaflet library to generate maps. This is done by generating the html and javascript code for a Leaflet map. Users can configure many Leaflet objects by defining javascript functions (event handlers and stuff). Currently these functions are defined as strings in python code. Like this:
event_handler = JsCode("""
function (event) {
//
}
""")
This is not really comfortable. Editor and linting support is lacking and it is difficult to write unit tests.
Several users have asked for a way to "import" the javascript functions from a *.js file into python. The javascript functions are really defined as strings, which are then passed to the templating engine. If we could parse the javascript files and retrieve the function definitions this would be a trivial task.
I know this is probably not your core use case, but the PythonMonkey library is really fast and does almost everything else we need to implement this. It also has a really natural API for our purposes. I would be very grateful if you could add this. It does not seem like it would be terribly difficult to implement, but I do not know much about your code internals.
@hansthen
We plan on implementing being able to access JS function properties from python (such as toString), but in the meantime, to get a JSFunction as a string, you can do:
Thanks for the suggestion. Unfortunately, it does not seem to work in all cases. For me it returns this:
function() {
[native code]
}
Ah, I forgot that we bind b to a when we do a.b in python if a is a JSObjectProxy and b is a function in order to get methods to work properly, since the this value of a JS method is determined by how it is accessed, while in python the self value is determined at the moment the method is created, i.e. all methods in python are bound functions (pyodide also does this, see here: https://github.com/pyodide/pyodide/blob/ee863a7f7907dfb6ee4948bde6908453c9d7ac43/src/core/jsproxy.c#L388 )
@caleb-distributive I think our wrappers should have a better toString method if possible. '[native code]' could mention that it is an proxy and, ideally, also mentions the function name when it's available
Activity
philippedistributive commentedon Apr 30, 2024
@hansthen Please elaborate on the actual case to help us prioritise this feature request
hansthen commentedon Apr 30, 2024
It is for the Folium library. Folium is a python library wrapping the javascript Leaflet library to generate maps. This is done by generating the html and javascript code for a Leaflet map. Users can configure many Leaflet objects by defining javascript functions (event handlers and stuff). Currently these functions are defined as strings in python code. Like this:
This is not really comfortable. Editor and linting support is lacking and it is difficult to write unit tests.
Several users have asked for a way to "import" the javascript functions from a *.js file into python. The javascript functions are really defined as strings, which are then passed to the templating engine. If we could parse the javascript files and retrieve the function definitions this would be a trivial task.
hansthen commentedon Apr 30, 2024
I know this is probably not your core use case, but the PythonMonkey library is really fast and does almost everything else we need to implement this. It also has a really natural API for our purposes. I would be very grateful if you could add this. It does not seem like it would be terribly difficult to implement, but I do not know much about your code internals.
zollqir commentedon May 3, 2024
@hansthen
We plan on implementing being able to access JS function properties from python (such as
toString), but in the meantime, to get a JSFunction as a string, you can do:or, if you don't like dense one-liners:
This just calls
toStringin JS land, and returns the result back to python.hansthen commentedon May 3, 2024
Thanks for the suggestion. Unfortunately, it does not seem to work in all cases. For me it returns this:
philippedistributive commentedon May 7, 2024
Hi, we'll look into this further, eventually, no timeline for now besides by mid-summer
hansthen commentedon May 8, 2024
Thanks for the heads up.
zollqir commentedon May 8, 2024
@hansthen
Ah, I forgot that we bind
btoawhen we doa.bin python ifais a JSObjectProxy andbis a function in order to get methods to work properly, since thethisvalue of a JS method is determined by how it is accessed, while in python theselfvalue is determined at the moment the method is created, i.e. all methods in python are bound functions (pyodide also does this, see here: https://github.com/pyodide/pyodide/blob/ee863a7f7907dfb6ee4948bde6908453c9d7ac43/src/core/jsproxy.c#L388 )To get around this, you can do:
I made sure to actually run this code myself this time to check that it works 😉
wesgarland commentedon May 8, 2024
@caleb-distributive I think our wrappers should have a better toString method if possible. '[native code]' could mention that it is an proxy and, ideally, also mentions the function name when it's available