Skip to content
Discussion options

You must be logged in to vote

Hi @novitae - Here's one way to do it using globalThis after evaluating the functions.

importpythonmonkeypythonmonkey.eval("""function hello() {return "hello";};function world() {return "world";};""")
print(pythonmonkey.globalThis.hello())
print(pythonmonkey.globalThis.world())

which outputs

hello
world

A cleaner approach might be to use separate files and then use CommonJS to require the JavaScript file / module for use in your Python program

myjsfunctions.js

functionhello(){return"hello";};functionworld(){return"world";};exports.hello=hello;exports.world=world;

my_python_file.py

importpythonmonkeyevaluated=pythonmonkey.require('./myjsfunctions');
print(evaluated

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected bynovitae
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@novitae@wiwichips