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
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.2.tar.gz
(4.6 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.2.tar.gz.
File metadata
- Download URL: pyevaljs4-0.2.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
749ac6d26078f3e68febe27a2687f7ac7ba2f67dd9513dc7b16363a3d507d411
|
|
| MD5 |
93ccfa267a0c27ff2616a61d93c8e628
|
|
| BLAKE2b-256 |
45f13c64deefedb8a2bbd98d3d636de24107f940f5ad3231fc18bc61d311172d
|
File details
Details for the file pyevaljs4-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pyevaljs4-0.2.2-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a90ab18fe6f77e2b4a27b03f466dd9a5651b0f956c399597c01d716bba6c332
|
|
| MD5 |
5f8e0cf82706479eb6bf63e07941b9a3
|
|
| BLAKE2b-256 |
261cda718b5e87047809c2ebc6ffe25da00359ede0eec260a25eace02f9608f1
|