Skip to main content

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.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyevaljs4-0.2.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pyevaljs4-0.2.3.tar.gz.

File metadata

  • Download URL: pyevaljs4-0.2.3.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

Hashes for pyevaljs4-0.2.3.tar.gz
Algorithm Hash digest
SHA256 d878b734ef68652d552eca79b25d294e8fe052cc2f09fab4c177b0a80cec3142
MD5 2a5b4ae5d8dbb599c797013e97ce202e
BLAKE2b-256 10f21a663565b01cba65e4812d7a028ac0331e757fb2a96b03dfa88ad9783902

See more details on using hashes here.

File details

Details for the file pyevaljs4-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: pyevaljs4-0.2.3-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

Hashes for pyevaljs4-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 21fadbe847b7c518d10ee621e8830664bcd67c2ac224d98c522094ab33bc0061
MD5 7cf438f6a4c87c8d70cbbcfcf58039f7
BLAKE2b-256 5194efa18caa0adfb41a5e26e2c4a733611ecd6828d6f2419a6041f6c18dda8c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page