Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into michal/6437
Browse files Browse the repository at this point in the history
* origin/master:
  removing error message from stdout in test + test refactoring (#6480)
  #6479 add readme to find start of documentation
  #6479 - fixed doc file names to match camel case (#6488)
  #6429 Added jupyterlab and jupyterhub. (#6472)
  Spot/6483 (#6485)
  #6410 implement %%javascript magic for rendering the `application/javascript` mime (#6482)

# Conflicts:
#	docker/setup.sh
  • Loading branch information
michalgce committed Dec 12, 2017
2 parents 2d37fe2 + 0fca768 commit 5977051
Show file tree
Hide file tree
Showing 36 changed files with 993 additions and 434 deletions.
62 changes: 62 additions & 0 deletions beakerx/jslab/src/javascriptRendererExtension.ts
@@ -0,0 +1,62 @@
/*
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { RenderedJavaScript } from '@jupyterlab/rendermime';

export const TEXT_JAVASCRIPT_MIMETYPE = 'text/javascript';
export const APPLICATION_JAVASCRIPT_MIMETYPE = 'application/javascript';

export class BeakerxRenderedJavascript extends RenderedJavaScript {
render(model: IRenderMime.IMimeModel): Promise<void> {
const evalInContext = function (str) { return eval(str); }.bind(this);

try {
evalInContext(String(model.data[this.mimeType]));

return Promise.resolve(undefined);
} catch (err) {
console.error(err);

const pre = document.createElement('pre');

pre.classList.add('js-error');
pre.innerHTML = `${String(err.message)}<br>See your browser Javascript console for more details.`;
this.node.appendChild(pre);

return Promise.resolve(undefined);
}
}
}

/**
* A mime renderer factory for text/javascript data.
*/
export
const rendererFactory: IRenderMime.IRendererFactory = {
safe: true,
mimeTypes: [TEXT_JAVASCRIPT_MIMETYPE, APPLICATION_JAVASCRIPT_MIMETYPE],
createRenderer: options => new BeakerxRenderedJavascript(options)
};

const extension: IRenderMime.IExtension = {
id: 'beakerx.javascript:factory',
rendererFactory,
rank: 0,
dataType: 'string'
};

export default extension;
19 changes: 19 additions & 0 deletions doc/README.md
@@ -0,0 +1,19 @@
<!--
Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# Documentation

[Start Here](../StartHere.ipynb)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions docker/setup.sh
Expand Up @@ -16,6 +16,8 @@
source activate beakerx
(cd beakerx; pip install -e . --verbose)
beakerx-install
jupyter labextension install @jupyter-widgets/jupyterlab-manager
(cd js/lab; jupyter labextension install .)

rm -r /home/beakerx/beakerx/js/node_modules
rm -r /home/beakerx/beakerx/js/dist
Expand Down
1 change: 1 addition & 0 deletions js/lab/.gitignore
@@ -1,2 +1,3 @@
/lib
/dist
node_modules/
30 changes: 28 additions & 2 deletions js/lab/package.json
Expand Up @@ -3,7 +3,7 @@
"version": "0.9.1",
"description": "BeakerX: Beaker Extensions for JupyterLab",
"author": "Two Sigma Open Source, LLC",
"main": "index.js",
"main": "dist/index.js",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand All @@ -15,9 +15,35 @@
"jupyterlab extension"
],
"scripts": {
"build": "npm run build:src",
"build:src": "tsc --project src",
"prepublish": "npm run build:src",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"@jupyter-widgets/base": "^1.1.5",
"@jupyter-widgets/controls": "^1.1.2",
"@jupyter-widgets/output": "^1.0.10",
"@jupyterlab/application": "^0.13.0",
"@jupyterlab/coreutils": "^0.13.0",
"@jupyterlab/docregistry": "^0.13.0",
"@jupyterlab/notebook": "^0.13.0",
"@jupyterlab/outputarea": "^0.13.1",
"@jupyterlab/rendermime": "^0.13.0",
"@jupyterlab/rendermime-interfaces": "^0.4.2",
"@jupyterlab/services": "^0.52.0",
"@types/node": "^8.0.56",
"typescript": "~2.6.2"
},
"jupyterlab": {
"extension": true
"extension": "dist/index.js",
"mimeExtension": "dist/javascriptRendererExtension.js"
},
"files": [
"dist/*.js",
"lib/*.js"
],
"dependencies": {
"beakerx": "^0.9.1"
}
}
17 changes: 12 additions & 5 deletions js/lab/index.js → js/lab/src/index.ts
Expand Up @@ -14,18 +14,25 @@
* limitations under the License.
*/

var beakerx = require('./lib/index.js');
var base = require('@jupyter-widgets/base');
declare function require(moduleName: string): any;

module.exports = {
import { IJupyterWidgetRegistry } from '@jupyter-widgets/base';
import BeakerxExtension from './plugin';
import { JupyterLab } from '@jupyterlab/application';

const beakerx = require('../lib/index.js');

export default {
id: 'beakerx',
requires: [base.IJupyterWidgetRegistry],
activate: function(app, widgets) {
requires: [IJupyterWidgetRegistry],
activate: (app: JupyterLab, widgets: IJupyterWidgetRegistry ) => {
widgets.registerWidget({
name: 'beakerx',
version: beakerx.version,
exports: beakerx
});

app.docRegistry.addWidgetExtension('Notebook', new BeakerxExtension());
},
autoStart: true
};
56 changes: 56 additions & 0 deletions js/lab/src/javascriptRendererExtension.ts
@@ -0,0 +1,56 @@
/*
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { RenderedJavaScript } from '@jupyterlab/rendermime'

export const TEXT_JAVASCRIPT_MIMETYPE = 'text/javascript';
export const APPLICATION_JAVASCRIPT_MIMETYPE = 'application/javascript';

export class BeakerxRenderedJavascript extends RenderedJavaScript {
render(model: IRenderMime.IMimeModel): Promise<void> {
const evalInContext = function(code: string) {
return eval(code);
}.bind(this);

try {
evalInContext(String(model.data[this.mimeType]));
} catch (e) {
this.node.innerHTML = `<pre>${String(e.message)} - please check console</pre>`;
}

return Promise.resolve(undefined);
}
}

/**
* A mime renderer factory for text/javascript data.
*/
export
const rendererFactory: IRenderMime.IRendererFactory = {
safe: true,
mimeTypes: [TEXT_JAVASCRIPT_MIMETYPE, APPLICATION_JAVASCRIPT_MIMETYPE],
createRenderer: options => new BeakerxRenderedJavascript(options)
};

const extension: IRenderMime.IExtension = {
id: 'beakerx.javascript:factory',
rendererFactory,
rank: 0,
dataType: 'string'
};

export default extension;
143 changes: 143 additions & 0 deletions js/lab/src/plugin.ts
@@ -0,0 +1,143 @@
/*
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { JSONArray } from '@phosphor/coreutils';
import { Widget } from '@phosphor/widgets';
import { DisposableDelegate } from '@phosphor/disposable';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { INotebookModel, NotebookPanel, Notebook } from '@jupyterlab/notebook';
import { Cell, CodeCell, CodeCellModel } from '@jupyterlab/cells';
import { showDialog, Dialog } from '@jupyterlab/apputils';
import { Kernel } from '@jupyterlab/services';

interface msgData {
name?: string,
value?: any
}

declare global {
interface Window {
beakerx: any,
require: Function
}
}

function displayHTML(widget: Widget, html: string): void {
if (!widget.node || !html) {
return;
}

const childElement = document.createElement('pre');

childElement.classList.add('jp-RenderedHTML');
childElement.innerHTML = html;
widget.node.appendChild(childElement);
}

function registerGlobal(): void {
window.beakerx = window.beakerx || {};
window.beakerx.displayHTML = displayHTML;
}

function sendJupyterCodeCells(
kernelInstance: Kernel.IKernelConnection,
notebook: Notebook,
comm: Kernel.IComm,
filter: string
): void {
const codeCells = <JSONArray>getCodeCellsByTag(notebook, filter).map(
(cell: CodeCell): object => cell.model.toJSON()
);

comm.send({ code_cells: codeCells });
}

function getCodeCellsByTag(notebook: Notebook, tag: string): Cell[] {
let cells = notebook.widgets || [];

return cells.filter(function(cell) {
const tags: any = cell.model.metadata.get('tags');

return (
cell.model instanceof CodeCellModel &&
tags && tags.length && tags.includes(tag)
);
});
}

function registerCommTargets(panel: NotebookPanel, context: DocumentRegistry.IContext<INotebookModel>) {
const session = context.session;
const kernelInstance = session.kernel;
const notebook = panel.notebook;

kernelInstance.registerCommTarget('beaker.getcodecells', function(comm, msg) {
const data: msgData = <object>msg.content.data;

comm.onMsg = function(msg) {
if(data.name == "CodeCells") {
sendJupyterCodeCells(kernelInstance, notebook, comm, JSON.parse(data.value));
}

window.beakerx[data.name] = JSON.parse(data.value);
};
});

kernelInstance.registerCommTarget('beaker.autotranslation', function(comm, msg) {
comm.onMsg = function(msg) {
const data: msgData = <object>msg.content.data;

window.beakerx[data.name] = JSON.parse(data.value);
};
});

kernelInstance.registerCommTarget('beaker.tag.run', function(comm, msg) {
comm.onMsg = function(msg) {
const data: { state?: any } = <object>msg.content.data;

if(!data.state || !data.state.runByTag) {
return;
}

const matchedCells = getCodeCellsByTag(notebook, data.state.runByTag);

if (matchedCells.length === 0) {
showDialog({
title: 'No cell with the tag !',
body: 'Tag: ' + data.state.runByTag,
buttons: [ Dialog.okButton({ label: 'OK' }) ]
});
} else {
matchedCells.forEach((cell) => {
cell instanceof CodeCell && CodeCell.execute(cell, session);
});
}
};
});
}

class BeakerxExtension implements DocumentRegistry.WidgetExtension {
createNew(panel: NotebookPanel, context: DocumentRegistry.IContext<INotebookModel>) {
registerGlobal();

Promise.all([panel.ready, context.ready]).then(function() {
registerCommTargets(panel, context);
});

return new DisposableDelegate(() => { });
}
}

export default BeakerxExtension;
14 changes: 14 additions & 0 deletions js/lab/src/tsconfig.json
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"allowJs": true,
"noImplicitAny": true,
"noEmitOnError": true,
"noUnusedLocals": true,
"lib": ["dom", "es5", "es2015.promise"],
"types": ["node"],
"module": "commonjs",
"moduleResolution": "node",
"target": "ES5",
"outDir": "../dist"
}
}

0 comments on commit 5977051

Please sign in to comment.