user13494862
user13494862

Reputation:

Can I use Electron without node.js

Im new to Javascript so I would like to keep it at the bare minimum. Is there a way that I can use the Electron to communicate with python script without having node.js? My app is just a basic app that takes some input from users from a html page and I need this text input to be processed in python and write an excel file. So there is not much happening in html so is there a simple way to transfer the input to python file? I want to use Electron because I need this html to be my UI and also I need to distribute this app.

Upvotes: 3

Views: 3248

Answers (2)

Prakalp varshney
Prakalp varshney

Reputation: 43

Recently i have done it with some sort of trick hope it will help you and there are the following step which i followed-

  1. Created a stand alone python exe using pyinstaller and the exe has flask server internally then i put the flask server inside my node application.
  2. Now we have to initiate our flask server and send a request to it for processing, i have done this with the help of "execFile" function as a child process, for which i have created a function and the code was something like that-

    async function callFlask(){ var child = require('child_process').execFile; child('path_to_python_exe ', function(err, data) { if(err){ console.error(err); return; } }); }

  3. Now we have initiated our flask server then will send the request with the help of fetch request like

    await callFlask().then( await fetch('host_ip_defined_in_flask'+encodeURIComponent('data'))

    1. Now further we can extend our then chain to get response from python if any and proceed further forexample -

    await callFlask().then( await fetch('host_ip_defined_in_flask'+encodeURIComponent('data')) .then(res => res.text()) .then(body => console.log(body)))

Here, your output data which python return will be printed in console then you can make your node application behave differently depending on output returned by it.

  1. Also you can package your app with available packagers for electron like electron-packager it will work like a charm.

Also there is are some disadvantage for using python as like it will increase your package size and the process will be difficult to kill from electron after processing so it will increase burden on host machine.

I am assuming that Explaining to create a flask server is not the scope of this question instead if you face any issues let me know, i hope it will help...

Upvotes: 1

Darren Cook
Darren Cook

Reputation: 28968

I guess the answer is "no": the main process running node will always be there.

An Electron app consists of a JavaScript main process, and one or more JavaScript renderer processes. There is no built-in Python support. And the user will need Python already installed. So, it sounds like a poor fit for what you need.

The answers here may be useful, and will show how to call the python script. I took a quick look at the flexx toolkit mentioned there. It seems to work with the user's browser, rather than producing a single executable.

Upvotes: 1

Related Questions