Skip to content

Commit 2094526

Browse files
authored
Merge pull request #3370 from MrBago/html-ow-update
Allow Output widget in html manager to render state updates
2 parents 11ade00 + 42af1b6 commit 2094526

File tree

4 files changed

+161
-15
lines changed

4 files changed

+161
-15
lines changed

examples/web3/jupyter-logo.svg

Lines changed: 90 additions & 0 deletions
Loading

examples/web3/widget_code.json

Lines changed: 14 additions & 9 deletions
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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,34 @@ export class OutputModel extends outputBase.OutputModel {
1818
return {
1919
...super.defaults(),
2020
msg_id: '',
21+
outputs: [],
2122
};
2223
}
2324

2425
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(): OutputAreaModel {
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,44 @@ describe('Output widget', function () {
197197
await manager.display_view(manager.create_view(model), widgetTag);
198198
expect(widgetTag.innerText).to.equal('something different');
199199
});
200+
201+
it('renders text output', async () => {
202+
const manager = new HTMLManager();
203+
const modelId = 'u-u-i-d';
204+
const modelCreate: base.IModelOptions = {
205+
model_name: 'OutputModel',
206+
model_id: modelId,
207+
model_module: '@jupyter-widgets/output',
208+
model_module_version: '*',
209+
};
210+
211+
const startingText = 'starting text';
212+
const endingText = 'ending text';
213+
const modelState = {
214+
_view_module: '@jupyter-widgets/output',
215+
outputs: [
216+
{
217+
output_type: 'stream',
218+
name: 'stdout',
219+
text: startingText,
220+
},
221+
],
222+
};
223+
224+
const widgetTag = document.createElement('div');
225+
widgetTag.className = 'widget-subarea';
226+
document.body.appendChild(widgetTag);
227+
const model = await manager.new_model(modelCreate, modelState);
228+
await manager.display_view(manager.create_view(model), widgetTag);
229+
expect(widgetTag.innerText).to.equal(startingText);
230+
231+
model.set('outputs', [
232+
{
233+
output_type: 'stream',
234+
name: 'stdout',
235+
text: endingText,
236+
},
237+
]);
238+
expect(widgetTag.innerText).to.equal(endingText);
239+
});
200240
});

0 commit comments

Comments
 (0)