When I searched on the net, I found many sample codes that send one character string, but I couldn't find sample code that passes multiple parameters, so I summarized them.
Describes how to pass multiple parameters when executing Python from Electrion. In Electron, Python is executed by Python Shell, and on the Python side, execution arguments are acquired by the argparse library.
Import and run Python Shell. The point is to arrange the arguments in args of options.
ElectronApp.js
const {PythonShell} = require('python-shell');
const pyMain = '/path/to/pyMain.py';
let options = {
mode: 'text',
pythonOption: ['-u'],
args:[
'-param1', 'name1',
'-param2', 'name2'
]
}
let pyshell = new PythonShell(pyMain, options);
pyshell.send();
pyshell.on('message', function(data){
console.log(data);
});
pyMain.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-param1", help="parameter1 discription")
parser.add_argument("-param2", help="parameter2 discription")
args = parser.parse_args()
print(args.param1) # name1
print(args.param2) # name2
Recommended Posts