When running a Python shell from Electron, pass multiple arguments to run Python.

Introduction

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.

Overview

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.

Sample code on Electron side

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);
});

Sample code on the Python side

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

When running a Python shell from Electron, pass multiple arguments to run Python.
How to run a Python program from within a shell script
Create a shell script to run the python file multiple times
From buying a computer to running a program with python
Execute Python function from Powershell (how to pass arguments)
How to pass arguments when invoking python script from blender on the command line
How to slice a block multiple array from a multiple array in Python
How to pass arguments to a Python script in SPSS Modeler Batch
A story about trying to run multiple python versions (Mac edition)
Call dlm from python to run a time-varying coefficient regression model
Pass a list by reference from Python to C ++ with pybind11
Everything from building a Python environment to running it on Windows
Send a message from Python to Slack
[Python / Tkinter] How to pass arguments to command
How to run a Maya Python script
A story I was addicted to when inserting from Python to a PostgreSQL table
How to write a string when there are multiple lines in python
Extension of Python by C or C ++ (when there are multiple arguments, when passing a list from the Python side)
Send a message from Slack to a Python server
How to pass the execution result of a shell command in a list in Python
Run a Python script from a C # GUI application
Edit Excel from Python to create a PivotTable
How to take multiple arguments when doing parallel processing using multiprocessing in python
How to generate a Python object from JSON
Python Note: When assigning a value to a string
[Linux] [kernel module] How to pass parameters as arguments when loading a loadable kernel module
Run a Python file from html using Django
How to manage arguments when implementing a Python script as a command line tool
Launch a shell while a Python script is running
Trap trapped when running a Python Windows executable
What I did when updating from Python 2.6 to 2.7
A note I was addicted to when running Python with Visual Studio Code
How to enable python3 to run when sending jobs from GCP Cloud Composer to Dataflow
Run a python script from excel (using xlwings)
[Linux] When you want to search for a specific character string from multiple files
I tried running python etc. from a bat file
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
Python --Notes when converting from str type to int type
From python to running instance on google cloud platform
Steps from installing Python 3 to creating a Django app
Things to note when initializing a list in Python
What's in that variable (when running a Python script)
Consider a conversion from a Python recursive function to a non-recursive function
Python script to create a JSON file from a CSV file
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
I want to run a quantum computer with Python
[Python] Start a batch file from Python and pass variables.
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Run python from excel
How to pass the execution result of a shell command in a list in Python (non-blocking version)
How to run a Python file at a Windows 10 command prompt
[Linux] Copy data from Linux to Windows with a shell script
Summary from building Python 3.4. * From source to building a scientific computing environment
How to launch AWS Batch from a python client app
[python] A note when trying to use numpy with Cython
Points to note when deleting multiple elements from the List
A simple way to avoid multiple for loops in Python