简体   繁体   English

节点集成在Electron中不起作用?

[英]Node Integration not working in Electron?

I am creating an inventory application with the backend written in Python 3.7. 我正在创建一个库存应用程序,后端用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. 我正在使用Electron为它构建GUI,并使用Node.js模块“python-shell”来与Python代码进行通信。 I would like for all of the code for python-shell to be in a separate JavaScript file (connector.js). 我想让python-shell的所有代码都在一个单独的JavaScript文件(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. 如果我从命令行使用node运行该文件,它可以正常工作,但是从HTML按钮调用该函数不起作用。

I know that with one of the last updates to Electron nodeIntegration by default is false, but I did set that to true. 我知道,对于Electron最后一次更新,nodeIntegration默认为false,但我确实将其设置为true。 Unfortunately it still doesn't allow me to communicate with the Node module within the Electron renderer process. 不幸的是,它仍然不允许我在Electron渲染器过程中与Node模块通信。 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. 如果我将connector.js代码复制到Electron使用的main.js文件中并在应用程序启动时调用它,它确实有效,但使用HTML按钮调用它也显然不起作用。

This is the main.js file for Electron with nodeIntegration set to true. 这是Electron的main.js文件,其中nodeIntegration设置为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. 这是使用正确参数调用Python脚本的connector.js文件。 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. functional.py用于测试一个非常简单的脚本,它接受第一个命令行参数(在本例中为addNewDataset )来调用一个函数,第二个作为它创建的文件的名称。


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 它使用简单的脚本标记加载到HTML文件中

<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. 我很确定我只是误解了Electron以及主要和渲染器进程的工作方式。 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. module.exports添加到python文件中。 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. 当您需要将html文件作为变量时,上面的代码将公开您的函数。 Like this in the html file: 在html文件中像这样:

<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. 我没有在IDE中测试我的代码,所以我为任何语法错误道歉。


More about exporting functions in modules: In Node.js, how do I "include" functions from my other files? 有关在模块中导出函数的更多信息: 在Node.js中,如何从其他文件中“包含”函数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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