Skip to content

Commit f11252e

Browse files
committed
updating jlab code
1 parent 70cbb39 commit f11252e

File tree

4 files changed

+192
-25
lines changed

4 files changed

+192
-25
lines changed

jlab/lib/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { IDisposable } from '@phosphor/disposable';
22
import { JupyterLabPlugin } from '@jupyterlab/application';
33
import { DocumentRegistry } from '@jupyterlab/docregistry';
44
import { NotebookPanel, INotebookModel } from '@jupyterlab/notebook';
5-
import "../style/index.css";
5+
import '../style/index.css';
66
/**
7-
* The plugin registration information.
7+
* Initialization data for the jupyterlab_xkcd extension.
88
*/
9-
declare const plugin: JupyterLabPlugin<void>;
9+
declare const extension: JupyterLabPlugin<void>;
1010
/**
1111
* A notebook widget extension that adds a button to the toolbar.
1212
*/
@@ -16,4 +16,4 @@ export declare class ButtonExtension implements DocumentRegistry.IWidgetExtensio
1616
*/
1717
createNew(panel: NotebookPanel, context: DocumentRegistry.IContext<INotebookModel>): IDisposable;
1818
}
19-
export default plugin;
19+
export default extension;

jlab/lib/index.js

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = Object.setPrototypeOf ||
4+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6+
return function (d, b) {
7+
extendStatics(d, b);
8+
function __() { this.constructor = d; }
9+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10+
};
11+
})();
212
Object.defineProperty(exports, "__esModule", { value: true });
313
var disposable_1 = require("@phosphor/disposable");
14+
var application_1 = require("@jupyterlab/application");
415
var apputils_1 = require("@jupyterlab/apputils");
16+
var services_1 = require("@jupyterlab/services");
17+
var coreutils_1 = require("@phosphor/coreutils");
18+
var widgets_1 = require("@phosphor/widgets");
519
require("../style/index.css");
620
/**
7-
* The plugin registration information.
21+
* Initialization data for the jupyterlab_xkcd extension.
822
*/
9-
var plugin = {
10-
activate: activate,
11-
id: 'jupyter.extensions.new-button',
12-
autoStart: true
23+
var extension = {
24+
id: 'jupyterlab_kr',
25+
autoStart: true,
26+
requires: [apputils_1.ICommandPalette, application_1.ILayoutRestorer],
27+
activate: activate
1328
};
1429
/**
1530
* A notebook widget extension that adds a button to the toolbar.
@@ -53,13 +68,76 @@ var ButtonExtension = (function () {
5368
}());
5469
exports.ButtonExtension = ButtonExtension;
5570
/**
56-
* Activate the extension.
71+
* An new widget.
72+
*/
73+
var NewWidget = (function (_super) {
74+
__extends(NewWidget, _super);
75+
/**
76+
* Construct a new xkcd widget.
77+
*/
78+
function NewWidget() {
79+
var _this = _super.call(this) || this;
80+
_this.settings = services_1.ServerConnection.makeSettings();
81+
_this.id = 'newwidget';
82+
_this.title.label = 'My Widget';
83+
_this.title.closable = true;
84+
_this.addClass('jp-newWidget');
85+
return _this;
86+
}
87+
/**
88+
* Handle update requests for the widget.
89+
*/
90+
NewWidget.prototype.onUpdateRequest = function (msg) {
91+
};
92+
return NewWidget;
93+
}(widgets_1.Widget));
94+
;
95+
/**
96+
* Activate the xckd widget extension.
5797
*/
58-
function activate(app) {
98+
function activate(app, palette, restorer) {
99+
console.log('JupyterLab extension newwidget is activated!');
59100
app.docRegistry.addWidgetExtension('Notebook', new ButtonExtension());
101+
// Declare a widget variable
102+
var widget;
103+
// Add an application command
104+
var command = 'newwidget:open';
105+
app.commands.addCommand(command, {
106+
label: 'New Widget',
107+
execute: function () {
108+
if (!widget) {
109+
// Create a new widget if one does not exist
110+
widget = new NewWidget();
111+
widget.update();
112+
}
113+
if (!tracker.has(widget)) {
114+
// Track the state of the widget for later restoration
115+
tracker.add(widget);
116+
}
117+
if (!widget.isAttached) {
118+
// Attach the widget to the main area if it's not there
119+
app.shell.addToMainArea(widget);
120+
}
121+
else {
122+
// Refresh the widget
123+
widget.update();
124+
}
125+
// Activate the widget
126+
app.shell.activateById(widget.id);
127+
}
128+
});
129+
// Add the command to the palette.
130+
palette.addItem({ command: command, category: 'Tools' });
131+
// Track and restore the widget state
132+
var tracker = new apputils_1.InstanceTracker({ namespace: 'tools' });
133+
restorer.restore(tracker, {
134+
command: command,
135+
args: function () { return coreutils_1.JSONExt.emptyObject; },
136+
name: function () { return 'newwidget'; }
137+
});
60138
}
61139
;
62140
/**
63141
* Export the plugin as default.
64142
*/
65-
exports.default = plugin;
143+
exports.default = extension;

jlab/src/index.ts

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import {
33
} from '@phosphor/disposable';
44

55
import {
6-
JupyterLab, JupyterLabPlugin
6+
JupyterLab, JupyterLabPlugin, ILayoutRestorer // new
77
} from '@jupyterlab/application';
88

99
import {
10-
ToolbarButton
10+
ToolbarButton, ICommandPalette, InstanceTracker // new
1111
} from '@jupyterlab/apputils';
1212

1313
import {
@@ -18,19 +18,34 @@ import {
1818
NotebookPanel, INotebookModel
1919
} from '@jupyterlab/notebook';
2020

21+
import {
22+
ServerConnection
23+
} from '@jupyterlab/services';
24+
25+
import {
26+
JSONExt // new
27+
} from '@phosphor/coreutils';
28+
29+
import {
30+
Message
31+
} from '@phosphor/messaging';
32+
33+
import {
34+
Widget
35+
} from '@phosphor/widgets';
2136

22-
import "../style/index.css";
37+
import '../style/index.css';
2338

2439
/**
25-
* The plugin registration information.
40+
* Initialization data for the jupyterlab_xkcd extension.
2641
*/
27-
const plugin: JupyterLabPlugin<void> = {
28-
activate,
29-
id: 'jupyter.extensions.new-button',
30-
autoStart: true
42+
const extension: JupyterLabPlugin<void> = {
43+
id: 'jupyterlab_kr',
44+
autoStart: true,
45+
requires: [ICommandPalette, ILayoutRestorer],
46+
activate: activate
3147
};
3248

33-
3449
/**
3550
* A notebook widget extension that adds a button to the toolbar.
3651
*/
@@ -70,15 +85,83 @@ class ButtonExtension implements DocumentRegistry.IWidgetExtension<NotebookPanel
7085
}
7186
}
7287

88+
89+
/**
90+
* An new widget.
91+
*/
92+
class KnowledgeWidget extends Widget {
93+
/**
94+
* Construct a new xkcd widget.
95+
*/
96+
constructor() {
97+
super();
98+
this.settings = ServerConnection.makeSettings();
99+
100+
this.id = 'knowledge';
101+
this.title.label = 'Knowledge';
102+
this.title.closable = true;
103+
this.addClass('jp-knowledgeWidget');
104+
}
105+
106+
/**
107+
* The server settings associated with the widget.
108+
*/
109+
readonly settings: ServerConnection.ISettings;
110+
111+
/**
112+
* Handle update requests for the widget.
113+
*/
114+
onUpdateRequest(msg: Message): void {
115+
}
116+
};
117+
118+
73119
/**
74-
* Activate the extension.
120+
* Activate the xckd widget extension.
75121
*/
76-
function activate(app: JupyterLab) {
122+
function activate(app: JupyterLab, palette: ICommandPalette, restorer: ILayoutRestorer) {
123+
console.log('JupyterLab extension knowledgelab is activated!');
124+
77125
app.docRegistry.addWidgetExtension('Notebook', new ButtonExtension());
126+
127+
// Declare a widget variable
128+
let widget: KnowledgeWidget;
129+
130+
// Add an application command
131+
const command: string = 'knowledge:open';
132+
app.commands.addCommand(command, {
133+
label: 'Knowledge',
134+
execute: () => {
135+
if (!widget) {
136+
widget = new KnowledgeWidget();
137+
widget.update();
138+
}
139+
if (!tracker.has(widget)) {
140+
tracker.add(widget);
141+
}
142+
if (!widget.isAttached) {
143+
app.shell.addToMainArea(widget);
144+
} else {
145+
widget.update();
146+
}
147+
app.shell.activateById(widget.id);
148+
}
149+
});
150+
151+
// Add the command to the palette.
152+
palette.addItem({ command, category: 'Tools' });
153+
154+
// Track and restore the widget state
155+
let tracker = new InstanceTracker<Widget>({ namespace: 'tools' });
156+
restorer.restore(tracker, {
157+
command,
158+
args: () => JSONExt.emptyObject,
159+
name: () => 'knowledge'
160+
});
78161
};
79162

80163

81164
/**
82165
* Export the plugin as default.
83166
*/
84-
export default plugin;
167+
export default extension;

jlab/style/index.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
button.jp-KnowledgeRepo {
22
background: url('data:image/svg+xml;charset=UTF-8,<svg fill="#616161" width="1792" height="1792" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M491 1536l91-91-235-235-91 91v107h128v128h107zm523-928q0-22-22-22-10 0-17 7l-542 542q-7 7-7 17 0 22 22 22 10 0 17-7l542-542q7-7 7-17zm-54-192l416 416-832 832h-416v-416zm683 96q0 53-37 90l-166 166-416-416 166-165q36-38 90-38 53 0 91 38l235 234q37 39 37 91z"/></svg>');
3-
}
3+
}
4+
5+
.jp-knowledgeWidget {
6+
display: flex;
7+
flex-direction: column;
8+
overflow: auto;
9+
}

0 commit comments

Comments
 (0)