简体   繁体   中英

Node Integration not working in Electron?

I am creating an inventory application with the backend written in Python 3.7. I am using Electron to build a GUI for it and the Node.js Module "python-shell" to be able to communicate with the Python code. I would like for all of the code for python-shell to be in a separate JavaScript file (connector.js). If I run just that file with node from the command line it works perfectly fine, but calling the function from an HTML Button doesn't work.

I know that with one of the last updates to Electron nodeIntegration by default is false, but I did set that to true. Unfortunately it still doesn't allow me to communicate with the Node module within the Electron renderer process. If I copy the connector.js code into the main.js file used by Electron and call it on start of the application, it does work but calling it using an HTML button also obviously doesn't work.

This is the main.js file for Electron with nodeIntegration set to true.


const {app, BrowserWindow, Menu} = require('electron')
const fs = require('fs')

let mainWindow

function createWindow() {
    mainWindow = new BrowserWindow({
        // Set fixed size of main window
        width: 1280,
        height: 768,
        resizable: false,
        webPrefrences: {
            nodeIntegration: true,
        }
    })

    mainWindow.loadFile('./main.html')

    mainWindow.on('closed', function () {
        mainWindow = null
    })

    //Opens the dev tools by default.
    mainWindow.webContents.openDevTools();
}

// Disable the default Menu Bar
Menu.setApplicationMenu(null)

app.on('ready', createWindow)

app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
    if (mainWindow === null) createWindow()
})

And this is the connector.js file that calls the Python script with the correct arguments. functionality.py is for testing a very simple script that takes the first commandline argument (in this case addNewDataset ) to call a function and the second as the name of a file it creates.


const {PythonShell} = require('python-shell')

var options = {
    mode: 'text',
    encoding: 'utf8',
    pythonPath: 'python3',
    pythonOptions: ['-u'],
    scriptPath: './',
    args: ['addNewDataset', 'testDataset123']
};

function test() {
    PythonShell.run('functionality.py', options, function (err) {
        if (err) throw err;
        console.log('done');
    });
}

It is loaded into the HTML file using a simple script tag

<script src="connector.js"></script>

and called using an input

<input type="submit" value="Button" onclick="test()">

I am pretty sure that I am just misunderstanding the way Electron and the main and renderer processes work. Unfortunately no matter what I look for on the Internet I don't seem to be able to get a solution that actually works.

Add a module.exports to your python file. It'll look like this:

module.exports = {
    test: function () {
        PythonShell.run('functionality.py', options, function (err) {
            if (err) throw err;
            console.log('done');
        });
    }
}

That above code will expose your function when you require the html file as a variable. Like this in the html file:

<HTML>
<input id="SpecialButton" type="submit" value="Button" onclick="test()">
<!-- ^add an id to the button -->

<script>

    const python = require('python-file-path')

    document.getElementById('SpecialButton').onclick = () => {
        python.test() //call the function of the python file like this
    }

</script>
</HTML>

I haven't tested my code in a IDE so I apologize for any syntax errors.


More about exporting functions in modules: In Node.js, how do I "include" functions from my other files?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM