Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Shell script or python script in from a Atom electron app

I'm trying to use the Atom electron to write a Desktop App for both Mac and Windows.

What I need here is :

A button.

And when the user click the button it runs the following shell (or python script):

ping x.x.x.x

And the result will be displayed in a TextArea.

I tried to use [shelljs] and [yargs] but it seems like it is not workable with Atom electron.

All I want is to use JAVASCRIPT to write Desktop App (with GUI of course) that calls some script (shell && python) to do some automation work.

Any suggestion will be appreciated, thanks :)

like image 926
supersuraccoon Avatar asked Jan 29 '16 08:01

supersuraccoon


People also ask

Can you call a shell script from Python?

If you need to execute a shell command with Python, there are two ways. You can either use the subprocess module or the RunShellCommand() function. The first option is easier to run one line of code and then exit, but it isn't as flexible when using arguments or producing text output.

Can you use Python in electron?

You can use python-shell to communicate between Python and Node. js/Electron. python-shell provides an easy way to run Python scripts from Node. js with basic and efficient inter-process communication and better error handling.

What is a Python shell?

The Python Shell is the interpreter that executes your Python programs, other pieces of Python code, or simple commands.


1 Answers

It can be done directly with Node, you can use the child_process module. Please notice this is asynchronous.

const exec = require('child_process').exec;

function execute(command, callback) {
    exec(command, (error, stdout, stderr) => { 
        callback(stdout); 
    });
};

// call the function
execute('ping -c 4 0.0.0.0', (output) => {
    console.log(output);
});

I encourage you to also have a look at npm, there are tons of modules that could help you to do what you want, without calling a python script.

like image 104
martpie Avatar answered Sep 28 '22 01:09

martpie