Skip to content Skip to sidebar Skip to footer

How To Execute Javascript Code From Python With Arguments

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 a

Solution 1:

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


Solution 2:

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")


Post a Comment for "How To Execute Javascript Code From Python With Arguments"