Skip to content

Commit

Permalink
Add check for port number and suppport for reading paths in advanced …
Browse files Browse the repository at this point in the history
…config
  • Loading branch information
suyashmahar committed Aug 24, 2020
1 parent e18c928 commit 554f969
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 10 deletions.
24 changes: 24 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@ const { app, ipcMain, BrowserWindow } = require('electron')
const Window = require('./Window')
const DataStore = require('./DataStore')
const electronLocalshortcut = require('electron-localshortcut');
const commandExists = require('command-exists');
const { execSync } = require('child_process')
const exec = require('child_process').exec;

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

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

function getPythonInterpreter() {
var result = undefined;
var found = commandExists.sync('python3')
|| commandExists.sync('python')
|| commandExists.sync('python');
if (found) {
result = execSync("python -c 'import sys; print(sys.executable)'")
}

console.log('Returning: '.concat(result));
return result;
}

function main () {
// todo list window
let mainWindow = new Window({
Expand All @@ -30,6 +46,14 @@ function main () {
mainWindow.webContents.send('todos', todosData.todos)
})

ipcMain.on('get-sys-cfg-jupyter-lab', (event) => {
console.log('get-sys-cfg-jupyter-lab() called')
var resp = (
getPythonInterpreter()
);
event.sender.send('asynchronous-reply', resp);
});

// create add todo window
ipcMain.on('open-url-window', () => {
// if addTodoWin does not already exist
Expand Down
5 changes: 5 additions & 0 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"standard": "^12.0.1"
},
"dependencies": {
"command-exists": "^1.2.9",
"electron": "^3.0.4",
"electron-localshortcut": "^3.2.1",
"electron-reload": "^1.2.5",
Expand Down
19 changes: 14 additions & 5 deletions src/renderer/new_server/advanceduser.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="container">
<div class="container large-header" id="advanced-config-header">
<a class="back-btn" href="javascript:void" id="backBtn"><i class="fas fa-arrow-left fa-2x"></i></a>
<h2 style="display: inline;">Advanced configuration</h2>
<h2 id="advanced-config-header-txt">Advanced configuration</h2>
</div>

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

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

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

<a class="refill-btn" href="javascript:void" id="refillBtn"><i class="fas fa-redo refill-btn-txt"></i></a>
<a class="refill-btn-tool-tip text-primary">rescan</a>

<div class="col-sm advanced-config-btn-grp">
<button id="submitBtn" type="submit" class="btn btn-primary float-right">start</button>
<button id="cancelBtn" type="cancel" class="btn btn-secondary float-right">cancel</button>
</div>

</div>

<div class="result-box">
<a id='resultBox'>
All good
</a>
</div>

<div class="need-help-link">
<a href="javascript:void">Need help?</a>
</div>
Expand Down
88 changes: 84 additions & 4 deletions src/renderer/new_server/advanceduser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,96 @@

const { ipcRenderer } = require('electron')
const electron = require('electron')
const path = require('path')
const remote = electron.remote
const remote = electron.remote;
const app = remote.app;

function updateUI(pythonInterpreterPath) {
console.log(pythonInterpreterPath);
document.getElementById('pythonInterpreterPath').value = pythonInterpreterPath;
document.getElementById('initialPath').value = app.getPath('home');
}

function getAndUpdateJLabCfg() {
return new Promise(resolve => {
ipcRenderer.send('get-sys-cfg-jupyter-lab', [])
ipcRenderer.on('asynchronous-reply', (event, result) => {
updateUI(result);
})
});
}

function focusFieldWithError(fieldId) {
switch (fieldId) {
case 1:
document.getElementById('pythonInterpreterPath').focus();
break;
case 2:
document.getElementById('initialPath').focus();
break;
case 2:
document.getElementById('portNum').focus();
break;
default:
console.warn('Unknown value ' + fieldId.toString())
break;
}
}

function displayMsg(type, msg) {
var resultElem = document.getElementById('resultBox');
if (type == 'error') {
console.error(msg);
resultElem.innerHTML = `<a class="text-warning"><i class="fas fa-exclamation-circle"></i> ${msg}</a>`
} else if (type == 'info') {
console.info(msg);
resultElem.innerHTML = `<a class="text-success"><i class="fas fa-info-circle"></i> ${msg}</a>`
}
}

// 0 = No issue
// [1-] = First field id that is wrong
function checkValues() {
var result = 0;

// Check the port number
var portNumStr = document.getElementById('portNum').value;
var portNum = parseInt(portNumStr);

if (isNaN(portNum) || portNum > 65535) {
console.warn('Cannot parse port number: ' + portNumStr);
displayMsg('error', `Port number ${portNumStr} is invalid.`);
result = 3;
} else {
console.log('using port ' + portNumStr + ' ' + portNum.toString());
}

return result;
}

function startServer() {
var retVal = checkValues();
if (retVal != 0) {
focusFieldWithError(retVal);
} else {
displayMsg('info', 'Everything looks good');
}
}

document.getElementById('backBtn').addEventListener('click', (evt) => {
var window = remote.getCurrentWindow();
window.webContents.goBack()
window.webContents.goBack();
})

document.getElementById('cancelBtn').addEventListener('click', (evt) => {
var window = remote.getCurrentWindow();
window.close()
window.close();
})

document.getElementById('submitBtn').addEventListener('click', (evt) => {
startServer();
})

document.getElementById('refillBtn').addEventListener('click', (evt) => {
getAndUpdateJLabCfg();
})

40 changes: 39 additions & 1 deletion src/renderer/new_server/newserver.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ body {
padding-bottom: 1%;
}

.result-box {
font-size: small;
text-align: left;
position:absolute;
bottom:0;
left:0;
padding-left: 3%;
padding-bottom: 1%;
}

.msg {
margin-top: 6%;
text-align: center;
Expand All @@ -27,12 +37,36 @@ body {
padding-right: 3%;
}

.refill-btn {
margin-left: 0;
padding-left: 0;
margin-right: 1%;
/* float: right; */
}

.refill-btn-tool-tip {
margin-left: 0;
display:inline-block;
/* vertical-align: bottom; */
font-size: smaller;
opacity: 0;
}

.refill-btn:hover + .refill-btn-tool-tip {
opacity: 1;
transition-delay: 0.3s;
}

.refill-btn-tool-tip {
}

.advanced-config-btn-grp {
display: inline;
margin-top: 7%;
}

.form-inline-link-div {
width: 15%;
width: 8%;
display: flex;
justify-content: center;
}
Expand All @@ -46,6 +80,10 @@ body {
padding-bottom: 10%;
}

#advanced-config-header-txt {
display: inline;
}

.no-top-padding {
padding-top: 0%;
}
Expand Down
1 change: 1 addition & 0 deletions src/renderer/new_server/newserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ document.getElementById('advancedUser').addEventListener('click', (evt) => {
console.log(filePath);
remote.getCurrentWindow().loadFile(filePath);
})

0 comments on commit 554f969

Please sign in to comment.