Merge pull request #6670 from jdfreder/output_widget
Output widget
commit
7252efd22c
@ -0,0 +1,57 @@
|
||||
// Copyright (c) IPython Development Team.
|
||||
// Distributed under the terms of the Modified BSD License.
|
||||
|
||||
define([
|
||||
"widgets/js/widget",
|
||||
"jquery",
|
||||
'notebook/js/outputarea',
|
||||
], function(widget, $, outputarea) {
|
||||
'use strict';
|
||||
|
||||
var OutputView = widget.DOMWidgetView.extend({
|
||||
initialize: function (parameters) {
|
||||
// Public constructor
|
||||
OutputView.__super__.initialize.apply(this, [parameters]);
|
||||
this.model.on('msg:custom', this._handle_route_msg, this);
|
||||
},
|
||||
|
||||
render: function(){
|
||||
// Called when view is rendered.
|
||||
this.output_area = new outputarea.OutputArea({
|
||||
selector: this.$el,
|
||||
prompt_area: false,
|
||||
events: this.model.widget_manager.notebook.events,
|
||||
keyboard_manager: this.model.widget_manager.keyboard_manager });
|
||||
|
||||
// Make output area reactive.
|
||||
var that = this;
|
||||
this.output_area.element.on('changed', function() {
|
||||
that.model.set('contents', that.output_area.element.html());
|
||||
});
|
||||
this.model.on('change:contents', function(){
|
||||
var html = this.model.get('contents');
|
||||
if (this.output_area.element.html() != html) {
|
||||
this.output_area.element.html(html);
|
||||
}
|
||||
}, this);
|
||||
|
||||
// Set initial contents.
|
||||
this.output_area.element.html(this.model.get('contents'));
|
||||
},
|
||||
|
||||
_handle_route_msg: function(content) {
|
||||
var cell = this.options.cell;
|
||||
if (content && cell) {
|
||||
if (content.method == 'push') {
|
||||
cell.push_output_area(this.output_area);
|
||||
} else if (content.method == 'pop') {
|
||||
cell.pop_output_area(this.output_area);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
'OutputView': OutputView,
|
||||
};
|
||||
});
|
||||
@ -0,0 +1,50 @@
|
||||
"""Output class.
|
||||
|
||||
Represents a widget that can be used to display output within the widget area.
|
||||
"""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
from .widget import DOMWidget
|
||||
import sys
|
||||
from IPython.utils.traitlets import Unicode, List
|
||||
from IPython.display import clear_output
|
||||
from IPython.testing.skipdoctest import skip_doctest
|
||||
|
||||
@skip_doctest
|
||||
class Output(DOMWidget):
|
||||
"""Widget used as a context manager to display output.
|
||||
|
||||
This widget can capture and display stdout, stderr, and rich output. To use
|
||||
it, create an instance of it and display it. Then use it as a context
|
||||
manager. Any output produced while in it's context will be captured and
|
||||
displayed in it instead of the standard output area.
|
||||
|
||||
Example
|
||||
from IPython.html import widgets
|
||||
from IPython.display import display
|
||||
out = widgets.Output()
|
||||
display(out)
|
||||
|
||||
print('prints to output area')
|
||||
|
||||
with out:
|
||||
print('prints to output widget')"""
|
||||
_view_name = Unicode('OutputView', sync=True)
|
||||
|
||||
def clear_output(self, *pargs, **kwargs):
|
||||
with self:
|
||||
clear_output(*pargs, **kwargs)
|
||||
|
||||
def __enter__(self):
|
||||
self._flush()
|
||||
self.send({'method': 'push'})
|
||||
|
||||
def __exit__(self, exception_type, exception_value, traceback):
|
||||
self._flush()
|
||||
self.send({'method': 'pop'})
|
||||
|
||||
def _flush(self):
|
||||
sys.stdout.flush()
|
||||
sys.stderr.flush()
|
||||
Loading…
Reference in new issue