Run JavaScript Code in Python through the Microsoft Chakra engine
Project description
PyEvalJS
PyEvalJS is a python wrapper for Microsoft Chakra engine, it act as a bridge between the Python and JavaScript objects, and with this module you can easily run JavaScript Code in Python without having to install nodejs.
Installation
pip install PyEvalJS
Usage
Execute a script
>>> from PyEvalJS import Runtime
>>> runtime = Runtime()
>>> runtime.eval("'red yellow blue'.split(' ')")
['red', 'yellow', 'blue']
Call a function
>>> from PyEvalJS import Runtime
>>> runtime = Runtime()
>>> runtime.compile("""
... function add(x, y) {
... return x + y;
... }
... """)
>>> runtime.call("add",1,2)
3
or just put args into the script
>>> from PyEvalJS import Runtime
>>> runtime = Runtime()
>>> runtime.eval("""
... function add2(num) {
... return num+2;
... } add2(8);""")
10
or by passing parameters
>>> from PyEvalJS import Runtime
>>> runtime = Runtime()
>>> runtime.set_variable("a",8)
True
>>> runtime.eval("""
... function add2(num) {
... return num+2;
... } add2(a);""")
10
Passing parameters
>>> from PyEvalJS import Runtime
>>> runtime = Runtime()
>>> runtime.set_variable("name", ['Jim','Bob','Tour'])
True
>>> runtime.get_variable("name")
['Jim', 'Bob', 'Tour']
Use a JavaScript module
>>> from PyEvalJS import Runtime
>>> runtime = Runtime()
>>> runtime.require("./js/crypto-js.js") #import CryptoJS
>>> runtime.compile("""
... function encryptByDESModeCBC(key,message) {
... var keyHex = CryptoJS.enc.Utf8.parse(key);
... var ivHex = CryptoJS.enc.Utf8.parse(key);
... encrypted = CryptoJS.DES.encrypt(message, keyHex, {
... iv:ivHex,
... mode: CryptoJS.mode.CBC,
... padding:CryptoJS.pad.Pkcs7
... }
... );
... return encrypted.ciphertext.toString();
... }""")
>>> runtime.call("encryptByDESModeCBC",'1234','this is a test')
'94b7b0cc2b71165ea067868f595fc03a'
Call the same function for each item in the list
>>> from PyEvalJS import Runtime
>>> runtime = Runtime()
>>> runtime.compile("""
... function add2(num) {
... return num+2;
... }""")
>>> runtime.call_for_each("add2",[1,2,4,6,5])
[3, 4, 6, 8, 7]
Supports
- Python2 >= 2.7
- Python3 >= 3.4
Platform
- macOS x64
- Linux x64
- Windows x86/x64 (tested on Windows 10 x64, Python 3.7)
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request :D
Licence
Code released under the MIT license
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
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 PyEvalJS-1.0.0.tar.gz.
File metadata
- Download URL: PyEvalJS-1.0.0.tar.gz
- Upload date:
- Size: 38.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2928bbb1e9705cf7cae00d8f8bd82b720c0fd796bb5cf42f2a1e92d34114cff
|
|
| MD5 |
ed814f786288ea39d2b89fbef919da41
|
|
| BLAKE2b-256 |
2c70b3c76ad794e9aa2366100f673b06e5ce0c94e37b9f41c45bc28658ce636b
|
File details
Details for the file PyEvalJS-1.0.0-py3-none-any.whl.
File metadata
- Download URL: PyEvalJS-1.0.0-py3-none-any.whl
- Upload date:
- Size: 38.7 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6fa329b9c73f264a4e734955cb970b0e22f675fcfb562fd48d988e2071c5851
|
|
| MD5 |
36da9179e04525b91806a8f149388078
|
|
| BLAKE2b-256 |
f3fb575b2141d85990ad81a57dd8bdac9cca43fff01adccfaadc0d13dd6a7b89
|