Skip to content

Commit 3c1bfe2

Browse files
committed
Backport PR jupyter-widgets#3370: Allow Output widget in html manager to render state updates
1 parent 02e8963 commit 3c1bfe2

File tree

4 files changed

+164
-18
lines changed

4 files changed

+164
-18
lines changed

examples/web3/jupyter-logo.svg

+90
Loading

examples/web3/widget_code.json

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
[
2-
"from ipywidgets import IntSlider, Text, VBox",
3-
"from IPython.display import display",
2+
"from ipywidgets import IntSlider, Output, Text, VBox",
3+
"from IPython.display import Image, display",
44
"",
5-
"s = IntSlider(max=200, value=100)",
6-
"t = Text()",
5+
"s = IntSlider(min=1, max=500, value=200, description='Width')",
6+
"t = Text(description='Area', disabled=True)",
7+
"o = Output()",
8+
"display(VBox([s, t, o]))",
79
"",
8-
"def update_text(change=None):",
9-
" t.value = str(s.value ** 2)",
10+
"def update(change=None):",
11+
" width = s.value",
12+
" t.value = str(width ** 2)",
13+
" o.outputs = ()",
14+
" img = Image(url='jupyter-logo.svg', width=width)",
15+
" o.append_display_data(img)",
1016
"",
11-
"s.observe(update_text, names='value')",
12-
"update_text()",
13-
"display(VBox([s, t]))"
17+
"s.observe(update, names='value')",
18+
"update()"
1419
]

packages/html-manager/src/output.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,38 @@ import $ from 'jquery';
1414
import '../css/output.css';
1515

1616
export class OutputModel extends outputBase.OutputModel {
17-
defaults() {
17+
defaults(): Backbone.ObjectHash {
1818
return {
1919
...super.defaults(),
20-
msg_id: ''
20+
msg_id: '',
21+
outputs: [],
2122
};
2223
}
2324

24-
initialize(attributes: any, options: any) {
25+
initialize(attributes: any, options: any): void {
2526
super.initialize(attributes, options);
26-
this._outputs = new OutputAreaModel({
27-
values: attributes.outputs,
28-
// Widgets (including this output widget) are only rendered in
29-
// trusted contexts
30-
trusted: true,
31-
});
27+
this._outputs = new OutputAreaModel({ trusted: true });
28+
this.listenTo(this, 'change:outputs', this.setOutputs);
29+
this.setOutputs();
3230
}
3331

3432
get outputs() {
3533
return this._outputs;
3634
}
3735

36+
clear_output(wait = false): void {
37+
this._outputs.clear(wait);
38+
}
39+
40+
setOutputs(model?: any, value?: any, options?: any): void {
41+
if (!(options && options.newMessage)) {
42+
// fromJSON does not clear the existing output
43+
this.clear_output();
44+
// fromJSON does not copy the message, so we make a deep copy
45+
this._outputs.fromJSON(JSON.parse(JSON.stringify(this.get('outputs'))));
46+
}
47+
}
48+
3849
private _outputs: OutputAreaModel;
3950
widget_manager: HTMLManager;
4051
}

packages/html-manager/test/src/output_test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,44 @@ describe('Output widget', function() {
205205
);
206206
expect(widgetTag.innerText).to.equal('something different');
207207
});
208+
209+
it('renders text output', async () => {
210+
const manager = new HTMLManager();
211+
const modelId = 'u-u-i-d';
212+
const modelCreate: base.IModelOptions = {
213+
model_name: 'OutputModel',
214+
model_id: modelId,
215+
model_module: '@jupyter-widgets/output',
216+
model_module_version: '*',
217+
};
218+
219+
const startingText = 'starting text';
220+
const endingText = 'ending text';
221+
const modelState = {
222+
_view_module: '@jupyter-widgets/output',
223+
outputs: [
224+
{
225+
output_type: 'stream',
226+
name: 'stdout',
227+
text: startingText,
228+
},
229+
],
230+
};
231+
232+
const widgetTag = document.createElement('div');
233+
widgetTag.className = 'widget-subarea';
234+
document.body.appendChild(widgetTag);
235+
const model = await manager.new_model(modelCreate, modelState);
236+
await manager.display_view(manager.create_view(model), widgetTag);
237+
expect(widgetTag.innerText).to.equal(startingText);
238+
239+
model.set('outputs', [
240+
{
241+
output_type: 'stream',
242+
name: 'stdout',
243+
text: endingText,
244+
},
245+
]);
246+
expect(widgetTag.innerText).to.equal(endingText);
247+
});
208248
});

0 commit comments

Comments
 (0)