Skip to content

Commit dce59aa

Browse files
committed
Improves the manager and adds a registry to register blocks, generators and toolboxes
1 parent d56ccaf commit dce59aa

File tree

14 files changed

+1127
-998
lines changed

14 files changed

+1127
-998
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ Repo: https://github.com/google/blockly
2020
To install the extension, execute:
2121

2222
```bash
23-
micromamba create -n blockly -c conda-forge python
23+
micromamba create -n blockly -c conda-forge python ipykernel xeus-python xeus-lua
2424
micromamba activate blockly
2525
pip install jupyterlab_blockly
2626
```
2727

28+
#### Kernels
29+
* ipykernel
30+
* xeus-python
31+
* xeus-lua
32+
* [JavaScript](https://github.com/n-riesco/ijavascript#installation)
33+
* [JavaScript](https://github.com/yunabe/tslab)
34+
2835
## Uninstall
2936

3037
To remove the extension, execute:

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@
4848
"dependencies": {
4949
"@jupyterlab/application": "^3.4",
5050
"@jupyterlab/apputils": "^3.4",
51+
"@jupyterlab/coreutils": "^5.4",
5152
"@jupyterlab/docregistry": "^3.4",
5253
"@jupyterlab/filebrowser": "^3.4",
5354
"@jupyterlab/launcher": "^3.4",
5455
"@jupyterlab/outputarea": "^3.4",
5556
"@jupyterlab/rendermime": "^3.4",
57+
"@jupyterlab/services": "^6.4",
58+
"@jupyterlab/ui-components": "^3.4",
5659
"@lumino/algorithm": "^1.9.0",
5760
"@lumino/coreutils": "^1.11.0",
5861
"@lumino/messaging": "^1.10.0",

src/factory.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
DocumentModel
55
} from '@jupyterlab/docregistry';
66
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
7-
// import { ITranslator } from '@jupyterlab/translation';
87

98
import { BlocklyEditor, BlocklyPanel } from './widget';
9+
import { BlocklyRegistry } from './registry';
1010
import { BlocklyManager } from './manager';
1111

1212
/**
@@ -16,10 +16,8 @@ export class BlocklyEditorFactory extends ABCWidgetFactory<
1616
BlocklyEditor,
1717
DocumentModel
1818
> {
19-
private _manager: BlocklyManager;
19+
private _registry: BlocklyRegistry;
2020
private _rendermime: IRenderMimeRegistry;
21-
private _language: string;
22-
// private _translator: ITranslator;
2321

2422
/**
2523
* Constructor of BlocklyEditorFactory.
@@ -28,14 +26,12 @@ export class BlocklyEditorFactory extends ABCWidgetFactory<
2826
*/
2927
constructor(options: BlocklyEditorFactory.IOptions) {
3028
super(options);
31-
this._manager = new BlocklyManager();
29+
this._registry = new BlocklyRegistry();
3230
this._rendermime = options.rendermime;
33-
this._language = this._manager.language;
34-
// this._translator = options.translator;
3531
}
3632

37-
get manager(): BlocklyManager {
38-
return this._manager;
33+
get registry(): BlocklyRegistry {
34+
return this._registry;
3935
}
4036

4137
/**
@@ -47,15 +43,9 @@ export class BlocklyEditorFactory extends ABCWidgetFactory<
4743
protected createNewWidget(
4844
context: DocumentRegistry.IContext<DocumentModel>
4945
): BlocklyEditor {
50-
return new BlocklyEditor({
51-
context,
52-
content: new BlocklyPanel(
53-
context,
54-
this._manager,
55-
this._rendermime,
56-
this._language
57-
)
58-
});
46+
const manager = new BlocklyManager(this._registry, context.sessionContext);
47+
const content = new BlocklyPanel(context, manager, this._rendermime);
48+
return new BlocklyEditor({ context, content, manager });
5949
}
6050
}
6151

src/index.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
JupyterFrontEndPlugin,
44
ILayoutRestorer
55
} from '@jupyterlab/application';
6+
import { jsonIcon } from '@jupyterlab/ui-components';
67
import { WidgetTracker } from '@jupyterlab/apputils';
78
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
89
import { ICommandPalette } from '@jupyterlab/apputils';
@@ -12,7 +13,7 @@ import { ITranslator } from '@jupyterlab/translation';
1213
import { ISettingRegistry } from '@jupyterlab/settingregistry';
1314

1415
import { BlocklyEditorFactory } from './factory';
15-
import { IBlocklyManager } from './token';
16+
import { IBlocklyRegisty } from './token';
1617
import { BlocklyEditor } from './widget';
1718

1819
import { blockly_icon } from './icons';
@@ -36,7 +37,7 @@ const PLUGIN_ID = '@jupyterlab/translation-extension:plugin';
3637
/**
3738
* Initialization data for the jupyterlab-blocky extension.
3839
*/
39-
const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
40+
const plugin: JupyterFrontEndPlugin<IBlocklyRegisty> = {
4041
id: 'jupyterlab-blocky:plugin',
4142
autoStart: true,
4243
requires: [
@@ -47,7 +48,7 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
4748
ITranslator
4849
],
4950
optional: [ILauncher, ICommandPalette],
50-
provides: IBlocklyManager,
51+
provides: IBlocklyRegisty,
5152
activate: (
5253
app: JupyterFrontEnd,
5354
restorer: ILayoutRestorer,
@@ -57,7 +58,7 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
5758
translator: ITranslator,
5859
launcher: ILauncher | null,
5960
palette: ICommandPalette | null
60-
): IBlocklyManager => {
61+
): IBlocklyRegisty => {
6162
console.log('JupyterLab extension jupyterlab-blocky is activated!');
6263

6364
// Namespace for the tracker
@@ -84,8 +85,8 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
8485
const widgetFactory = new BlocklyEditorFactory({
8586
name: FACTORY,
8687
modelName: 'text',
87-
fileTypes: ['json'],
88-
defaultFor: ['json'],
88+
fileTypes: ['blockly'],
89+
defaultFor: ['blockly'],
8990

9091
// Kernel options, in this case we need to execute the code generated
9192
// in the blockly editor. The best way would be to use kernels, for
@@ -114,6 +115,17 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
114115
});
115116
tracker.add(widget);
116117
});
118+
// Registering the file type
119+
app.docRegistry.addFileType({
120+
name: 'blockly',
121+
displayName: 'Blockly',
122+
contentType: 'file',
123+
fileFormat: 'json',
124+
extensions: ['.jpblockly'],
125+
mimeTypes: ['application/json'],
126+
icon: jsonIcon,
127+
iconLabel: 'JupyterLab-Blockly'
128+
});
117129
// Registering the widget factory
118130
app.docRegistry.addWidgetFactory(widgetFactory);
119131

@@ -140,7 +152,7 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
140152
console.log(`Current Language : '${language}'`);
141153

142154
// Transmitting the current language to the manager.
143-
widgetFactory.manager.setlanguage(language);
155+
widgetFactory.registry.setlanguage(language);
144156
});
145157

146158
commands.addCommand(command, {
@@ -187,7 +199,7 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
187199
});
188200
}
189201

190-
return widgetFactory.manager;
202+
return widgetFactory.registry;
191203
}
192204
};
193205

src/layout.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { SimplifiedOutputArea, OutputAreaModel } from '@jupyterlab/outputarea';
22
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
3-
import { ISessionContext } from '@jupyterlab/apputils';
4-
// import { ITranslator } from '@jupyterlab/translation';
3+
import { ISessionContext, showErrorMessage } from '@jupyterlab/apputils';
54

65
import { Message } from '@lumino/messaging';
76
import { PartialJSONValue } from '@lumino/coreutils';
@@ -22,7 +21,6 @@ export class BlocklyLayout extends PanelLayout {
2221
private _manager: BlocklyManager;
2322
private _workspace: Blockly.WorkspaceSvg;
2423
private _sessionContext: ISessionContext;
25-
// private _translator: ITranslator;
2624
private _outputArea: SimplifiedOutputArea;
2725

2826
/**
@@ -33,12 +31,10 @@ export class BlocklyLayout extends PanelLayout {
3331
manager: BlocklyManager,
3432
sessionContext: ISessionContext,
3533
rendermime: IRenderMimeRegistry
36-
// translator: ITranslator
3734
) {
3835
super();
3936
this._manager = manager;
4037
this._sessionContext = sessionContext;
41-
// this._translator = translator;
4238

4339
// Creating the container for the Blockly editor
4440
// and the output area to render the execution replies.
@@ -106,12 +102,22 @@ export class BlocklyLayout extends PanelLayout {
106102

107103
// Execute the code using the kernel, by using a static method from the
108104
// same class to make an execution request.
109-
SimplifiedOutputArea.execute(code, this._outputArea, this._sessionContext)
110-
.then(resp => {
111-
this.addWidget(this._outputArea);
112-
this._resizeWorkspace();
113-
})
114-
.catch(e => console.error(e));
105+
if (this._sessionContext.hasNoKernel) {
106+
// Check whether there is a kernel
107+
showErrorMessage(
108+
'Select a valid kernel',
109+
`There is not a valid kernel selected, select one from the dropdown menu in the toolbar.
110+
If there isn't a valid kernel please install 'xeus-python' from Pypi.org or using mamba.
111+
`
112+
);
113+
} else {
114+
SimplifiedOutputArea.execute(code, this._outputArea, this._sessionContext)
115+
.then(resp => {
116+
this.addWidget(this._outputArea);
117+
this._resizeWorkspace();
118+
})
119+
.catch(e => console.error(e));
120+
}
115121
}
116122

117123
/**
@@ -144,13 +150,6 @@ export class BlocklyLayout extends PanelLayout {
144150
toolbox: this._manager.toolbox,
145151
theme: THEME
146152
});
147-
148-
// let categories: string;
149-
150-
// Loading the ITranslator
151-
// const trans = this._translator.load('jupyterlab-blockly');
152-
153-
// categories = trans.__('Category');
154153
}
155154

156155
private _resizeWorkspace(): void {

0 commit comments

Comments
 (0)