-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrenderer.js
62 lines (57 loc) · 2.09 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
const { dialog, shell } = require('electron').remote;
const { PythonShell } = require('python-shell');
const buffer = require('buffer');
const Store = require('electron-store');
const store = new Store();
const path = require('path');
const fs = require('fs');
window.pythonRun = function(options, script, tmp_file, cwd) {
var old_cwd = process.cwd();
process.chdir(cwd);
let python = new PythonShell(script, options);
python.on('message', function (message) {
var full_message = document.getElementById('content_console').textContent + message + '\n';
document.getElementById('content_console').textContent = full_message.slice(-5000);
var e = document.getElementById('console-body');
e.scrollTo(0, e.scrollHeight);
});
python.on('stderr', function (stderr) {
document.getElementById('content_console').textContent += stderr + '\n';
var e = document.getElementById('console-body');
e.scrollTo(0, e.scrollHeight);
});
python.on('close', function () {
document.getElementById('content_console').textContent += '> Python program finished\n';
var e = document.getElementById('console-body');
e.scrollTo(0, e.scrollHeight);
fs.unlinkSync(tmp_file);
process.chdir(old_cwd);
});
python.on('error', function () {
window.alert('Error: process exited with code ' + python.exitCode);
});
};
window.writeFile = function(file, data) {
fs.writeFileSync(file, data, (err) => {
if (err) window.alert(err);
console.log('The file has been saved at ' + file);
});
};
window.readFile = function(file) {
return fs.readFileSync(file, 'utf8', (err, data) => {
if (err) window.alert(err);
return data;
});
};
window.selectPath = function(options) {
return dialog.showOpenDialogSync(options);
};
window.openPath = function(pathname) {
shell.openPath(pathname);
};