-
Notifications
You must be signed in to change notification settings - Fork 40
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
implement toString on javascript functions #331
Comments
@hansthen Please elaborate on the actual case to help us prioritise this feature request |
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. |
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 import pythonmonkey as pm
module = pm.require("./my.js")
print(pm.eval("(func) => { return func.toString(); })")(module.hello)) or, if you don't like dense one-liners: import pythonmonkey as pm
module = pm.require("./my.js")
getFunctionString = pm.eval("(func) => { return func.toString(); }")
helloString = getFunctionString(module.hello)
print(helloString) This just calls |
Thanks for the suggestion. Unfortunately, it does not seem to work in all cases. For me it returns this:
|
Hi, we'll look into this further, eventually, no timeline for now besides by mid-summer |
Thanks for the heads up. |
Ah, I forgot that we bind To get around this, you can do: import pythonmonkey as pm
module = pm.require("./my.js")
unboundFunction = pm.eval("(module) => module.hello")(module)
getFunctionString = pm.eval("(func) => func.toString()")
print(getFunctionString(unboundFunction)) I made sure to actually run this code myself this time to check that it works 😉 |
@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 |
Describe your feature request here.
In javascript I can do the following:
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.Code example
The text was updated successfully, but these errors were encountered: