spawn python process is Reloading the page in electron

297 Views Asked by At

[Update]: Before reloading, fora fraction of second a warning [violation] 'click' hander took xx ms is showinf. (found out from screen recording)

I am using Electron with React. To communicate with main process(Electron) and renderer(React) i am using ipcRederer and ipcMain Modules and using hooks to load an image on web page.

The ipcMain module spawns a python process and its result (a image location) is send back to which then loads the image that was create by a python script by providing the location

Simple dataflow is: React ->Hooks -> ipcRender -> ipcMain -> spawn python process -> ipc Main - > ipcRender -> Hooks(useState)

When i execute it it shows the image but immediateby reloads the webpage. Going deep i found that the problem was spawning the python process. That is reloading the page.

main.js(electron):

const { ipcMain } = require('electron');

ipcMain.on('synchronous-message', (event, arg) => {

  //.................Code to run a Python script...............
  var python = require('child_process').spawn('python', ['./public/test.py']);
  python.stdout.on('data',function(data){

  //send the output of python to React
  event.returnValue = (data.toString('utf8'));

  });
 //.................................

})

app.js(react):

const { ipcRenderer } = window.require('electron');

function Home(){
    const [graph, setGraph] = useState("");
    function test(){
        setGraph(ipcRenderer.sendSync('synchronous-message', 0));
      }
    return (<div className="Home">
                <div onClick={test}>CLick Me</div>
                <div id="graph">
                    <img src={graph} />
                </div>
    </div>)
}

graph variable contains the returned value from ipcMain message

0

There are 0 best solutions below