{ "metadata": { "name": "direct_view_widget" }, "nbformat": 3, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "source": [ "Direct View Widget" ] }, { "cell_type": "markdown", "source": [ "IPython has a JavaScript widget for interacting with an IPython parallel engine interactively in the Notebook. This Notebook shows how this widget can be used." ] }, { "cell_type": "code", "input": [ "from IPython.frontend.html.notebook.widgets import directview", "from IPython.parallel import Client" ], "language": "python", "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "source": [ "Let's create a `Client` and build a `DirectView` containing all of the engines:" ] }, { "cell_type": "code", "input": [ "c = Client()", "dv = c[:]" ], "language": "python", "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "source": [ "To interact with the engines we simply pass the `DirectView` instance to the `interact` function. The resulting widget has an embedded notebook cell, but any code entered into the embedded cell is run on the engines you choose. The engines are now full blown IPython kernels so you can enter arbitrary Python/IPython code and even make plots on the engines." ] }, { "cell_type": "code", "input": [ "directview.interact(dv)" ], "language": "python", "outputs": [ { "javascript": [ "//----------------------------------------------------------------------------", "// Copyright (C) 2008-2012 The IPython Development Team", "//", "// Distributed under the terms of the BSD License. The full license is in", "// the file COPYING, distributed as part of this software.", "//----------------------------------------------------------------------------", "", "//============================================================================", "// EngineInteract", "//============================================================================", "", "var key = IPython.utils.keycodes;", "", "", "var DirectViewWidget = function (selector, kernel, targets) {", " // The kernel doesn't have to be set at creation time, in that case", " // it will be null and set_kernel has to be called later.", " this.selector = selector;", " this.element = $(selector);", " this.kernel = kernel || null;", " this.code_mirror = null;", " this.targets = targets;", " this.create_element();", "};", "", "", "DirectViewWidget.prototype.create_element = function () {", " this.element.addClass('cell border-box-sizing code_cell vbox');", " this.element.attr('tabindex','2');", " this.element.css('padding-right',0);", "", " var control = $('
').addClass('dv_control').height('30px');", " var control_label = $('').html('Select engine(s) to run code on interactively: ');", " control_label.css('line-height','30px');", " var select = $('').addClass('dv_select ui-widget ui-widget-content');", " select.css('font-size','85%').css('margin-bottom','5px');", " var n = this.targets.length;", " select.append($('').html('all').attr('value','all'));", " for (var i=0; i