2

Executing javascript from python using Naked.toolshed.shell: How can I execute JavaScript code from Python? Answered my question fine, But is there any way to do this AND pass an argument through to be executed? like

from Naked.toolshed.shell import execute_js response = execute_js('file.js', arg1) # arguments to pass if response.exitcode == 0: print(response.stdout) else: sys.stderr.write(response.stderr) 

while file.js may look something like

var x = 10; x = 10 - arg1; // to pass argument here console.log(x); function greet() { console.log("Hello World!"); } greet() 

Is there a way to accomplish this?

1

2 Answers 2

2

As the documentation reveals, yes it is possible:

The execute_js() function runs the execute() function on a Node.js script file. Instead of passing the command to be executed as the first parameter, pass a Node.js script filepath as the first parameter and any additional command arguments as the second parameter (optional). The executed command is concatenated from these strings with the following code:

if len(arguments) > 0: js_command = 'node ' + file_path + " " + arguments else: js_command = 'node ' + file_path 

Parameters:
file_path (string) - the filepath to the Node.js script that is to be executed by the shell

arguments (string) - optional, any additional arguments to be used with your command

Documentation: http://naked.readthedocs.io/toolshed_shell.html#Naked.toolshed.shell.execute_js

1
  • Alright I figured it out. The arguments are stored in process.argv. Thanks
    – fusemoth
    CommentedJan 24, 2018 at 23:33
0

if you want to pass an argument(variable) in execute_js('file.js', arg1) it create a problem like value I received in node js command argument is "NaN" so to overcome this problem I tried below thing which works fine for me:

First make a string need to pass in execute_js like:

node_cmd = "file.js" + " " + str(variable) now we can pass node_cmd in execute_js as below:

execute_js("node_cmd")

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.