Skip to content
Merged
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
1 change: 1 addition & 0 deletions ipython_widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .widgets import *
21 changes: 21 additions & 0 deletions ipython_widgets/install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
# Thanks @takluyver for your cite2c install.py.
from os.path import dirname, abspath, join as pjoin
from jupyter_notebook.nbextensions import install_nbextension
from jupyter_notebook.services.config import ConfigManager
import argparse

parser = argparse.ArgumentParser(description="Installs the IPython widgets")
parser.add_argument("-u", "--user", help="Install as current user", action="store_true")
parser.add_argument("-s", "--symlink", help="Symlink instead of copying files", action="store_true")
args = parser.parse_args()

print("Installing nbextension ...")
staticdir = pjoin(dirname(abspath(__file__)), 'static')
install_nbextension(staticdir, destination='widgets', user=args.user, symlink=args.symlink)

print("Enabling the extension ...")
cm = ConfigManager()
cm.update('notebook', {"load_extensions": {"widgets/notebook/js/extension": True}})

print("Done.")
66 changes: 66 additions & 0 deletions ipython_widgets/static/notebook/js/extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

define([
'nbextensions/widgets/widgets/js/init',
'nbextensions/widgets/notebook/js/widgetarea',
'base/js/events'
], function(widgetmanager, widgetarea, events) {
"use strict";

/**
* Create a widget manager for a kernel instance.
*/
var handle_kernel = function(kernel) {
if (kernel.comm_manager && kernel.widget_manager === undefined) {

// Create a widget manager instance. Use the global
// IPython.notebook handle.
var manager = new widgetmanager.WidgetManager(kernel.comm_manager, IPython.notebook);

// Store a handle to the manager so we know not to
// another for this kernel. This also is a convinience
// for the user.
kernel.widget_manager = manager;
}
};

// If a kernel already exists, create a widget manager.
if (IPython.notebook && IPython.notebook.kernel) {
handle_kernel(IPython.notebook.kernel);
}
// When the kernel is created, create a widget manager.
events.on('kernel_created.Kernel kernel_created.Session', function(event, data) {
handle_kernel(data.kernel);
});

/**
* Creates a widgetarea for the cell if it is a CodeCell.
* If the cell isn't a CodeCell, no action is taken.
*/
var handle_cell = function(cell) {
if (cell.cell_type==='code') {
var area = new widgetarea.WidgetArea(cell);
cell.widgetarea = area;
}
};

// Create widget areas for cells that already exist.
var cells = IPython.notebook.get_cells();
for (var i = 0; i < cells.length; i++) {
handle_cell(cells[i]);
}

// Listen to cell creation and deletion events. When a
// cell is created, create a widget area for that cell.
events.on('create.Cell', function(event, data) {
handle_cell(data.cell);
});
// When a cell is deleted, delete the widget area if it
// exists.
events.on('delete.Cell', function(event, data) {
if (data.cell && data.cell.widgetarea) {
data.cell.widgetarea.dispose();
}
});
});
163 changes: 163 additions & 0 deletions ipython_widgets/static/notebook/js/widgetarea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

define(['jquery', 'base/js/events'], function($, events) {

/**
* WidgetArea
*/
var WidgetArea = function(cell) {
this.widget_views = [];

this._cell = cell;
this._widgets_live = true;
this.disposed = false;

this._create_elements();
this._bind_events();
}

/**
* Display a widget view in the cell.
*/
WidgetArea.prototype.display_widget_view = function(view_promise) {

// Display a dummy element
var dummy = $('<div/>');
this.widget_subarea.append(dummy);

// Display the view.
var that = this;
return view_promise.then(function(view) {
that.widget_area.show();
dummy.replaceWith(view.$el);
that.widget_views.push(view);

// Check the live state of the view's model.
if (view.model.comm_live) {
that._widget_live(view);
} else {
that._widget_dead(view);
}

// Listen to comm live events for the view.
view.on('comm:live', that._widget_live, that);
view.on('comm:dead', that._widget_dead, that);
return view;
});
};

/**
* Disposes of the widget area and its widgets.
*/
WidgetArea.prototype.dispose = function() {
this._clear();
this.disposed = true;
};

/**
* Creates the elements of the widget area and appends them
* to the associated cell.
*/
WidgetArea.prototype._create_elements = function() {
var widget_area = $('<div/>')
.addClass('widget-area')
.hide();
this.widget_area = widget_area;
var widget_prompt = $('<div/>')
.addClass('prompt')
.appendTo(widget_area);
var widget_subarea = $('<div/>')
.addClass('widget-subarea')
.appendTo(widget_area);
this.widget_subarea = widget_subarea;
var that = this;
var widget_clear_buton = $('<button />')
.addClass('close')
.html('&times;')
.click(function() {
widget_area.slideUp('', function(){
for (var i = 0; i < that.widget_views.length; i++) {
var view = that.widget_views[i];
view.remove();

// Remove widget live events.
view.off('comm:live', that._widget_live);
view.off('comm:dead', that._widget_dead);
}
that.widget_views = [];
widget_subarea.html('');
});
})
.appendTo(widget_prompt);

if (this._cell.input) {
this._cell.input.after(widget_area);
} else {
throw new Error('Cell does not have an `input` element. Is it not a CodeCell?');
}
}

/**
* Listens to events of the cell.
*/
WidgetArea.prototype._bind_events = function() {
var that = this;
events.on('execute.CodeCell', function(event, data) {
if (data.cell===that._cell) {
that._clear();
}
});
};

/**
* Handles when a widget loses it's comm connection.
* @param {WidgetView} view
*/
WidgetArea.prototype._widget_dead = function(view) {
if (this._widgets_live) {
this._widgets_live = false;
this.widget_area.addClass('connection-problems');
}

};

/**
* Handles when a widget is connected to a live comm.
* @param {WidgetView} view
*/
WidgetArea.prototype._widget_live = function(view) {
if (!this._widgets_live) {
// Check that the other widgets are live too. O(N) operation.
// Abort the function at the first dead widget found.
for (var i = 0; i < this.widget_views.length; i++) {
if (!this.widget_views[i].model.comm_live) return;
}
this._widgets_live = true;
this.widget_area.removeClass('connection-problems');
}
};

/**
* Clears the widgets in the widget area.
*/
WidgetArea.prototype._clear = function() {
// Clear widget area
for (var i = 0; i < this.widget_views.length; i++) {
var view = this.widget_views[i];
view.remove();

// Remove widget live events.
view.off('comm:live', this._widget_live);
view.off('comm:dead', this._widget_dead);
}
this.widget_views = [];
this.widget_subarea.html('');
this.widget_subarea.height('');
this.widget_area.height('');
this.widget_area.hide();
};

return {WidgetArea: WidgetArea};
});

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/manager",
"widgets/js/widget",
"widgets/js/widget_link",
"widgets/js/widget_bool",
"widgets/js/widget_button",
"widgets/js/widget_box",
"widgets/js/widget_float",
"widgets/js/widget_image",
"widgets/js/widget_int",
"widgets/js/widget_output",
"widgets/js/widget_selection",
"widgets/js/widget_selectioncontainer",
"widgets/js/widget_string",
"nbextensions/widgets/widgets/js/manager",
"nbextensions/widgets/widgets/js/widget",
"nbextensions/widgets/widgets/js/widget_link",
"nbextensions/widgets/widgets/js/widget_bool",
"nbextensions/widgets/widgets/js/widget_button",
"nbextensions/widgets/widgets/js/widget_box",
"nbextensions/widgets/widgets/js/widget_float",
"nbextensions/widgets/widgets/js/widget_image",
"nbextensions/widgets/widgets/js/widget_int",
"nbextensions/widgets/widgets/js/widget_output",
"nbextensions/widgets/widgets/js/widget_selection",
"nbextensions/widgets/widgets/js/widget_selectioncontainer",
"nbextensions/widgets/widgets/js/widget_string",
], function(widgetmanager, widget) {
// Register all of the loaded models and views with the widget manager.
for (var i = 2; i < arguments.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ define([

WidgetManager.prototype.display_view_in_cell = function(cell, model) {
// Displays a view in a cell.
if (cell.display_widget_view) {
if (cell.widgetarea) {
var that = this;
return cell.display_widget_view(this.create_view(model, {
return cell.widgetarea.display_widget_view(this.create_view(model, {
cell: cell,
// Only set cell_index when view is displayed as directly.
cell_index: that.notebook.find_cell_index(cell),
Expand All @@ -152,7 +152,7 @@ define([
return view;
}).catch(utils.reject('Could not create or display view', true));
} else {
return Promise.reject(new Error('Cell does not have a `display_widget_view` method'));
return Promise.reject(new Error('Cell does not have a `widgetarea` defined'));
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

define(["widgets/js/manager",
define(["nbextensions/widgets/widgets/js/manager",
"underscore",
"backbone",
"jquery",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/widget",
"nbextensions/widgets/widgets/js/widget",
"jquery",
"bootstrap",
], function(widget, $){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/widget",
"nbextensions/widgets/widgets/js/widget",
"jqueryui",
"underscore",
"base/js/utils",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/widget",
"nbextensions/widgets/widgets/js/widget",
"jquery",
"bootstrap",
], function(widget, $){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/widget",
"widgets/js/widget_int",
"nbextensions/widgets/widgets/js/widget",
"nbextensions/widgets/widgets/js/widget_int",
], function(widget, int_widgets){
var IntSliderView = int_widgets.IntSliderView;
var IntTextView = int_widgets.IntTextView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/widget",
"nbextensions/widgets/widgets/js/widget",
"jquery",
], function(widget, $){

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/widget",
"nbextensions/widgets/widgets/js/widget",
"jqueryui",
"base/js/keyboard",
"bootstrap"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/widget",
"nbextensions/widgets/widgets/js/widget",
"jquery",
], function(widget, $){
var LinkModel = widget.WidgetModel.extend({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

define([
"widgets/js/widget",
"nbextensions/widgets/widgets/js/widget",
"jquery",
'notebook/js/outputarea',
], function(widget, $, outputarea) {
Expand Down
Loading