javascript - 用python更新 Electron 中的元素(和 flask ?)

标签 javascript python flask electron

我正在尝试使用python脚本更新 Electron 元素。我已经阅读了许多教程,但是大多数教程都无效,而那些有效的教程我不明白如何更改它以在程序中使用它。因此,我创建了一个 Electron 应用程序,该应用程序使用javascript更新了

元素,而另一个应由python更新的

元素。从我阅读的内容中,我需要使用我不知道的flask,这就是为什么我需要有人向我解释如何使用javascript调用python函数以及.py文件中需要的flask代码。这是我的项目的文件:
main.js(由 Electron 伪造自动生成的,通常名为index.js,已重命名)

const { app, BrowserWindow } = require('electron');
const path = require('path');

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

const createWindow = () => {
  
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1200,
    height: 700 
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.

index.html(将使用python更新的页面的html文件)
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Test</title>

</head>

<body>
  
  <p id="loadedByJavascript">Loading current value from Javascript</p>
  <p id="loadedByPython">Loading current value from python</p>


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

</body>

</html>
index.js(将使用python更新的页面的.js文件)
var printFromJs = document.getElementById("loadedByJavascript")
var printFromPython = document.getElementById("loadedByPython")
var count = 0

function updateWithJavascript() {
    printFromJs.innerHTML = count;
    count +=1;
}
updateWithJavascript()

function updateWithPython() {
    //I dont know how to call the function hello from hello.py
}
updateWithPython()

//call them every 3 seconds
setInterval(updateWithJavascript, 3000)
setInterval(updateWithPython, 3000)

hello.py(我要调用其hello()函数的python文件)
from datetime import datetime

def hello():
    return str(datetime.now())
python函数还应该始终返回一个字符串,还是应该返回更复杂的东西(如列表或数据框),并在不同的html元素中使用每个值?
感谢您的时间

最佳答案

您可以使用JQuery向Flask路由发出GET请求。
JS:

function updateWithPython() {
    $.get('url + /hello', function(data) {
        printFromPython.innerHTML = data;
    });
}
Python:
from flask import Flask
from datetime import datetime

app = Flask(__name__)

@app.route("/hello")
def hello():
    return str(datetime.now())

if __name__ == "__main__":
    app.run()

关于javascript - 用python更新 Electron 中的元素(和 flask ?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66232405/

相关文章:

javascript - CSS/Javascript : How to make rotating circular menu with multiple states?

Javascript 如何覆盖构造函数方法?

javascript - JavaScript 事件系统是否违反了 LSP?

javascript - AWS Cognito 在 Web View 中保留经过身份验证的 session

python - 在Python中创建响应式(Reactive)迭代器的策略是什么?

python - C 代码在尝试通过 xmlrpc 进行 Python 远程过程调用时崩溃

python - 将字符串拆分为 2char 字符串列表

python - 504 连接错误 Flask Nginx uWSGI Ubuntu

python / flask : TypeError: an integer is required (got type str)

javascript - 在 Flask 中嵌入 Bokeh 图