Skip to content

Commit 554f969

Browse files
committed
Add check for port number and suppport for reading paths in advanced config
1 parent e18c928 commit 554f969

File tree

7 files changed

+168
-10
lines changed

7 files changed

+168
-10
lines changed

src/main.js

+24
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,28 @@ const { app, ipcMain, BrowserWindow } = require('electron')
66
const Window = require('./Window')
77
const DataStore = require('./DataStore')
88
const electronLocalshortcut = require('electron-localshortcut');
9+
const commandExists = require('command-exists');
10+
const { execSync } = require('child_process')
11+
const exec = require('child_process').exec;
912

1013
require('electron-reload')(process.cwd())
1114

1215
// create a new todo store name "Todos Main"
1316
const todosData = new DataStore({ name: 'Todos Main' })
1417

18+
function getPythonInterpreter() {
19+
var result = undefined;
20+
var found = commandExists.sync('python3')
21+
|| commandExists.sync('python')
22+
|| commandExists.sync('python');
23+
if (found) {
24+
result = execSync("python -c 'import sys; print(sys.executable)'")
25+
}
26+
27+
console.log('Returning: '.concat(result));
28+
return result;
29+
}
30+
1531
function main () {
1632
// todo list window
1733
let mainWindow = new Window({
@@ -30,6 +46,14 @@ function main () {
3046
mainWindow.webContents.send('todos', todosData.todos)
3147
})
3248

49+
ipcMain.on('get-sys-cfg-jupyter-lab', (event) => {
50+
console.log('get-sys-cfg-jupyter-lab() called')
51+
var resp = (
52+
getPythonInterpreter()
53+
);
54+
event.sender.send('asynchronous-reply', resp);
55+
});
56+
3357
// create add todo window
3458
ipcMain.on('open-url-window', () => {
3559
// if addTodoWin does not already exist

src/package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"standard": "^12.0.1"
2020
},
2121
"dependencies": {
22+
"command-exists": "^1.2.9",
2223
"electron": "^3.0.4",
2324
"electron-localshortcut": "^3.2.1",
2425
"electron-reload": "^1.2.5",

src/renderer/new_server/advanceduser.html

+14-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<div class="container">
1212
<div class="container large-header" id="advanced-config-header">
1313
<a class="back-btn" href="javascript:void" id="backBtn"><i class="fas fa-arrow-left fa-2x"></i></a>
14-
<h2 style="display: inline;">Advanced configuration</h2>
14+
<h2 id="advanced-config-header-txt">Advanced configuration</h2>
1515
</div>
1616

1717
<div class="input-group mb-3">
@@ -22,26 +22,35 @@ <h2 style="display: inline;">Advanced configuration</h2>
2222
</div>
2323

2424
<div class="input-group mb-3">
25-
<input type="text" class="form-control" placeholder="Path to initial directory" aria-label="Recipient's username" aria-describedby="basic-addon2" id="pythonInterpreterPath">
25+
<input type="text" class="form-control" placeholder="Path to initial directory" aria-label="Recipient's username" aria-describedby="basic-addon2" id="initialPath">
2626
<div class="input-group-append form-inline-link-div">
27-
<a class="form-inline-link" href="javascript:void" id="pythonInterpreterPathBtn"><i class="fas fa-search"></i></a>
27+
<a class="form-inline-link" href="javascript:void" id="initialPathBtn"><i class="fas fa-search"></i></a>
2828
</div>
2929
</div>
3030

3131
<div class="input-group mb-3">
32-
<input type="text" class="form-control" placeholder="Port number (number or 'auto')" aria-label="Recipient's username" aria-describedby="basic-addon2" id="pythonInterpreterPath">
32+
<input type="text" class="form-control" placeholder="Port number (number or 'auto')" aria-label="Recipient's username" aria-describedby="basic-addon2" id="portNum">
3333
<div class="input-group-append form-inline-link-div">
34-
<a class="form-inline-link" href="javascript:void" id="pythonInterpreterPathBtn"></a>
34+
<a class="form-inline-link" href="javascript:void" id="portNumBtn"></a>
3535
</div>
3636
</div>
3737

38+
<a class="refill-btn" href="javascript:void" id="refillBtn"><i class="fas fa-redo refill-btn-txt"></i></a>
39+
<a class="refill-btn-tool-tip text-primary">rescan</a>
40+
3841
<div class="col-sm advanced-config-btn-grp">
3942
<button id="submitBtn" type="submit" class="btn btn-primary float-right">start</button>
4043
<button id="cancelBtn" type="cancel" class="btn btn-secondary float-right">cancel</button>
4144
</div>
4245

4346
</div>
4447

48+
<div class="result-box">
49+
<a id='resultBox'>
50+
All good
51+
</a>
52+
</div>
53+
4554
<div class="need-help-link">
4655
<a href="javascript:void">Need help?</a>
4756
</div>

src/renderer/new_server/advanceduser.js

+84-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,96 @@
22

33
const { ipcRenderer } = require('electron')
44
const electron = require('electron')
5-
const path = require('path')
6-
const remote = electron.remote
5+
const remote = electron.remote;
6+
const app = remote.app;
7+
8+
function updateUI(pythonInterpreterPath) {
9+
console.log(pythonInterpreterPath);
10+
document.getElementById('pythonInterpreterPath').value = pythonInterpreterPath;
11+
document.getElementById('initialPath').value = app.getPath('home');
12+
}
13+
14+
function getAndUpdateJLabCfg() {
15+
return new Promise(resolve => {
16+
ipcRenderer.send('get-sys-cfg-jupyter-lab', [])
17+
ipcRenderer.on('asynchronous-reply', (event, result) => {
18+
updateUI(result);
19+
})
20+
});
21+
}
22+
23+
function focusFieldWithError(fieldId) {
24+
switch (fieldId) {
25+
case 1:
26+
document.getElementById('pythonInterpreterPath').focus();
27+
break;
28+
case 2:
29+
document.getElementById('initialPath').focus();
30+
break;
31+
case 2:
32+
document.getElementById('portNum').focus();
33+
break;
34+
default:
35+
console.warn('Unknown value ' + fieldId.toString())
36+
break;
37+
}
38+
}
39+
40+
function displayMsg(type, msg) {
41+
var resultElem = document.getElementById('resultBox');
42+
if (type == 'error') {
43+
console.error(msg);
44+
resultElem.innerHTML = `<a class="text-warning"><i class="fas fa-exclamation-circle"></i> ${msg}</a>`
45+
} else if (type == 'info') {
46+
console.info(msg);
47+
resultElem.innerHTML = `<a class="text-success"><i class="fas fa-info-circle"></i> ${msg}</a>`
48+
}
49+
}
50+
51+
// 0 = No issue
52+
// [1-] = First field id that is wrong
53+
function checkValues() {
54+
var result = 0;
55+
56+
// Check the port number
57+
var portNumStr = document.getElementById('portNum').value;
58+
var portNum = parseInt(portNumStr);
59+
60+
if (isNaN(portNum) || portNum > 65535) {
61+
console.warn('Cannot parse port number: ' + portNumStr);
62+
displayMsg('error', `Port number ${portNumStr} is invalid.`);
63+
result = 3;
64+
} else {
65+
console.log('using port ' + portNumStr + ' ' + portNum.toString());
66+
}
67+
68+
return result;
69+
}
70+
71+
function startServer() {
72+
var retVal = checkValues();
73+
if (retVal != 0) {
74+
focusFieldWithError(retVal);
75+
} else {
76+
displayMsg('info', 'Everything looks good');
77+
}
78+
}
779

880
document.getElementById('backBtn').addEventListener('click', (evt) => {
981
var window = remote.getCurrentWindow();
10-
window.webContents.goBack()
82+
window.webContents.goBack();
1183
})
1284

1385
document.getElementById('cancelBtn').addEventListener('click', (evt) => {
1486
var window = remote.getCurrentWindow();
15-
window.close()
87+
window.close();
88+
})
89+
90+
document.getElementById('submitBtn').addEventListener('click', (evt) => {
91+
startServer();
92+
})
93+
94+
document.getElementById('refillBtn').addEventListener('click', (evt) => {
95+
getAndUpdateJLabCfg();
1696
})
1797

src/renderer/new_server/newserver.css

+39-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ body {
1212
padding-bottom: 1%;
1313
}
1414

15+
.result-box {
16+
font-size: small;
17+
text-align: left;
18+
position:absolute;
19+
bottom:0;
20+
left:0;
21+
padding-left: 3%;
22+
padding-bottom: 1%;
23+
}
24+
1525
.msg {
1626
margin-top: 6%;
1727
text-align: center;
@@ -27,12 +37,36 @@ body {
2737
padding-right: 3%;
2838
}
2939

40+
.refill-btn {
41+
margin-left: 0;
42+
padding-left: 0;
43+
margin-right: 1%;
44+
/* float: right; */
45+
}
46+
47+
.refill-btn-tool-tip {
48+
margin-left: 0;
49+
display:inline-block;
50+
/* vertical-align: bottom; */
51+
font-size: smaller;
52+
opacity: 0;
53+
}
54+
55+
.refill-btn:hover + .refill-btn-tool-tip {
56+
opacity: 1;
57+
transition-delay: 0.3s;
58+
}
59+
60+
.refill-btn-tool-tip {
61+
}
62+
3063
.advanced-config-btn-grp {
64+
display: inline;
3165
margin-top: 7%;
3266
}
3367

3468
.form-inline-link-div {
35-
width: 15%;
69+
width: 8%;
3670
display: flex;
3771
justify-content: center;
3872
}
@@ -46,6 +80,10 @@ body {
4680
padding-bottom: 10%;
4781
}
4882

83+
#advanced-config-header-txt {
84+
display: inline;
85+
}
86+
4987
.no-top-padding {
5088
padding-top: 0%;
5189
}

src/renderer/new_server/newserver.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ document.getElementById('advancedUser').addEventListener('click', (evt) => {
1111
console.log(filePath);
1212
remote.getCurrentWindow().loadFile(filePath);
1313
})
14+

0 commit comments

Comments
 (0)