-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
340 lines (296 loc) · 12.1 KB
/
index.html
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Jupyter AWS Lambda - Demo Page</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T"
crossorigin="anonymous"></script>
<style type="text/css">
.code-container {
font-family: 'Courier New', Courier, monospace;
}
.page-header {
margin-bottom: 48px;
}
.hide {
display: none;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col col-md-12 page-header text-center">
<h1 class="text-center">Jupyter AWS Lambda - Demo Page</h1>
<br>
<button type="button" onClick="javascript:executeNotebook()" class="btn btn-danger">Execute Notebook</a>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-12 col-12">
<h3 class="text-center">Input Jupyter Notebook
<button type="button" onClick="javascript: document.getElementById('file').click()" class="btn btn-sm btn-primary">Upload from a file</a>
</h3>
<div id="root" cols="80" rows="20" class="form-control code-container"></div>
<textarea id="ipynbInputTextField" cols="80" rows="20" class="form-control code-container hide">
</textarea>
<br/>
<div class="hide">
<h3>Choose a notebook file to upload and then click Post JSON.</h3>
<input id="file" type="file" />
<input id="textfile" type="file" />
<input id="multipleFiles" type="file" multiple/>
<button onclick="downloadNotebook()">Download notebook</button>
<button onclick="executeNotebook()">Post JSON</button>
</div>
<h3 class="text-center"> Input text file
<button type="button" class="btn btn-sm btn-primary" onclick="javascript:document.getElementById('textfile').click()">Upload from a file</button>
<button type="button" class="btn btn-sm btn-warning" onclick="javascript:document.getElementById('multipleFiles').click()">Select mulitple files</button>
</h3>
<textarea id="textField" cols=80 rows=6 class="form-control">
</textarea>
<span style="font-size: 10px; font-weight: 100" id="fileinfo"></span>
<br/>
<h3 class="text-center"> Edit notebook
<select id="selectCell" name="selectCells" onchange="myCell(event)">
<option disabled selected>Select cell</option>
</select>
<button type="button" class="btn btn-sm btn-success" onclick="saveChanges()">Save changes</a>
</h3>
<div id="root2" cols="80" rows="20" class="form-control code-container">
<textarea id="editField" cols="80" rows="6" class="form-control code-container">
</textarea>
</div>
<span style="font-size: 10px; font-weight: 100" id="fileinfo"></span>
<br/>
</div>
<div class="col-md-6 col-sm-12 col-12">
<h3 class="text-center">Resulting IPYNB
<button type="button" class="btn btn-sm btn-success" onclick="downloadNotebook()">Download results</a>
</h3>
<div id="root1" cols="80" rows="20" class="form-control code-container"></div>
<textarea id="responseJSON" cols="80" rows="20" class="form-control code-container hide">
</textarea>
<h3 class="text-center">Test Results
<button type="button" class="btn btn-sm btn-success" onclick="downloadResults()">Download Test Results</a>
</h3>
<textarea id="resultJSON" cols="80" rows="6" class="form-control code-container">
</textarea>
</div>
</div>
</div>
<script>
function loadJSON(json) {
window.problemJsonFile = json;
const button = document.getElementById('NotebookPreview').nextSibling;
button.click();
}
var executeNotebook = function () {
window.problem = window.problemJsonFile;
const button = document.getElementById('root1').querySelector('#NotebookPreview').nextSibling;
button.click('e');
console.log("POSTing ipynb text as JSON");
var x = document.getElementById("ipynbInputTextField").value;
var inputText = document.getElementById("textField").value;
if (x)
var nb = JSON.parse(x);
if (inputText && inputText.length > 0 && (!window.UPLOADED_FILES || window.UPLOADED_FILES.length === 0)) {
window.UPLOADED_FILES = [
{name: 'data1.txt',
content: btoa(inputText)}
];
}
var result = {};
result['notebook'] = nb;
if (window.UPLOADED_FILES) {
result.files = {};
for (var i = 0; i < window.UPLOADED_FILES.length; i++) {
result.files[window.UPLOADED_FILES[i].name] = window.UPLOADED_FILES[i].content;
}
}
postData(result)
}
var postData = function (data) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function () {
if (xhr.status === 200) {
var userInfo = JSON.parse(xhr.responseText);
console.log(userInfo.duration, "seconds of execution time.")
console.log(userInfo,"userInfo")
document.getElementById("responseJSON").value = JSON.stringify(userInfo.ipynb);
document.getElementById("resultJSON").value = JSON.stringify(userInfo.result);
// pass data to component via problem global object
window.problem = userInfo.ipynb;
const button = document.getElementById('root1').querySelector('#NotebookPreview').nextSibling;
button.click('e');
}
else {
console.log("Nope");
document.getElementById("responseJSON").value = "Nope";
}
};
xhr.send(JSON.stringify(data));
}
function encodeUploadedFiles(files) {
console.log(files);
for (var i = 0; i < files.length; i++) {
var reader = new FileReader();
const currentFile = files[i];
reader.onload = function (readerEvt) {
var binaryString = readerEvt.target.result;
var fileInfo = {name: currentFile.name, content: btoa(binaryString) };
window.UPLOADED_FILES.push(fileInfo);
};
reader.readAsBinaryString(files[i]);
}
$("#fileinfo").text("Selected files: " + files.length);
}
/*
This function is being executed each time a new .ipynb file is selected. It puts
contents of the .ipynb file to the input text area with the 'ipynbInputTextField' ID.
*/
function onChange(event) {
var reader = new FileReader();
reader.onload = onInputIpynbReaderLoad;
reader.readAsText(event.target.files[0]);
}
function onMultipleFilesSelected(event) {
window.UPLOADED_FILES = [];
encodeUploadedFiles(event.target.files);
}
/*
This function is being executed each time a new text file is selected.
It puts contents of the file to the text area with the 'textField' ID.
Text area contains a non-encoded version of the file.
*/
function onTextFileSelected(event) {
console.log('onTextFileSelected entered')
var fileReader = new FileReader();
fileReader.onload = textFileReaderLoadHandler;
fileReader.readAsText(event.target.files[0]);
window.UPLOADED_FILES = [];
encodeUploadedFiles([event.target.files[0]]);
}
function onInputIpynbReaderLoad(event) {
//console.log(event.target.result);
if (event.target.result) {
var obj = JSON.parse(event.target.result);
}
loadJSON(obj);
var sel = document.getElementById("selectCell");
$("#selectCell").empty().append("<option disabled selected>Select cell</option>");
document.getElementById("editField").value = null;
var opt = null;
for(i = 0; i<obj.cells.length; i++) {
opt = document.createElement("option");
opt.value = obj.cells[i].source.join("");
opt.innerHTML = i;
sel.appendChild(opt);
}
document.getElementById("ipynbInputTextField").value = JSON.stringify(obj);
}
function myCell(e) {
document.getElementById("editField").value = e.target.value;
}
function textFileReaderLoadHandler(event) {
//console.log(event.target.result);
if (event.target.result) {
var obj = event.target.result;
}
document.getElementById("textField").value = obj;
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function saveChanges() {
var e = document.getElementById("selectCell");
var selectValue = e.options[e.selectedIndex].innerHTML;
var textValue = [];
textValue.push(document.getElementById("editField").value);
var obj = JSON.parse(document.getElementById("ipynbInputTextField").value);
obj.cells[selectValue].source = textValue;
e.options[e.selectedIndex].value = textValue;
document.getElementById("ipynbInputTextField").value = JSON.stringify(obj);
loadJSON(obj);
}
function downloadNotebook() {
var fileContents = document.getElementById("responseJSON").value;
download("notebook.ipynb", fileContents);
}
function downloadResults() {
var fileContents = document.getElementById("resultJSON").value;
download("results.json", fileContents);
}
document.getElementById('file').addEventListener('change', onChange);
document.getElementById('textfile').addEventListener('change', onTextFileSelected);
document.getElementById('multipleFiles').addEventListener('change', onMultipleFilesSelected);
</script>
<script type="text/javascript" src="https://s3-ap-southeast-1.amazonaws.com/alset-public/main-jupter-react-viewer.js"></script>
<script>
( () => {
const sampleFile = {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"for x in range(15):\n",
" print(x)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
};
document.getElementById("ipynbInputTextField").value = JSON.stringify(sampleFile);
loadJSON(sampleFile);
var sel = document.getElementById('selectCell');
var opt = null;
for(i = 0; i<sampleFile.cells.length; i++) {
opt = document.createElement('option');
opt.value = sampleFile.cells[i].source.join("");
opt.innerHTML = i;
sel.appendChild(opt);
}
}
)();</script>
</body>
</html>