ElectronのFlaskが動作せずエラーになってしまいます。
以下のサイトを参考にしました。
https://qiita.com/goto_y/items/6cabe72da415755b29b5
Python
1app.py 2 3from __future__ import print_function 4import time 5from flask import Flask, render_template 6 7app = Flask(__name__) 8 9@app.route("/") 10def hello(): 11 return render_template("index.html") 12 13if __name__ == "__main__": 14 print('on hello') 15 app.run(host="127.0.0.1", port=5000) 16
json
1package.json 2 3{ 4 "name": "sample_app", 5 "version": "1.0.0", 6 "description": "", 7 "main": "index.js", 8 "scripts": { 9 "test": "echo \"Error: no test specified\" && exit 1" 10 }, 11 "keywords": [], 12 "author": "", 13 "license": "ISC", 14 "devDependencies": { 15 "electron": "^6.0.2" 16 }, 17 "dependencies": { 18 "python-shell": "^1.0.8", 19 "request": "^2.88.0", 20 "request-promise": "^4.2.4" 21 } 22} 23
javascript
1index.js 2 3// Electron側の初期設定 4const {PythonShell} = require('python-shell'); 5const electron = require('electron'); 6const app = electron.app; 7const BrowserWindow = electron.BrowserWindow; 8let mainWindow; 9 10// アプリを閉じた時にquit 11app.on('window-all-closed', function() { 12 app.quit(); 13}); 14 15// アプリ起動後の処理 16app.on('ready', function() { 17 PythonShell.run('./app.py', null, function (err, result) { // exe:'./resources/app/app.py' edit: './app.py' 18 if (err) throw err; 19 console.log(result); 20 }); 21 const rq = require('request-promise'); 22 const mainAddr = 'http://localhost:5000'; 23 24 const openWindow = function() { 25 mainWindow = new BrowserWindow({width: 400, height: 300 }); 26 mainWindow.loadURL(mainAddr); 27 28 // 開発ツールを有効化 29 // mainWindow.webContents.openDevTools(); 30 31 // 終了処理 32 mainWindow.on('closed', function() { 33 34 // キャッシュを削除 35 electron.session.defaultSession.clearCache(() => {}) 36 mainWindow = null; 37 }); 38 }; 39 40 const startUp = function() { 41 rq(mainAddr) 42 .then(function(htmlString) { 43 console.log('server started'); 44 openWindow(); 45 }) 46 .catch(function(err) { 47 startUp(); 48 }); 49 }; 50 51 startUp(); 52});
html
1index.html 2 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6 <meta charset="UTF-8"> 7 <title>Title</title> 8 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous"> 9</head> 10<body> 11<div class="container"> 12 <h1>Hello, World!</h1> 13 <button class="btn btn-primary">button</button> 14 <p id="text">text</p> 15</div> 16</body> 17</html>
その記事のコメントに書いて質問した方が早い気が。
確かに、そちらで質問してみます。
ありがとうございました。
回答1件
あなたの回答
tips
プレビュー