Skip to content

Commit 0897314

Browse files
author
minjk-bl
committed
CHROME: change Jupyter api to colab api
1 parent abb7029 commit 0897314

File tree

2 files changed

+132
-63
lines changed

2 files changed

+132
-63
lines changed

js/com/com_Config.js

Lines changed: 128 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ define([
3939
//========================================================================
4040
// Constructor
4141
//========================================================================
42-
constructor(initialData) {
42+
/**
43+
*
44+
* @param {*} initialData
45+
* @param {*} extensionType extension type: notebook/chrome/lab
46+
*/
47+
constructor(extensionType='notebook', initialData={}) {
48+
// initial mode
49+
this.extensionType = extensionType;
4350
// initial configuration
4451
this.data = {
4552
// Configuration
@@ -291,15 +298,22 @@ define([
291298
}
292299

293300
loadData(configKey = 'vpudf') {
301+
let that = this;
294302
return new Promise(function(resolve, reject) {
295-
Jupyter.notebook.config.load();
296-
Jupyter.notebook.config.loaded.then(function() {
297-
var data = Jupyter.notebook.config.data[configKey];
298-
if (data == undefined) {
299-
data = {};
300-
}
301-
resolve(data);
302-
});
303+
// CHROME: edited to use chrome.storage
304+
if (that.extensionType === 'notebook') {
305+
Jupyter.notebook.config.load();
306+
Jupyter.notebook.config.loaded.then(function() {
307+
var data = Jupyter.notebook.config.data[configKey];
308+
if (data == undefined) {
309+
data = {};
310+
}
311+
resolve(data);
312+
});
313+
} else if (that.extensionType === 'chrome') {
314+
// CHROME: TODO: chrome.storage
315+
resolve({});
316+
}
303317
});
304318
};
305319

@@ -310,39 +324,50 @@ define([
310324
* @returns
311325
*/
312326
getData(dataKey='', configKey='vpudf') {
327+
let that = this;
313328
return new Promise(function(resolve, reject) {
314-
Jupyter.notebook.config.load();
315-
Jupyter.notebook.config.loaded.then(function() {
316-
var data = Jupyter.notebook.config.data[configKey];
317-
if (data == undefined) {
318-
resolve(data);
319-
return;
320-
}
321-
if (dataKey == '') {
322-
resolve(data);
323-
return;
324-
}
325-
if (Object.keys(data).length > 0) {
326-
resolve(data[dataKey]);
327-
return;
328-
}
329-
reject('No data available.');
330-
});
329+
if (that.extensionType === 'notebook') {
330+
Jupyter.notebook.config.load();
331+
Jupyter.notebook.config.loaded.then(function() {
332+
var data = Jupyter.notebook.config.data[configKey];
333+
if (data == undefined) {
334+
resolve(data);
335+
return;
336+
}
337+
if (dataKey == '') {
338+
resolve(data);
339+
return;
340+
}
341+
if (Object.keys(data).length > 0) {
342+
resolve(data[dataKey]);
343+
return;
344+
}
345+
reject('No data available.');
346+
});
347+
} else if (that.extensionType === 'chrome') {
348+
// CHROME: TODO: chrome.storage
349+
resolve({});
350+
}
331351
});
332352
}
333353

334354
getDataSimple(dataKey='', configKey='vpudf') {
335-
Jupyter.notebook.config.load();
336-
var data = Jupyter.notebook.config.data[configKey];
337-
if (data == undefined) {
355+
if (this.extensionType === 'notebook') {
356+
Jupyter.notebook.config.load();
357+
var data = Jupyter.notebook.config.data[configKey];
358+
if (data == undefined) {
359+
return undefined;
360+
}
361+
if (dataKey == '') {
362+
return data;
363+
}
364+
if (Object.keys(data).length > 0) {
365+
return data[dataKey];
366+
}
367+
} else if (this.extensionType === 'chrome') {
368+
// CHROME: TODO: chrome.storage
338369
return undefined;
339370
}
340-
if (dataKey == '') {
341-
return data;
342-
}
343-
if (Object.keys(data).length > 0) {
344-
return data[dataKey];
345-
}
346371

347372
return undefined;
348373
}
@@ -353,17 +378,27 @@ define([
353378
* @param {String} configKey
354379
*/
355380
setData(dataObj, configKey='vpudf') {
356-
// set data using key
357-
Jupyter.notebook.config.loaded.then(function() {
358-
Jupyter.notebook.config.update({[configKey]: dataObj});
359-
});
381+
if (this.extensionType === 'notebook') {
382+
// set data using key
383+
Jupyter.notebook.config.loaded.then(function() {
384+
Jupyter.notebook.config.update({[configKey]: dataObj});
385+
});
386+
} else if (this.extensionType === 'chrome') {
387+
// CHROME: TODO: chrome.storage
388+
389+
}
360390
}
361391

362392
removeData(key, configKey = 'vpudf') {
363-
// if set value to null, it removes from config data
364-
Jupyter.notebook.config.loaded.then(function() {
365-
Jupyter.notebook.config.update({[configKey]: {[key]: null}});
366-
});
393+
if (this.extensionType === 'notebook') {
394+
// if set value to null, it removes from config data
395+
Jupyter.notebook.config.loaded.then(function() {
396+
Jupyter.notebook.config.update({[configKey]: {[key]: null}});
397+
});
398+
} else if (this.extensionType === 'chrome') {
399+
// CHROME: TODO: chrome.storage
400+
401+
}
367402
}
368403

369404
/**
@@ -372,18 +407,35 @@ define([
372407
* @param {String} configKey
373408
*/
374409
getMetadata(dataKey='', configKey='vp') {
375-
let metadata = Jupyter.notebook.metadata[configKey];
376-
if (metadata) {
377-
// update this metadataSetting
378-
this.metadataSettings = {
379-
...this.metadataSettings,
380-
...metadata
381-
};
382-
// no datakey, return all metadata
383-
if (dataKey == '') {
384-
return metadata;
410+
if (this.extensionType === 'notebook') {
411+
let metadata = Jupyter.notebook.metadata[configKey];
412+
if (metadata) {
413+
// update this metadataSetting
414+
this.metadataSettings = {
415+
...this.metadataSettings,
416+
...metadata
417+
};
418+
// no datakey, return all metadata
419+
if (dataKey == '') {
420+
return metadata;
421+
}
422+
return metadata[dataKey];
423+
}
424+
} else if (this.extensionType === 'chrome') {
425+
// CHROME: use colab.global.notebookModel.metadata
426+
let metadata = colab.global.notebookModel.metadata[configKey];
427+
if (metadata) {
428+
// update this metadataSetting
429+
this.metadataSettings = {
430+
...this.metadataSettings,
431+
...metadata
432+
};
433+
// no datakey, return all metadata
434+
if (dataKey == '') {
435+
return metadata;
436+
}
437+
return metadata[dataKey];
385438
}
386-
return metadata[dataKey];
387439
}
388440
return {};
389441
}
@@ -394,27 +446,41 @@ define([
394446
* @param {String} configKey
395447
*/
396448
setMetadata(dataObj, configKey='vp') {
397-
let oldData = Jupyter.notebook.metadata[configKey];
398-
Jupyter.notebook.metadata[configKey] = {
399-
...oldData,
400-
...dataObj
401-
};
402-
Jupyter.notebook.set_dirty();
449+
if (this.extensionType === 'notebook') {
450+
let oldData = Jupyter.notebook.metadata[configKey];
451+
Jupyter.notebook.metadata[configKey] = {
452+
...oldData,
453+
...dataObj
454+
};
455+
Jupyter.notebook.set_dirty();
456+
457+
} else if (this.extensionType === 'chrome') {
458+
// CHROME: use colab.global.notebookModel.metadata
459+
let oldData = colab.global.notebookModel.metadata[configKey];
460+
colab.global.notebookModel.metadata[configKey] = {
461+
...oldData,
462+
...dataObj
463+
};
464+
}
403465

404466
// update this metadataSetting
405467
this.metadataSettings = {
406468
...this.metadataSettings,
407469
...dataObj
408470
};
409-
410471
}
411472

412473
/**
413474
* Reset metadata (on jupyter file)
414475
* @param {String} configKey
415476
*/
416477
resetMetadata(configKey='vp') {
417-
Jupyter.notebook.metadata[configKey] = {};
478+
if (this.extensionType === 'notebook') {
479+
Jupyter.notebook.metadata[configKey] = {};
480+
} else if (this.extensionType === 'chrome') {
481+
// CHROME: use colab.global.notebookModel.metadata
482+
colab.global.notebookModel.metadata[configKey] = {};
483+
}
418484
}
419485

420486
/**

js/com/com_Const.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ define ([
3131
// Base Path Constants
3232
//========================================================================
3333
Constants.PATH_SEPARATOR = "/";
34-
Constants.BASE_PATH = Jupyter.notebook.base_url + "nbextensions" + Constants.PATH_SEPARATOR + "visualpython" + Constants.PATH_SEPARATOR;
34+
// CHROME: edited
35+
// Constants.BASE_PATH = Jupyter.notebook.base_url + "nbextensions" + Constants.PATH_SEPARATOR + "visualpython" + Constants.PATH_SEPARATOR;
36+
Constants.BASE_PATH = vpBase + "visualpython" + Constants.PATH_SEPARATOR;
37+
3538
Constants.SOURCE_PATH = Constants.BASE_PATH + "js" + Constants.PATH_SEPARATOR;
3639
Constants.IMAGE_PATH = Constants.BASE_PATH + "img" + Constants.PATH_SEPARATOR;
3740
Constants.STYLE_PATH = Constants.BASE_PATH + "css" + Constants.PATH_SEPARATOR;

0 commit comments

Comments
 (0)