In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to call JS in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Take a simple JS script as an example to write the code to a file
/ / norm.js// calculates the sum of two numbers function add (num1, num2) {return num1 + num2;}
Among them, a method is defined to calculate the sum of two numbers.
3. Method 1: PyExecJS
PyExecJS is the most frequently used method, and the underlying implementation is to run JS code in the local JS environment.
Supported JS environments include: Node.js, PyV8, PhantomJS, Nashorn, etc.
First, we need to install the dependency package PyExecJS
/ / py_exec_js_demo.py// installation depends on pip3 install PyExecJS
Then, read the source code from the JS file
Def js_from_file (file_name): "" read js file: return: "" with open (file_name, 'ringing, encoding='UTF-8') as file: result = file.read () return result
Finally, use the compile () method of the execjs class to compile and load the above JS string and return a context object
Import execjsfrom js_code import * # compile and load the js string context1 = execjs.compile (js_from_file ('. / norm.js'))
Finally, the JS method is executed by calling the call () method of the context object
The parameter contains the name of the method to which the JS code is called and the input parameter of the corresponding method
# call the add () method in the js code with arguments 2 and method name: add# parameters: 2 and 3result1 = context1.call ("add", 2, 3) print (result1)
It should be noted that because PyExecJS runs in the local JS environment, the JS environment will be started before use, resulting in a slow running speed.
For more features, please see:
Https://github.com/doloopwhile/PyExecJS
4. Method 2: js2py
As a pure Python-implemented JS interpreter, js2py can completely break away from the JS environment and directly convert JS code into Python code.
First, install the dependent library
# install dependent library pip3 install js2py
Then use the EvalJs () method in js2py to generate a context object
# generate a context context = js2py.EvalJs () by getting the upper and lower js2py
Then use the context object to execute the JS script, which is converted to Python code
# execute the entire JS code context.execute (js_content)
Finally, the method in JS is called in context and the input parameters are defined.
# use context context to call specific functions # function name: add# parameter: 1Magne2result = context.add (1Magne2) print (result)
It is important to note that if JS is a long obfuscated code, the conversion to Python process may report an error
For more features, please see:
Https://github.com/PiotrDabkowski/Js2Py
5. Method 3: Node.js
Actually use Python's os.popen to execute node commands and JS scripts
First, make sure that the Node.js environment is installed locally
Modify the JS script to add an export function init to facilitate internal functions to be called
/ / calculate the sum of two numbers and function add (num1, num2) {return num1 + num2;} / / add an export function (node mode) module.exports.init = function (arg1, arg2) {/ / call the function and return console.log (add (arg1, arg2));}
Then, compose the command that calls the JS method into a string
# make up the command to call js # node command: node-ecmd = 'node-e "require (\\"% s\\ "). Init (% s% s)"'% ('. / norm', 3,5)
Finally, execute the command through os.popen
Pipeline = os.popen (cmd) # read result result = pipeline.read () print ('result:', result) 6. Method 4: PyV8
PyV8 is a dependent library encapsulated by Google for Chrome V8 engine in Python.
It does not depend on the local JS environment and runs very fast.
Import PyV8from js_code import js_from_filewith PyV8.JSContext () as ctx: ctx.eval (js_from_file ('. / norm.js')) # calls the js function and specifies the parameter ctx.locals.add (1, 2). After reading the above, do you have any further understanding of how to call JS in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.