Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

[WIP] Changes necessary to work standalone similar to ipywidgets examples #157

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .npmignore
Empty file.
4 changes: 2 additions & 2 deletions elements/urth-core-behaviors/execution-complete-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
console.debug("Registering _onExecutionComplete for Execute Reply messages.");
this.__replyCallback = this._onExecutionComplete.bind(this);

IPython.notebook.kernel.widget_manager.notebook.events.on(
IPython.notebook.events.on(
'shell_reply.Kernel', this.__replyCallback
);
},

detached: function() {
console.debug("Unregistering _onExecutionComplete for Execute Reply messages.");
if (this.__replyCallback){
IPython.notebook.kernel.widget_manager.notebook.events.off(
IPython.notebook.events.off(
'shell_reply.Kernel', this.__replyCallback
);
}
Expand Down
2 changes: 1 addition & 1 deletion elements/urth-core-behaviors/jupyter-kernel-observer.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'kernel_ready.Kernel', this.__kernelReadyCallback
);

if( IPython.notebook.kernel.is_connected() ){
if( IPython.notebook.kernel && IPython.notebook.kernel.is_connected() ){
this.onKernelReady();
}
},
Expand Down
2 changes: 2 additions & 0 deletions etc/examples/standalone/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
index.built.js
es5/
3 changes: 3 additions & 0 deletions etc/examples/standalone/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esnext": true
}
29 changes: 29 additions & 0 deletions etc/examples/standalone/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

.PHONY: help clean build run

clean: clean-npm clean-build

clean-npm:
-@rm -r node_modules

clean-build:
-@rm -r es5
-@rm index.built.js

build-ipywidgets:
@echo 'Building ipywidgets'
cd ../../../../ipywidgets/ipywidgets && npm install

build-urth-widgets:
@echo 'Building urth-widgets'
cd ../../.. && make dist NOSCALA=true && npm install

build: build-urth-widgets build-ipywidgets
@echo 'Building example'
npm install
-cd node_modules/urth-widgets/dist/urth_widgets && ln -s bower_components/ urth_components

run:
python -m SimpleHTTPServer 8000
11 changes: 11 additions & 0 deletions etc/examples/standalone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Using urth-widgets in non-notebook, web context
## Description
This directory is an example project that shows how you can embed the widgets in
a context other than the notebook.

## Try it
This assumes that ipywidgets is cloned out at the same level as declarativewidgets
In this directory:

1. `make build`
2. `make run`
51 changes: 51 additions & 0 deletions etc/examples/standalone/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<html>
<head>
<script src="index.built.js"></script>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<link rel="stylesheet" type="text/css" href="./node_modules/font-awesome/css/font-awesome.css">
<style>
body {
margin-left: auto;
margin-right: auto;
max-width: 900px;
background-color: #eee;
}

.jupyter-js-widgets-example {
margin-top: 10px;
margin-bottom: 10px;
background-color: white;
overflow: auto;
box-shadow: 0px 0px 12px 1px rgba(87, 87, 87, 0.2);
}

.widgetarea, .inputarea {
margin: 5px;
}

.CodeMirror {
border: 1px solid #ccc;
height: auto;
background-color: #f7f7f7;
border-radius: 2px;
}

.CodeMirror-Scroll {
height: auto;
}
</style>
</head>
<body>
<div class="jupyter-js-widgets-example">
<div class="inputarea"></div>
<div class="widgetarea"></div>
<template is='urth-core-bind' channel='a'>
<template is='dom-if' if='{{kernelready}}'>
<urth-core-function ref="hello" arg-name="{{name}}" result="{{msg}}" auto></urth-core-function>
name: <input type="text" value="{{name::input}}"></input>
</template>
<H1>{{msg}}</H1>
</template>
</div>
</body>
</html>
31 changes: 31 additions & 0 deletions etc/examples/standalone/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "urth-standalone-example",
"version": "1.0.0",
"description": "Example of urth-widgets running in a standalone web app using jupyter-js-services",
"main": "index.js",
"scripts": {
"prepublish": "npm run build",
"build": "npm run build:es5 && npm run build:bundle",
"build:es5": "babel src --out-dir es5 --presets es2015",
"build:bundle": "browserify -g node-lessify ./es5/index.js --outfile ./index.built.js --debug",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "IPython",
"license": "BSD-3-Clause",
"dependencies": {
"browserify": "^11.2.0",
"codemirror": "^5.9.0",
"font-awesome": "^4.5.0",
"jupyter-js-widgets": "file:../../../../ipywidgets/ipywidgets",
"urth-widgets": "file:../../../../declarativewidgets",
"jquery": "^2.1.4",
"jquery-ui": "^1.10.5",
"jupyter-js-services": "^0.2.2",
"node-lessify": "^0.1.1"
},
"devDependencies": {
"babel-cli": "^6.3.15",
"babel-preset-es2015": "^6.3.13",
"bower": "^1.7.0"
}
}
61 changes: 61 additions & 0 deletions etc/examples/standalone/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var CodeMirror = require("codemirror");
require("../node_modules/codemirror/lib/codemirror.css");
require("../node_modules/codemirror/mode/python/python");

var WidgetManager = require("./manager").WidgetManager;

var urth_widgets = require('urth-widgets');



var services = require('jupyter-js-services');
var getKernelSpecs = services.getKernelSpecs;
var startNewKernel = services.startNewKernel;

var BASEURL = prompt('Notebook BASEURL', 'http://192.168.99.100:8888');

var WSURL = 'ws:' + BASEURL.split(':').slice(1).join(':');

urth_widgets.init('./node_modules/urth-widgets/dist/urth_widgets/');

document.addEventListener("DOMContentLoaded", function(event) {

// Connect to the notebook webserver.
getKernelSpecs(BASEURL).then(kernelSpecs => {
return startNewKernel({
baseUrl: BASEURL,
wsUrl: WSURL,
name: kernelSpecs.default,
});
}).then(kernel => {

// Create a codemirror instance
var code = require("../widget_code.json").join("\n");
var inputarea = document.getElementsByClassName("inputarea")[0];
var editor = CodeMirror(inputarea, {
value: code,
mode: "python",
tabSize: 4,
showCursorWhenSelecting: true,
viewportMargin: Infinity,
readOnly: true
});

// Create the widget area and widget manager
var widgetarea = document.getElementsByClassName("widgetarea")[0];
var manager = new WidgetManager(kernel, widgetarea);

//more shimming
IPython.notebook.kernel = kernel;
IPython.notebook.kernel.widget_manager = manager;
IPython.notebook.events.trigger('kernel_ready.Kernel');

// Run backend code to create the widgets. You could also create the
// widgets in the frontend, like the other /web/ examples demonstrate.
var execPromise = kernel.execute({ code: code });
execPromise.onReply = function(){
IPython.notebook.events.trigger('shell_reply.Kernel');
}

});
});
37 changes: 37 additions & 0 deletions etc/examples/standalone/src/manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require('../node_modules/jupyter-js-widgets/static/components/bootstrap/css/bootstrap.css');
require('../node_modules/jquery-ui/themes/smoothness/jquery-ui.min.css');

const ipywidgets = require('jupyter-js-widgets');

export class WidgetManager extends ipywidgets.ManagerBase {
constructor(kernel, el) {
super();
this.kernel = kernel;
this.el = el;

// Create a comm manager shim
this.commManager = new ipywidgets.shims.services.CommManager(kernel);

// Register the comm target
this.commManager.register_target(this.comm_target_name, this.handle_comm_open.bind(this));
}

display_view(msg, view, options) {
var that = this;
return Promise.resolve(view).then(function(view) {
that.el.appendChild(view.el);
view.on('remove', function() {
console.log('view removed', view);
});
return view;
});
}

_create_comm(targetName, id, metadata) {
return Promise.resolve( this.commManager.new_comm(targetName, metadata, id) );
}

_get_comm_info() {
return Promise.resolve({});
}
}
8 changes: 8 additions & 0 deletions etc/examples/standalone/widget_code.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
"from urth.widgets.widget_channels import channel",
"def hello(name):",
" return 'Hello ' + name",
"channel('a').set('kernelready', True)"
]


39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

// This is the main file that will be loaded when urth-widgets is required in an
// npm environment.
//
//module.exports = function createDefine(targetModule) {
// var amdefine = require('amdefine')(targetModule, require);
//
// return function define() {
// var args = Array.prototype.slice.call(arguments);
// //if (args.length > 1) {
// // args[0] = args[0].map(function(arg) {
// // if (arg === 'jqueryui') arg = 'jquery';
// // arg = arg.replace('nbextensions/widgets/widgets/css/', '../css/');
// // arg = arg.replace('nbextensions/widgets/components/require-css/css!', '');
// // return arg;
// // });
// //}
// amdefine.apply(this, args);
// };
//};

global.jQuery = global.$ = require('jquery');
var init = require('./dist/urth_widgets/js/init/init');

exports.init = init;

var Events = function () {};

var events = new Events();


//shim in IPython
IPython = {
notebook: {
events: $([events])
}
}
8 changes: 8 additions & 0 deletions nb-extension/js/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
* Loads the web component polyfill and all web components specified in
* elements.html.
*/
// npm compatibility
function requireLocalFiles() {
require('jquery');
require('../widgets/DeclWidgetModel');
}

if (typeof define !== 'function') { var define = require('amdefine')(module, require); }

define([
'require',
'jquery',
Expand Down
32 changes: 28 additions & 4 deletions nb-extension/js/widgets/DeclWidgetModel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

// npm compatibility
function requireLocalFiles() {
require('../../../../../../node_modules/jupyter-js-widgets/static/widgets/js/manager-base');
require('../../../../../../node_modules/jupyter-js-widgets/static/widgets/js/widget');
}

if (typeof define !== 'function') {
var amdefine = require('amdefine')(module, require);

var mapping = {}
mapping['nbextensions/widgets/widgets/js/manager'] = '../../../../../../node_modules/jupyter-js-widgets/static/widgets/js/manager-base'
mapping['nbextensions/widgets/widgets/js/widget'] = '../../../../../../node_modules/jupyter-js-widgets/static/widgets/js/widget'

var define = function(){
var args = Array.prototype.slice.call(arguments);
if (args.length > 1) {
args[0] = args[0].map(function(arg) {
arg = mapping[arg] || arg;
return arg;
});
}
amdefine.apply(this, args);
}
}

define(["nbextensions/widgets/widgets/js/manager",
"nbextensions/widgets/widgets/js/widget",
"base/js/namespace"
], function(widgetmanager, ipywidget, IPython) {
"nbextensions/widgets/widgets/js/widget"
], function(widgetmanager, ipywidget) {
"use strict";

/**
Expand Down Expand Up @@ -33,7 +57,7 @@ define(["nbextensions/widgets/widgets/js/manager",
this.comm.send(data, callbacks);
};

widgetmanager.WidgetManager.register_widget_model('DeclWidgetModel', DeclWidgetModel);
widgetmanager.ManagerBase.register_widget_model('DeclWidgetModel', DeclWidgetModel);

return DeclWidgetModel;

Expand Down
2 changes: 1 addition & 1 deletion notebooks/examples/urth-scala-widgets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"outputs": [],
"source": [
"// modify to IP and Port of this notebook server\n",
"%addjar http://192.168.99.100:8888/nbextensions/urth_widgets/urth-widgets.jar"
"%addjar http://192.168.99.100:8888/nbexten tsions/urth_widgets/urth-widgets.jar"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

que?

]
},
{
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"selenium-standalone": "latest",
"web-component-tester": "~3.4"
},
"dependencies": {
"amdefine": "^1.0.0",
"jquery": "^2.1.4",
"jupyter-js-widgets": "file:../ipywidgets/ipywidgets"
},
"config": {},
"scripts": {
"bower": "bower",
Expand Down