Skip to content

Commit 3aae241

Browse files
authored
Merge pull request #15 from DenisaCG/code_execution
code execution implementation
2 parents 6c7a102 + 8d38929 commit 3aae241

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

src/layout.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SimplifiedOutputArea, OutputAreaModel } from '@jupyterlab/outputarea';
12
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
23
import { ISessionContext } from '@jupyterlab/apputils';
34

@@ -18,8 +19,8 @@ export class BlocklyLayout extends PanelLayout {
1819
private _host: HTMLElement;
1920
private _manager: BlocklyManager;
2021
private _workspace: Blockly.WorkspaceSvg;
21-
//private _sessionContext: ISessionContext;
22-
private _outputArea: Widget;
22+
private _sessionContext: ISessionContext;
23+
private _outputArea: SimplifiedOutputArea;
2324

2425
/**
2526
* Construct a `BlocklyLayout`.
@@ -32,12 +33,18 @@ export class BlocklyLayout extends PanelLayout {
3233
) {
3334
super();
3435
this._manager = manager;
35-
//this._sessionContext = sessionContext;
36+
this._sessionContext = sessionContext;
3637

3738
// Creating the container for the Blockly editor
3839
// and the output area to render the execution replies.
3940
this._host = document.createElement('div');
40-
this._outputArea = new Widget();
41+
42+
// Creating a SimplifiedOutputArea widget to render the
43+
// outputs from the execution reply.
44+
this._outputArea= new SimplifiedOutputArea({
45+
model: new OutputAreaModel({ trusted: true }),
46+
rendermime
47+
});
4148
}
4249

4350
get workspace(): PartialJSONValue {
@@ -86,8 +93,17 @@ export class BlocklyLayout extends PanelLayout {
8693
}
8794

8895
run(): void {
89-
//const code = this._manager.generator.workspaceToCode(this._workspace);
90-
// Execute the code using the kernel
96+
// Serializing our workspace into the chosen language generator.
97+
const code = this._manager.generator.workspaceToCode(this._workspace);
98+
99+
// Execute the code using the kernel, by using a static method from the
100+
// same class to make an execution request.
101+
SimplifiedOutputArea.execute(code, this._outputArea, this._sessionContext)
102+
.then(resp => {
103+
this.addWidget(this._outputArea);
104+
this._resizeWorkspace();
105+
})
106+
.catch(e => console.error(e));
91107
}
92108

93109
/**

src/widget.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
DocumentWidget,
44
DocumentModel
55
} from '@jupyterlab/docregistry';
6+
import { ToolbarButton } from '@jupyterlab/apputils';
67
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
8+
import { runIcon } from '@jupyterlab/ui-components';
79

810
import { Panel } from '@lumino/widgets';
911
import { Signal } from '@lumino/signaling';
@@ -20,9 +22,18 @@ export class BlocklyEditor extends DocumentWidget<BlocklyPanel, DocumentModel> {
2022

2123
// Create and add a button to the toolbar to execute
2224
// the code.
23-
// Example: https://github.com/jupyterlab/extension-examples/blob/9c35013ce5da125f1b5865b3f7cbb301970d5970/toolbar-button/src/index.ts#L44-L52
24-
// (this.content.layout as BlocklyLayout).run();
25-
// this.toolbar.addItem('run', button);
25+
const runCode = () => {
26+
(this.content.layout as BlocklyLayout).run();
27+
};
28+
const button = new ToolbarButton({
29+
label: 'Run Code',
30+
icon: runIcon,
31+
className: 'jp-blockly-button',
32+
onClick: runCode,
33+
tooltip: 'Run Code'
34+
});
35+
button.addClass('jp-blockly-runButton');
36+
this.toolbar.addItem('run', button);
2637
}
2738

2839
/**

style/base.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.jp-blockly-button:hover {
2+
background-color: transparent !important;
3+
}
4+
5+
.jp-blockly-runButton {
6+
background-color: var(--md-green-500)
7+
}
8+
9+
.jp-blockly-runButton:hover {
10+
background-color: var(--md-green-700)
11+
}

0 commit comments

Comments
 (0)