Skip to content

Commit 19d9fe1

Browse files
committed
ability to append results to the result
1 parent 9bddcd7 commit 19d9fe1

3 files changed

Lines changed: 72 additions & 32 deletions

File tree

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@
708708
},
709709
"dependencies": {
710710
"@types/jquery": "^1.10.31",
711+
"@types/socket.io": "^1.4.27",
712+
"@types/socket.io-client": "^1.4.27",
711713
"@types/uuid": "^3.3.27",
712714
"anser": "^1.1.0",
713715
"copy-paste": "^1.3.0",
@@ -718,6 +720,7 @@
718720
"line-by-line": "^0.1.4",
719721
"named-js-regexp": "^1.3.1",
720722
"prepend-file": "^1.3.0",
723+
"socket.io": "^1.4.8",
721724
"spawnteract": "^2.2.0",
722725
"tmp": "0.0.28",
723726
"transformime": "^3.1.2",

src/client/jupyter/browser/main.ts

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,56 @@ const transformime = require('transformime');
44
const MarkdownTransform = require('transformime-marked');
55
const transform = transformime.createTransform([MarkdownTransform]) as Function;
66

7-
(window as any).initializeResults = (rootDirName) => {
8-
const data = (window as any).JUPYTER_DATA as any[];
9-
const __dirname = rootDirName;
10-
data.forEach(data => {
11-
if (typeof data['text/html'] === 'string') {
12-
data['text/html'] = data['text/html'].replace(/<\/scripts>/g, '</script>');
7+
function displayData(data: any, whiteBg: boolean): Promise<HTMLElement> {
8+
if (typeof data['text/html'] === 'string') {
9+
data['text/html'] = data['text/html'].replace(/<\/scripts>/g, '</script>');
10+
}
11+
return transform(data).then(result => {
12+
// If dealing with images add them inside a div with white background
13+
if (whiteBg === true || Object.keys(data).some(key => key.startsWith('image/'))) {
14+
const div = document.createElement('div');
15+
div.style.backgroundColor = 'white';
16+
div.style.display = 'inline-block';
17+
div.appendChild(result.el);
18+
document.body.appendChild(div);
19+
return div;
20+
}
21+
else {
22+
document.body.appendChild(result.el);
23+
return result.el;
24+
}
25+
});
26+
}
27+
28+
(window as any).initializeResults = (rootDirName: string, port: number, whiteBg: boolean) => {
29+
const results = (window as any).JUPYTER_DATA as any[];
30+
(window as any).__dirname = rootDirName;
31+
try {
32+
if (typeof port === 'number' && port > 0) {
33+
var socket = (window as any).io.connect('http://localhost:' + port);
34+
socket.on('results', function (results: any[]) {
35+
const promises = results.map(data => displayData(data, whiteBg));
36+
Promise.all<HTMLElement>(promises).then(elements => {
37+
// Bring the first item into view
38+
if (elements.length > 0) {
39+
elements[0].scrollIntoView(true);
40+
}
41+
});
42+
});
43+
}
44+
}
45+
catch (ex) {
46+
const errorDiv = document.createElement('div');
47+
errorDiv.innerHTML = 'Initializing live updates for results failed with the following error:\n' + ex.message;
48+
errorDiv.style.color = 'red';
49+
document.body.appendChild(errorDiv);
50+
}
51+
52+
const promises = results.map(data => displayData(data, whiteBg));
53+
Promise.all<HTMLElement>(promises).then(elements => {
54+
// Bring the first item into view
55+
if (elements.length > 0) {
56+
elements[0].scrollIntoView(true);
1357
}
14-
transform(data).then(result => {
15-
// If dealing with images add them inside a div with white background
16-
if (Object.keys(data).some(key => key.startsWith('image/'))) {
17-
const div = document.createElement('div');
18-
div.style.backgroundColor = 'white';
19-
div.style.display = 'inline-block';
20-
div.appendChild(result.el);
21-
document.body.appendChild(div);
22-
}
23-
else {
24-
document.body.appendChild(result.el);
25-
}
26-
});
2758
});
2859
};

src/client/jupyter/display/main.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,43 @@ import {KernelPicker} from './kernelPicker';
33
import {Commands} from '../../common/constants';
44
import {KernelspecMetadata} from '../contracts';
55
import {TextDocumentContentProvider} from './resultView';
6+
import {Server} from '../server/main';
67

78
const jupyterSchema = 'jupyter-result-viewer';
89
const previewUri = vscode.Uri.parse(jupyterSchema + '://authority/jupyter');
910

1011
export class JupyterDisplay extends vscode.Disposable {
1112
private disposables: vscode.Disposable[];
1213
private previewWindow: TextDocumentContentProvider;
14+
private server: Server;
1315
constructor() {
1416
super(() => { });
1517
this.disposables = [];
1618
this.disposables.push(new KernelPicker());
1719
this.disposables.push(vscode.commands.registerCommand(Commands.Jupyter.Kernel_Options, this.showKernelOptions.bind(this)));
20+
this.server = new Server();
21+
this.disposables.push(this.server);
1822
this.previewWindow = new TextDocumentContentProvider();
1923
this.disposables.push(vscode.workspace.registerTextDocumentContentProvider(jupyterSchema, this.previewWindow));
2024
}
2125

22-
2326
private displayed = false;
2427
public showResults(result: string, data: any): any {
25-
this.previewWindow.setText(result, data);
26-
// Dirty hack to support instances when document has been closed
27-
if (this.displayed) {
28-
this.previewWindow.update();
29-
}
30-
this.displayed = true;
31-
return vscode.commands.executeCommand('vscode.previewHtml', previewUri, vscode.ViewColumn.Two, 'Results')
32-
.then(() => {
33-
// Do nothing
34-
}, reason => {
35-
vscode.window.showErrorMessage(reason);
36-
});
28+
this.server.start().then(port => {
29+
this.previewWindow.ServerPort = port;
30+
this.previewWindow.setText(result, data);
31+
// Dirty hack to support instances when document has been closed
32+
if (this.displayed) {
33+
this.previewWindow.update();
34+
}
35+
this.displayed = true;
36+
return vscode.commands.executeCommand('vscode.previewHtml', previewUri, vscode.ViewColumn.Two, 'Results')
37+
.then(() => {
38+
// Do nothing
39+
}, reason => {
40+
vscode.window.showErrorMessage(reason);
41+
});
42+
});
3743
}
3844

3945
public dispose() {

0 commit comments

Comments
 (0)