Moves jupyter extension to both bonobo.contrib.jupyter (for the jupyter widget) and to bonobo.plugins (for the executor-side plugin).
This commit is contained in:
@ -74,7 +74,7 @@ def run(graph, *, plugins=None, services=None, strategy=None):
|
||||
|
||||
if _is_jupyter_notebook():
|
||||
try:
|
||||
from bonobo.ext.jupyter import JupyterOutputPlugin
|
||||
from bonobo.contrib.jupyter import JupyterOutputPlugin
|
||||
except ImportError:
|
||||
import logging
|
||||
logging.warning(
|
||||
|
||||
15
bonobo/contrib/jupyter/__init__.py
Normal file
15
bonobo/contrib/jupyter/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
from bonobo.plugins.jupyter import JupyterOutputPlugin
|
||||
|
||||
|
||||
def _jupyter_nbextension_paths():
|
||||
return [{
|
||||
'section': 'notebook',
|
||||
'src': 'static',
|
||||
'dest': 'bonobo-jupyter',
|
||||
'require': 'bonobo-jupyter/extension'
|
||||
}]
|
||||
|
||||
|
||||
__all__ = [
|
||||
'JupyterOutputPlugin',
|
||||
]
|
||||
1
bonobo/contrib/jupyter/js/.gitignore
vendored
Normal file
1
bonobo/contrib/jupyter/js/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/node_modules
|
||||
19
bonobo/contrib/jupyter/js/README.rst
Normal file
19
bonobo/contrib/jupyter/js/README.rst
Normal file
@ -0,0 +1,19 @@
|
||||
Bonobo within Jupyter
|
||||
=====================
|
||||
|
||||
Install
|
||||
-------
|
||||
|
||||
.. code-block:: shell-session
|
||||
|
||||
yarn install
|
||||
|
||||
|
||||
Watch mode (for development)
|
||||
----------------------------
|
||||
|
||||
.. code-block:: shell-session
|
||||
|
||||
yarn run webpack --watch
|
||||
|
||||
|
||||
@ -69,7 +69,7 @@ define(["jupyter-js-widgets"], function(__WEBPACK_EXTERNAL_MODULE_2__) { return
|
||||
// When serialiazing entire widget state for embedding, only values different from the
|
||||
// defaults will be specified.
|
||||
|
||||
var BonoboModel = widgets.DOMWidgetModel.extend({
|
||||
const BonoboModel = widgets.DOMWidgetModel.extend({
|
||||
defaults: _.extend({}, widgets.DOMWidgetModel.prototype.defaults, {
|
||||
_model_name: 'BonoboModel',
|
||||
_view_name: 'BonoboView',
|
||||
@ -81,7 +81,7 @@ define(["jupyter-js-widgets"], function(__WEBPACK_EXTERNAL_MODULE_2__) { return
|
||||
|
||||
|
||||
// Custom View. Renders the widget model.
|
||||
var BonoboView = widgets.DOMWidgetView.extend({
|
||||
const BonoboView = widgets.DOMWidgetView.extend({
|
||||
render: function () {
|
||||
this.value_changed();
|
||||
this.model.on('change:value', this.value_changed, this);
|
||||
@ -89,7 +89,9 @@ define(["jupyter-js-widgets"], function(__WEBPACK_EXTERNAL_MODULE_2__) { return
|
||||
|
||||
value_changed: function () {
|
||||
this.$el.html(
|
||||
this.model.get('value').join('<br>')
|
||||
'<div class="rendered_html"><table style="margin: 0; border: 1px solid black;">' + this.model.get('value').map((key, i) => {
|
||||
return `<tr><td>${key.status}</td><td>${key.name}</td><td>${key.stats}</td><td>${key.flags}</td></tr>`
|
||||
}).join('\n') + '</table></div>'
|
||||
);
|
||||
},
|
||||
});
|
||||
1
bonobo/contrib/jupyter/js/dist/index.js.map
vendored
Normal file
1
bonobo/contrib/jupyter/js/dist/index.js.map
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -8,7 +8,7 @@ var _ = require('underscore');
|
||||
// When serialiazing entire widget state for embedding, only values different from the
|
||||
// defaults will be specified.
|
||||
|
||||
var BonoboModel = widgets.DOMWidgetModel.extend({
|
||||
const BonoboModel = widgets.DOMWidgetModel.extend({
|
||||
defaults: _.extend({}, widgets.DOMWidgetModel.prototype.defaults, {
|
||||
_model_name: 'BonoboModel',
|
||||
_view_name: 'BonoboView',
|
||||
@ -20,7 +20,7 @@ var BonoboModel = widgets.DOMWidgetModel.extend({
|
||||
|
||||
|
||||
// Custom View. Renders the widget model.
|
||||
var BonoboView = widgets.DOMWidgetView.extend({
|
||||
const BonoboView = widgets.DOMWidgetView.extend({
|
||||
render: function () {
|
||||
this.value_changed();
|
||||
this.model.on('change:value', this.value_changed, this);
|
||||
@ -28,7 +28,9 @@ var BonoboView = widgets.DOMWidgetView.extend({
|
||||
|
||||
value_changed: function () {
|
||||
this.$el.html(
|
||||
this.model.get('value').join('<br>')
|
||||
'<div class="rendered_html"><table style="margin: 0; border: 1px solid black;">' + this.model.get('value').map((key, i) => {
|
||||
return `<tr><td>${key.status}</td><td>${key.name}</td><td>${key.stats}</td><td>${key.flags}</td></tr>`
|
||||
}).join('\n') + '</table></div>'
|
||||
);
|
||||
},
|
||||
});
|
||||
@ -72,7 +72,7 @@ define(["jupyter-js-widgets"], function(__WEBPACK_EXTERNAL_MODULE_2__) { return
|
||||
// When serialiazing entire widget state for embedding, only values different from the
|
||||
// defaults will be specified.
|
||||
|
||||
var BonoboModel = widgets.DOMWidgetModel.extend({
|
||||
const BonoboModel = widgets.DOMWidgetModel.extend({
|
||||
defaults: _.extend({}, widgets.DOMWidgetModel.prototype.defaults, {
|
||||
_model_name: 'BonoboModel',
|
||||
_view_name: 'BonoboView',
|
||||
@ -84,7 +84,7 @@ define(["jupyter-js-widgets"], function(__WEBPACK_EXTERNAL_MODULE_2__) { return
|
||||
|
||||
|
||||
// Custom View. Renders the widget model.
|
||||
var BonoboView = widgets.DOMWidgetView.extend({
|
||||
const BonoboView = widgets.DOMWidgetView.extend({
|
||||
render: function () {
|
||||
this.value_changed();
|
||||
this.model.on('change:value', this.value_changed, this);
|
||||
@ -92,7 +92,9 @@ define(["jupyter-js-widgets"], function(__WEBPACK_EXTERNAL_MODULE_2__) { return
|
||||
|
||||
value_changed: function () {
|
||||
this.$el.html(
|
||||
this.model.get('value').join('<br>')
|
||||
'<div class="rendered_html"><table style="margin: 0; border: 1px solid black;">' + this.model.get('value').map((key, i) => {
|
||||
return `<tr><td>${key.status}</td><td>${key.name}</td><td>${key.stats}</td><td>${key.flags}</td></tr>`
|
||||
}).join('\n') + '</table></div>'
|
||||
);
|
||||
},
|
||||
});
|
||||
1
bonobo/contrib/jupyter/static/index.js.map
Normal file
1
bonobo/contrib/jupyter/static/index.js.map
Normal file
File diff suppressed because one or more lines are too long
@ -159,6 +159,14 @@ class NodeExecutionContext(WithStatistics, LoopingExecutionContext):
|
||||
# self._exec_count += 1
|
||||
pass
|
||||
|
||||
def as_dict(self):
|
||||
return {
|
||||
'status': self.status,
|
||||
'name': self.name,
|
||||
'stats': self.get_statistics_as_string(),
|
||||
'flags': self.get_flags_as_string(),
|
||||
}
|
||||
|
||||
|
||||
def isflag(param):
|
||||
return isinstance(param, Token) and param in (NOT_MODIFIED, )
|
||||
|
||||
@ -8,6 +8,7 @@ from bonobo.constants import BEGIN, END
|
||||
from bonobo.execution.strategies.base import Strategy
|
||||
from bonobo.util import get_name
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class ExecutorStrategy(Strategy):
|
||||
"""
|
||||
@ -30,8 +31,7 @@ class ExecutorStrategy(Strategy):
|
||||
try:
|
||||
context.start(self.get_starter(executor, futures))
|
||||
except:
|
||||
logging.getLogger(__name__
|
||||
).warning('KeyboardInterrupt received. Trying to terminate the nodes gracefully.')
|
||||
logger.critical('Exception caught while starting execution context.', exc_info=sys.exc_info())
|
||||
|
||||
while context.alive:
|
||||
try:
|
||||
|
||||
@ -1 +0,0 @@
|
||||
""" Extensions, not required. """
|
||||
@ -1,10 +0,0 @@
|
||||
from .plugin import JupyterOutputPlugin
|
||||
|
||||
|
||||
def _jupyter_nbextension_paths():
|
||||
return [{'section': 'notebook', 'src': 'static', 'dest': 'bonobo-jupyter', 'require': 'bonobo-jupyter/extension'}]
|
||||
|
||||
|
||||
__all__ = [
|
||||
'JupyterOutputPlugin',
|
||||
]
|
||||
@ -1,19 +0,0 @@
|
||||
Bonobo integration in Jupyter
|
||||
|
||||
Package Install
|
||||
---------------
|
||||
|
||||
**Prerequisites**
|
||||
- [node](http://nodejs.org/)
|
||||
|
||||
```bash
|
||||
npm install --save bonobo-jupyter
|
||||
```
|
||||
|
||||
Watch mode (for development)
|
||||
----------------------------
|
||||
|
||||
```bash
|
||||
./node_modules/.bin/webpack --watch
|
||||
``
|
||||
|
||||
1
bonobo/ext/jupyter/js/dist/index.js.map
vendored
1
bonobo/ext/jupyter/js/dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
@ -1,26 +0,0 @@
|
||||
import logging
|
||||
|
||||
from bonobo.ext.jupyter.widget import BonoboWidget
|
||||
from bonobo.plugins import Plugin
|
||||
|
||||
try:
|
||||
import IPython.core.display
|
||||
except ImportError as e:
|
||||
logging.exception(
|
||||
'You must install Jupyter to use the bonobo Jupyter extension. Easiest way is to install the '
|
||||
'optional "jupyter" dependencies with «pip install bonobo[jupyter]», but you can also install a '
|
||||
'specific version by yourself.'
|
||||
)
|
||||
|
||||
|
||||
class JupyterOutputPlugin(Plugin):
|
||||
def initialize(self):
|
||||
self.widget = BonoboWidget()
|
||||
IPython.core.display.display(self.widget)
|
||||
|
||||
def run(self):
|
||||
self.widget.value = [
|
||||
str(self.context.parent[i]) for i in self.context.parent.graph.topologically_sorted_indexes
|
||||
]
|
||||
|
||||
finalize = run
|
||||
File diff suppressed because one or more lines are too long
@ -3,8 +3,11 @@ class Plugin:
|
||||
A plugin is an extension to the core behavior of bonobo. If you're writing transformations, you should not need
|
||||
to use this interface.
|
||||
|
||||
For examples, you can read bonobo.ext.console.ConsoleOutputPlugin, or bonobo.ext.jupyter.JupyterOutputPlugin that
|
||||
respectively permits an interactive output on an ANSI console and a rich output in a jupyter notebook.
|
||||
For examples, you can read bonobo.plugins.console.ConsoleOutputPlugin, or bonobo.plugins.jupyter.JupyterOutputPlugin
|
||||
that respectively permits an interactive output on an ANSI console and a rich output in a jupyter notebook. Note
|
||||
that you most probably won't instanciate them by yourself at runtime, as it's the default behaviour of bonobo to use
|
||||
them if your in a compatible context (aka an interactive terminal for the console plugin, or a jupyter notebook for
|
||||
the notebook plugin.)
|
||||
|
||||
Warning: THE PLUGIN API IS PRE-ALPHA AND WILL EVOLVE BEFORE 1.0, DO NOT RELY ON IT BEING STABLE!
|
||||
|
||||
|
||||
35
bonobo/plugins/jupyter.py
Normal file
35
bonobo/plugins/jupyter.py
Normal file
@ -0,0 +1,35 @@
|
||||
import logging
|
||||
|
||||
from bonobo.contrib.jupyter.widget import BonoboWidget
|
||||
from bonobo.execution import events
|
||||
from bonobo.plugins import Plugin
|
||||
|
||||
try:
|
||||
import IPython.core.display
|
||||
except ImportError as e:
|
||||
logging.exception(
|
||||
'You must install Jupyter to use the bonobo Jupyter extension. Easiest way is to install the '
|
||||
'optional "jupyter" dependencies with «pip install bonobo[jupyter]», but you can also install a '
|
||||
'specific version by yourself.'
|
||||
)
|
||||
|
||||
|
||||
class JupyterOutputPlugin(Plugin):
|
||||
def register(self, dispatcher):
|
||||
dispatcher.add_listener(events.START, self.setup)
|
||||
dispatcher.add_listener(events.TICK, self.tick)
|
||||
dispatcher.add_listener(events.STOPPED, self.tick)
|
||||
|
||||
def unregister(self, dispatcher):
|
||||
dispatcher.remove_listener(events.STOPPED, self.tick)
|
||||
dispatcher.remove_listener(events.TICK, self.tick)
|
||||
dispatcher.remove_listener(events.START, self.setup)
|
||||
|
||||
def setup(self, event):
|
||||
self.widget = BonoboWidget()
|
||||
IPython.core.display.display(self.widget)
|
||||
|
||||
def tick(self, event):
|
||||
self.widget.value = [
|
||||
event.context[i].as_dict() for i in event.context.graph.topologically_sorted_indexes
|
||||
]
|
||||
@ -1,6 +1,9 @@
|
||||
import html
|
||||
import json
|
||||
from copy import copy
|
||||
|
||||
from graphviz.dot import Digraph
|
||||
|
||||
from bonobo.constants import BEGIN
|
||||
from bonobo.util import get_name
|
||||
|
||||
@ -112,23 +115,31 @@ class Graph:
|
||||
self._topologcally_sorted_indexes_cache = tuple(filter(lambda i: type(i) is int, reversed(order)))
|
||||
return self._topologcally_sorted_indexes_cache
|
||||
|
||||
@property
|
||||
def graphviz(self):
|
||||
try:
|
||||
return self._graphviz
|
||||
except AttributeError:
|
||||
g = Digraph()
|
||||
g.attr(rankdir='LR')
|
||||
g.node('BEGIN', shape='point')
|
||||
for i in self.outputs_of(BEGIN):
|
||||
g.edge('BEGIN', str(i))
|
||||
for ix in self.topologically_sorted_indexes:
|
||||
g.node(str(ix), label=get_name(self[ix]))
|
||||
for iy in self.outputs_of(ix):
|
||||
g.edge(str(ix), str(iy))
|
||||
self._graphviz = g
|
||||
return self._graphviz
|
||||
|
||||
def _repr_dot_(self):
|
||||
src = [
|
||||
'digraph {',
|
||||
' rankdir = LR;',
|
||||
' "BEGIN" [shape="point"];',
|
||||
]
|
||||
return str(self.graphviz)
|
||||
|
||||
for i in self.outputs_of(BEGIN):
|
||||
src.append(' "BEGIN" -> ' + _get_graphviz_node_id(self, i) + ';')
|
||||
def _repr_svg_(self):
|
||||
return self.graphviz._repr_svg_()
|
||||
|
||||
for ix in self.topologically_sorted_indexes:
|
||||
for iy in self.outputs_of(ix):
|
||||
src.append(' {} -> {};'.format(_get_graphviz_node_id(self, ix), _get_graphviz_node_id(self, iy)))
|
||||
|
||||
src.append('}')
|
||||
|
||||
return '\n'.join(src)
|
||||
def _repr_html_(self):
|
||||
return '<div>{}</div><pre>{}</pre>'.format(self.graphviz._repr_svg_(), html.escape(repr(self)))
|
||||
|
||||
def _resolve_index(self, mixed):
|
||||
""" Find the index based on various strategies for a node, probably an input or output of chain. Supported inputs are indexes, node values or names.
|
||||
|
||||
Reference in New Issue
Block a user