A high-performance Python library that relies on nodejs to execute JavaScript
Project description
A high-performance Python library that relies on nodejs to execute JavaScript
Environments
Please note that you need to install Node.js and set up the NODE_PATH or NODE environment variable properly!
If not set, the default will use the Node.js in the system's path.
Installation
pip install pyevaljs4
Quickstart
import pyevaljs4
# example 1
rt = pyevaljs4.compile_("function f(a,b){console.log(a,b);return a+b;}")
res = rt.call('f', 'x', 'y')
print(res)
assert res == "xy"
# Another way of passing parameters
res = rt.call('f', arg_list=['x', 'y'])
print(res)
assert res == "xy"
# Other equivalent calling methods
print(rt.f('x', 'y'))
print(rt.f(arg_list=['x', 'y']))
# close runtime
rt.close()
# example 2
with pyevaljs4.compile_("function f(a,b){console.log(a,b);return a+b;}") as rt:
res = rt.call('f', 'x', 'y')
print(res)
assert res == "xy"
# example 3
# empty context
rt = pyevaljs4.compile_()
# Add the function f to this context
rt.eval("function f(a,b){console.log(a,b);return a+b;}")
res = rt.call('f', 'x', 'y')
print(res)
assert res == 'xy'
# Other calling methods
res = rt.eval("f('x', 'y')")
print(res)
assert res == "xy"
rt.close()
# example 4
# Call JS asynchronous function
rt = pyevaljs4.compile_()
rt.eval("async function f(a,b){console.log(a,b);return a+b;}")
# To call the JS asynchronous function, you need to pass async_js_func=True
res = rt.call('f', 'x', 'y', async_js_func=True)
print(res)
assert res == "xy"
res = rt.f('x', 'y', async_js_func=True)
print(res)
assert res == "xy"
# The shortcut for calling the JS asynchronous function can only be used this way.
# other cases are not supported.
res = rt.eval('await f("x", "y")')
print(res)
assert res == "xy"
rt.close()
Use a custom version of Node.js
- By setting environment variables to use a custom version of Node, you only need to set the NODE_PATH or NODE environment variables.
import os
# Highest priority
os.environ['NODE_PATH'] = '/path/to/node.exe'
# Second priority
os.environ['NODE'] = '/path/to/node.exe'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyevaljs4-0.2.0.tar.gz
(4.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyevaljs4-0.2.0.tar.gz.
File metadata
- Download URL: pyevaljs4-0.2.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe398a36d01046c62c90ed7f3f0b2860bcd55aa5896faf24761e9262767c0595
|
|
| MD5 |
0b317ef6ea1effe6da53462d849f6ce9
|
|
| BLAKE2b-256 |
84d9fcf7d65bce10f8d7d85ac61e113cb084a9caf856975854fd82d4e6ad013e
|
File details
Details for the file pyevaljs4-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pyevaljs4-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c87da8b8388ba5e8d8de5684e7d6983466822bce41cfa38de9963b0548a17085
|
|
| MD5 |
03dd88390b425dbc1c2e8256522f260b
|
|
| BLAKE2b-256 |
87fb7745ff51e003dd6d08ebdf72ace84057d0fc107a4b9e42a243b5768c9af5
|