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.1.tar.gz (4.5 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.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

Hashes for pyevaljs4-0.2.1.tar.gz
Algorithm Hash digest
SHA256 da2fe8b56e58c71d6eed49446ae3fab2f059022edc3d7373ca82c2e60108c70e
MD5 625e286e31f902519ddd030c2ad0f931
BLAKE2b-256 9634d3207739a3d7261b96c3401078732bbc2d8fcea6793dd05d594e4c8b8a09

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyevaljs4-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.7

File hashes

Hashes for pyevaljs4-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 445a82f5fc67bc6e2bc0f534ca3e139e0196d77deb411fb473ed61df3324bc59
MD5 ab8ece017e8842ee3f27c57b9419c09b
BLAKE2b-256 8020933e1fbf4056f446fe6430762c622c8a6ec9f3040b91a7de394948b41263

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